我編寫了一個C#
程序,用於從PDF文件中刪除沒有任何操作的註釋。我使用iTextSharp
。但是,在我的一個文件中,刪除了一個非空註釋(這是JavaScript操作),而其他非空註釋未被刪除。是這樣嗎?是因爲我的代碼還是有問題PDF file?從PDF中刪除空註釋
using System;
using System.IO;
using iTextSharp.text.pdf;
namespace removeEmpty
{
class Program
{
public static void Main()
{
//Bytes will hold our final PDFs
byte[] bytes;
using (var ms = new MemoryStream()) {
using (var reader = new PdfReader("sandri6.pdf")) {
using (var stamper = new PdfStamper(reader, ms)) {
for (int i = 1; i <= reader.NumberOfPages; i++) {
// get a page a PDF page
PdfDictionary page = reader.GetPageN(i);
// get all the annotations of page i
PdfArray annotationsArray = page.GetAsArray(PdfName.ANNOTS);
// if page does not have annotations
if (annotationsArray == null) {
continue;
}
// for each annotation
for (int j = 0; j < annotationsArray.Size; j++) {
// for current annotation
PdfDictionary currentAnnotation = annotationsArray.GetAsDict(j);
PdfDictionary annotationAction = currentAnnotation.GetAsDict(PdfName.AA);
if (annotationAction == null) {
annotationsArray.Remove(j);
Console.Write("Removed annotation {0} with no action from page {1}\n", j, i);
}
}
}
}
}
//grab the bytes before closing things out
bytes = ms.ToArray();
}
//write thebytes to disk
File.WriteAllBytes("output.pdf", bytes);
Console.ReadKey();
}
}
}
謝謝你的回答。這是否意味着** AA **入門商店JavaScript在第二個註釋中。我很困惑,因爲在另一個[文件](https://www.upload.ee/files/6680430/sandri6.pdf.html)註釋是空的(Adobe Acrobat如此說),它們不會被代碼刪除。我該如何解決? – menteith
@menteith *「這是否意味着AA條目將JavaScript存儲在第二個註釋中。」* - 否如截圖所示,第二個註釋既沒有** A也沒有** AA **條目。 – mkl
@menteith關於你的其他文件:我無法重現你的觀察。恰恰那些沒有動作的註釋(正好是作者名稱周圍的鏈接註釋)被刪除。我通過檢查PDF對象並單擊Acrobat進行檢查。 – mkl