我創建了一個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