简体   繁体   中英

How to set copyright metadata of an existing PDF using iTextSharp for C#

How can the copyright metadata of an existing (ie a pdf loaded from file or memory stream) pdf file be set using iTextSharp for C#?

Thanks a lot

The native XMP structures don't have copyright implemented (or at least they don't in a way that Adobe Reader recognizes.) To do that you can reverse engineer what Adobe kicks out and write it manually:

        String inputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services.pdf");
        String outputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services_Out.pdf");

        PdfReader reader = new PdfReader(inputPDF);
        using (FileStream fs = new FileStream(outputPDF, FileMode.Create, FileAccess.Write, FileShare.Read))
        {
            using (PdfStamper stamper = new PdfStamper(reader, fs))
            {
                using (MemoryStream ms = new MemoryStream())
                {

                    string CopyrightName = "YOUR NAME HERE";
                    string CopyrightUrl = "http://www.example.com/";

                    XmpWriter xmp = new XmpWriter(ms);
                    xmp.AddRdfDescription("xmlns:dc=\"http://purl.org/dc/elements/1.1/\"", String.Format("<dc:rights><rdf:Alt><rdf:li xml:lang=\"x-default\">{0}</rdf:li></rdf:Alt></dc:rights>", CopyrightName));
                    xmp.AddRdfDescription("xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\"", string.Format("<xmpRights:Marked>True</xmpRights:Marked><xmpRights:WebStatement>{0}</xmpRights:WebStatement>", CopyrightUrl));
                    xmp.Close();
                    stamper.XmpMetadata = ms.ToArray();
                    stamper.Close();

                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM