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

江枫思渺然 提交于 2019-12-12 09:43:51

问题


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

Thanks a lot


回答1:


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();

                }
            }
        }


来源:https://stackoverflow.com/questions/6939280/how-to-set-copyright-metadata-of-an-existing-pdf-using-itextsharp-for-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!