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

发布于 2024-11-28 03:16:00 字数 76 浏览 1 评论 0原文

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

多谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-12-05 03:16:00

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

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

                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文