如何使用iTextSharp为C#设置现有PDF的版权元数据

时间:2011-08-04 09:49:10

标签: c# pdf metadata itextsharp

如何使用iTextSharp为C#设置现有(即从文件或内存流加载的pdf)pdf文件的版权元数据?

非常感谢

1 个答案:

答案 0 :(得分:2)

本机XMP结构没有实现版权(或者至少它们没有Adobe Reader识别的方式。)为此,您可以对Adobe开出并手动编写的内容进行反向工程:

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

                }
            }
        }