我创建了一个pdf并在其中添加了一个元数据,并且还使用iTextsharp库对其进行了加密。 现在我想从pdf中删除加密。我成功地使用了iTextSharp,但无法删除我添加的元数据。 任何人都可以请giude我如何删除元数据。这很紧急。使用iTextsharp从现有的PDF中删除元数据
谢谢。
我创建了一个pdf并在其中添加了一个元数据,并且还使用iTextsharp库对其进行了加密。 现在我想从pdf中删除加密。我成功地使用了iTextSharp,但无法删除我添加的元数据。 任何人都可以请giude我如何删除元数据。这很紧急。使用iTextsharp从现有的PDF中删除元数据
谢谢。
当删除元数据时,最容易直接使用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();
}
}
}
嗨,我添加了额外的肉质数据信息。我首先使用reader删除元数据,然后通过设置stamper的更多info属性来添加元数据。但元数据值仍然存在。 – Lipika 2012-02-02 06:43:02
欢迎使用Stackoverflow。恐怕我们无法帮助你,因为你的问题太短,缺乏细节。请阅读stackoverflow.com/questions/how-to-ask – 2012-02-02 12:36:52