2017-08-04 36 views
3

我已經創建了一個用於輸入文件的閱讀器,另一個用於標記文件。我不確定是否應循環註釋,然後將它們逐個添加到輸出中,或者如果有方法從標記文件中提取所有註釋並將其添加到保留其x,z座標的輸入文件中。在另一個pdf的所有頁面中從一個pdf中添加一組註釋,並創建一個新的輸出pdf

我有下面的代碼,我不確定在評論部分做什麼。 AddAnnotation方法僅將PdfAnnotation作爲輸入,但我不確定如何將PdfDictionary轉換爲PdfAnnotaiton。

class Program 
{ 
    public static string inputFile = @"E:\pdf-sample.pdf"; 
    public static string markupFile = @"E:\StampPdf.pdf"; 
    public static string outputFile = @"E:\pdf.pdf"; 

    public static PdfReader inputReader = new PdfReader(inputFile); 
    public static PdfReader markupReader = new PdfReader(markupFile); 

    static void Main(string[] args) 
    { 
     PdfDocument inputDoc = new PdfDocument(inputReader, new PdfWriter(outputFile)); 

     PdfDocument markupDoc = new PdfDocument(markupReader); 

     int n = inputDoc.GetNumberOfPages(); 
     for (int i = 1; i <= n; i++) 
     { 
      PdfPage page = inputDoc.GetPage(i); 

      PdfDictionary markupPage = markupDoc.GetFirstPage().GetPdfObject(); 
      PdfArray annots = markupPage.GetAsArray(PdfName.Annots); 

      if(annots != null) 
      { 
       for(int j=0; j < annots.Size(); j++) 
       { 
        PdfDictionary annotItem = annots.GetAsDictionary(i); 
        //****** 
        //page.AddAnnotation(?); 
        //****** 
       } 
      } 
     } 
     inputDoc.Close(); 
    } 
} 

在iText7中發現新的GetAnnotations方法後,我嘗試了另一個變體。這裏的代碼運行良好,但我無法打開O/P文件,並得到文件損壞的錯誤。另外,當我運行inputDoc.Close()而不是下面給出的最後一行時,我得到一個錯誤「PDF間接對象屬於其他PDF文檔。複製對象到當前的PDF文檔「

 PdfReader ireader = new PdfReader(inputFile); 
     PdfDocument inputDoc = new PdfDocument(ireader, new PdfWriter(outputFile)); 
     PdfReader mreader = new PdfReader(markupFile); 
     PdfDocument markupDoc = new PdfDocument(mreader); 
     var annots = markupDoc.GetFirstPage().GetAnnotations(); 
     if (annots != null) 
     { 
      for (int j = 0; j < annots.Count(); j++) 
      { 
       inputDoc.GetFirstPage().AddAnnotation(annots[j]); 
      } 
     } 
     ireader.Close(); 
     mreader.Close(); 
     markupDoc.Close(); 
     inputDoc.SetCloseWriter(true); 
+0

你使用什麼庫來閱讀pdf的? – Piotr

+0

@Piotr - 我正在使用iText7 –

回答

1

也許試試這個:

if (annots != null) 
    { 
     for (int j = 0; j < annots.Size(); j++) 
     { 
      PdfDictionary annotItem = annots.GetAsDictionary(i); 
      PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(annotItem); 
      page.AddAnnotation(lineAnnotation); 
     } 
    } 

如果它不工作,這裏是一些文檔(不幸的是在Java中)

http://developers.itextpdf.com/examples/actions-and-annotations/clone-creating-and-adding-annotations

如果你可以發表你想複製的註釋的Pdf - 也許我可以調試並嘗試更多。

+0

@ Piotr我添加了我的輸入和標記pdf文件的圖像,你可以參考它,因爲你可以看到它包含許多註釋行。你可以幫我一些建議。或者你可以寄給我你的郵件ID,我可以郵寄你的PDF文件。 –

+0

上傳並給我們一個鏈接(例如:https://wetransfer.com/) - 這種方式不僅僅是我,但每個人都有機會幫助你) – Piotr

+0

https://wetransfer.com/downloads/ fb37905d3501892ff4808e487452928220170804122229/97c1b7 –

相關問題