繁体   English   中英

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

[英]Remove metadata from existing Pdf using iTextsharp

我创建了一个pdf文件,并向其中添加了元数据,并使用iTextsharp库对其进行了加密。 现在,我想从pdf中删除加密。 我使用iTextSharp成功做到了这一点,但无法删除我添加的元数据。 任何人都可以请我欺骗我如何删除元数据。 这非常紧急。

谢谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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