使用 iTextsharp 从现有 Pdf 中删除元数据

发布于 2025-01-02 00:58:08 字数 137 浏览 0 评论 0原文

我创建了一个 pdf 并向其中添加了元数据,并使用 iTextsharp 库对其进行了加密。 现在我想从 pdf 中删除加密。我使用 iTextSharp 成功完成了此操作,但无法删除我添加的元数据。 谁能告诉我如何删除元数据。其紧急。

谢谢。

I created a pdf and added a metadata into it and also encrypted it uisng iTextsharp library.
Now I want to remove the encryption from the pdf. I successfully did so using iTextSharp but was not able to remove the metadata that I added.
Can anyone please giude me how can I remove the metadata. Its urgent.

Thanks.

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

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

发布评论

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

评论(1

别靠近我心 2025-01-09 00:58:08

删除元数据时,最简单的方法是直接使用 PdfReader 对象。完成此操作后,您可以将其写回磁盘。下面的代码是面向 iTextSharp 5.1.2.0 的完整工作 C# 2010 WinForms 应用程序。它首先创建带有一些元数据的 PDF,然后使用 PdfReader 修改 PDF 的内存版本,最后将更改写入磁盘。请参阅代码以获取其他注释。

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            //File with meta data added
            string InputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            //File with meta data removed
            string OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");

            //Create a file with meta data, nothing special here
            using (FileStream FS = new FileStream(InputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document Doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(Doc, FS)) {
                        Doc.Open();
                        Doc.Add(new Paragraph("Test"));
                        //Add a standard header
                        Doc.AddTitle("This is a test");
                        //Add a custom header
                        Doc.AddHeader("Test Header", "This is also a test");
                        Doc.Close();
                    }
                }
            }

            //Read our newly created file
            PdfReader R = new PdfReader(InputFile);
            //Loop through each piece of meta data and remove it
            foreach (KeyValuePair<string, string> KV in R.Info) {
                R.Info.Remove(KV.Key);
            }

            //The code above modifies an in-memory representation of the PDF, we need to write these changes to disk now
            using (FileStream FS = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document Doc = new Document()) {
                    //Use the PdfCopy object to copy each page
                    using (PdfCopy writer = new PdfCopy(Doc, FS)) {
                        Doc.Open();
                        //Loop through each page
                        for (int i = 1; i <= R.NumberOfPages; i++) {
                            //Add it to the new document
                            writer.AddPage(writer.GetImportedPage(R, i));
                        }
                        Doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}

When removing meta data it is easiest to work directly with the PdfReader object. Once you do that you can write that back to disk. The code below is a full working C# 2010 WinForms application targeting iTextSharp 5.1.2.0. It first creates a PDF with some meta data, then it modifies an in-memory version of the PDF using a PdfReader, and finally writes the changes to disk. See the code for additional comments.

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            //File with meta data added
            string InputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            //File with meta data removed
            string OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");

            //Create a file with meta data, nothing special here
            using (FileStream FS = new FileStream(InputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document Doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(Doc, FS)) {
                        Doc.Open();
                        Doc.Add(new Paragraph("Test"));
                        //Add a standard header
                        Doc.AddTitle("This is a test");
                        //Add a custom header
                        Doc.AddHeader("Test Header", "This is also a test");
                        Doc.Close();
                    }
                }
            }

            //Read our newly created file
            PdfReader R = new PdfReader(InputFile);
            //Loop through each piece of meta data and remove it
            foreach (KeyValuePair<string, string> KV in R.Info) {
                R.Info.Remove(KV.Key);
            }

            //The code above modifies an in-memory representation of the PDF, we need to write these changes to disk now
            using (FileStream FS = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document Doc = new Document()) {
                    //Use the PdfCopy object to copy each page
                    using (PdfCopy writer = new PdfCopy(Doc, FS)) {
                        Doc.Open();
                        //Loop through each page
                        for (int i = 1; i <= R.NumberOfPages; i++) {
                            //Add it to the new document
                            writer.AddPage(writer.GetImportedPage(R, i));
                        }
                        Doc.Close();
                    }
                }
            }

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