2015-02-11 13 views
0

我想添加一個便籤回覆到使用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); 
 
         } 
 
        } 
 
       } 
 
      } 
 
     }
我已經使用可能不準確的或有效的,因爲大部分的代碼,通過試驗和錯誤並檢查其他類似的例子(也檢查PDF規範)中發現的方法。 有人可以填寫該代碼,這有助於實現這一點。 注意: SO question沒有提供答案的代碼。

回答

0

請看看AddInReplyTo的例子。

我們有一個名爲hello_sticky_note.pdf文件看起來像這樣:

PDF with a sticky note

我要跳過的方法來檢測便籤的註解(你的問題,你已經有了這個代碼) 。在我的示例中,我知道此註釋是/Annots數組中的第一個條目(索引爲0的註釋)。

這是我要如何添加一個「答覆」註釋:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException { 
    PdfReader reader = new PdfReader(src); 
    PdfDictionary page = reader.getPageN(1); 
    PdfArray annots = page.getAsArray(PdfName.ANNOTS); 
    PdfDictionary sticky = annots.getAsDict(0); 
    PdfArray stickyRect = sticky.getAsArray(PdfName.RECT); 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); 
    PdfWriter writer = stamper.getWriter(); 
    Rectangle stickyRectangle = new Rectangle(
     stickyRect.getAsNumber(0).floatValue(), stickyRect.getAsNumber(1).floatValue(), 
     stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue() 
    ); 
    PdfAnnotation replySticky = PdfAnnotation.createText(
      writer, stickyRectangle, "Reply", "Hello PDF", true, "Comment"); 
    replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0)); 
    stamper.addAnnotation(replySticky, 1); 
    stamper.close(); 
} 

和你一樣,我得到的原始標註(在我的代碼,它的命名sticky)和我得到的該註釋的位置(stickyRect)。我創建一個stickyRectangle對象的方式與您做的稍微不同(我的方式更好,但這並不重要),我使用該stickyRectangle創建一個名爲replySticky的新PdfAnnotation

這就是你已經擁有的。現在我添加缺少的一部分:

replySticky.Put(PdfName.IRT, annots.GetAsIndirectObject(0)); 

在你的代碼,添加註釋詞典,但實際上你需要的是參考該字典。

所生成的PDF看起來像hello_in_reply_to.pdf

​​

+0

感謝。所以問題是,我試圖將PdfDictionary對象轉換爲indirectObject。相反,我應該從Annotation數組本身作爲一個間接對象獲取。所以缺少的代碼是:newAnnot.Put(PdfName.IRT,pageAnnotsArray.GetAsIndirectObject(0)); – 2015-02-11 12:51:15

+0

NM屬性缺少答覆。我想,它會自動生成。在外部數據庫中保存NM屬性是否可靠?即,如果用戶編輯pdf,是否有可能NM獲得新的值。如果NM改變,我將不再能夠識別評論。我將如何爲答覆生成一個新的獨特NM。 – 2015-02-11 13:13:12

+0

'/ NM'條目被定義爲*(可選)註釋名稱,在其頁面上的所有註釋中唯一標識它的文本字符串。*只要在那裏,您可以選擇回覆想要的'String'在頁面上沒有其他具有相同名稱的註釋。一旦名稱被定義,就不應該改變。您可以將NM存儲在數據庫中,以及PDF文件的ID和頁碼。 – 2015-02-11 15:31:12