我想添加一個便籤回覆到使用iTextSharp的pdf。我可以在pdf中創建新的註釋。但是我不能將它作爲已有註釋的孩子鏈接。我將父母中的大部分屬性複製到其子中。我通過分析答覆的屬性來複制它,方法是手動添加Adobe Reader的答覆。我缺少的是財產/ IRT。它需要對父級彈出框的引用。 Like/IRT 16 0 R. 以下是我正在嘗試的代碼。如何使用iTextSharp添加回復註釋
private void annotateReplyPdf()
{
string outputFile = @"D:\temp\temp.pdf";
// Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
using (PdfReader reader = new PdfReader(FILE_NAME))
{
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
// Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
//get page 1
PdfDictionary pageDic = reader.GetPageN(1);
//get annotations in page 1
PdfArray pageAnnotsArray = pageDic.GetAsArray(PdfName.ANNOTS);
if (pageAnnotsArray != null)
{
PdfDictionary curAnnotDic = pageAnnotsArray.GetAsDict(0);
PdfArray rect = curAnnotDic.GetAsArray(PdfName.RECT);
Rectangle rectangle = new Rectangle(float.Parse(rect[0].ToString()), float.Parse(rect[1].ToString()), float.Parse(rect[2].ToString()), float.Parse(rect[3].ToString()));
PdfAnnotation newAnnot = new PdfAnnotation(stamper.Writer, rectangle);
newAnnot.Title = "john.conor";
var dtNow = DateTime.Now;
newAnnot.Put(PdfName.C, curAnnotDic.Get(PdfName.C));
newAnnot.Put(PdfName.CONTENTS, new PdfString("Reply using prog"));
newAnnot.Put(PdfName.CREATIONDATE, new PdfDate(dtNow));
// newAnnot.Put(PdfName.IRT, curAnnotDic.); stuck here
newAnnot.Put(PdfName.M, new PdfDate(dtNow));
newAnnot.Put(PdfName.NAME, curAnnotDic.Get(PdfName.NAME));
newAnnot.Put(PdfName.RC, curAnnotDic.Get(PdfName.RC));
newAnnot.Put(PdfName.SUBTYPE, PdfName.TEXT);
newAnnot.Put(PdfName.SUBJECT, curAnnotDic.Get(PdfName.SUBJECT));
stamper.AddAnnotation(newAnnot, 1);
}
}
}
}
}
感謝。所以問題是,我試圖將PdfDictionary對象轉換爲indirectObject。相反,我應該從Annotation數組本身作爲一個間接對象獲取。所以缺少的代碼是:newAnnot.Put(PdfName.IRT,pageAnnotsArray.GetAsIndirectObject(0)); – 2015-02-11 12:51:15
NM屬性缺少答覆。我想,它會自動生成。在外部數據庫中保存NM屬性是否可靠?即,如果用戶編輯pdf,是否有可能NM獲得新的值。如果NM改變,我將不再能夠識別評論。我將如何爲答覆生成一個新的獨特NM。 – 2015-02-11 13:13:12
'/ NM'條目被定義爲*(可選)註釋名稱,在其頁面上的所有註釋中唯一標識它的文本字符串。*只要在那裏,您可以選擇回覆想要的'String'在頁面上沒有其他具有相同名稱的註釋。一旦名稱被定義,就不應該改變。您可以將NM存儲在數據庫中,以及PDF文件的ID和頁碼。 – 2015-02-11 15:31:12