2012-07-25 67 views
5

如何正確地從現有的磁盤上的PDF文件添加的頁面(S),以當前的福利產生的內存PDF文檔的文件不成文?從文件的現有PDF添加到使用iTextSharp的

我們有生產使用iTextSharp的PDF文檔類。這工作正常。目前,我們在最後一個頁面中添加條款&條件爲圖像:

this.nettPriceListDocument.NewPage(); 
this.currentPage++; 
Image logo = Image.GetInstance("/inetpub/Applications/Trade/Reps/images/TermsAndConditions.gif"); 
logo.SetAbsolutePosition(0, 0); 
logo.ScaleToFit(this.nettPriceListDocument.PageSize.Width, this.nettPriceListDocument.PageSize.Height); 
this.nettPriceListDocument.Add(logo); 

我有這樣的圖像作爲PDF文檔,並希望將其追加。如果我能弄明白這一點,可以將我們可能需要的其他PDF文檔附加到我所生成的內容中。

我曾嘗試:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf"; 
PdfReader reader = new PdfReader(tcfile); 
PdfWriter writer = PdfWriter.GetInstance(this.nettPriceListDocument, this.nettPriceListMemoryStream); 
PdfContentByte content = writer.DirectContentUnder; 
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++) 
{ 
    this.nettPriceListDocument.NewPage(); 
    this.currentPage++; 
    PdfImportedPage newpage = writer.GetImportedPage(reader, pageno); 
    content.AddTemplate(newpage, 1f, 1f); 
} 

這會導致在writer.DirectContentUnder

「文件無法打開」 例外,我也試過:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf"; 
PdfReader reader = new PdfReader(tcfile); 
PdfConcatenate concat = new PdfConcatenate(this.nettPriceListMemoryStream); 
concat.AddPages(reader); 

導致插入空白,頁面大小通常在文檔的第一頁。

我也試過:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf"; 
PdfReader reader = new PdfReader(tcfile); 
PdfCopy copier = new PdfCopy(nettPriceListDocument, nettPriceListMemoryStream); 
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++) 
{ 
    this.currentPage++; 
    PdfImportedPage newpage = copier.GetImportedPage(reader, pageno); 
    copier.AddPage(newpage); 
} 
copier.Close(); 
reader.Close(); 

其中在copier.AddPage(newpage)導致一個NullReferenceException。

我也試過:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf"; 
PdfReader reader = new PdfReader(tcfile); 
PdfCopyFields copier = new PdfCopyFields(nettPriceListMemoryStream); 
copier.AddDocument(reader); 

這也導致一個NullReferenceException在copier.AddDocument(reader)

我已經得到了大多數的這些想法來自不同StackOverflow的問題和答案。有一件事似乎沒有人處理,就是將現有PDF文件中的新頁面添加到已存在的尚未寫入磁盤上的PDF文件的內存文檔中。這個文件已經被打開,並且有數據頁面被寫入。如果我離開這個條款&條件過程出來,或者只是寫它是一個圖像(最初),結果PDF出來就很好。

要完成我開始:如何正確地從現有的磁盤上的PDF文件添加的頁面(S),以當前的福利產生的內存PDF文檔?提前爲您對此的沉思

感謝和讚賞。請讓我知道我是否可以提供進一步的信息。

回答

7

這裏有一個簡單的合併方法是複製的PDF文件成一個PDF。合併pdf時,我經常使用這種方法。我已經用它來合併不同大小的頁面,而且沒有問題。希望能幫助到你。

public MemoryStream MergePdfForms(List<byte[]> files) 
{ 
    if (files.Count > 1) 
    { 
     PdfReader pdfFile; 
     Document doc; 
     PdfWriter pCopy; 
     MemoryStream msOutput = new MemoryStream(); 

     pdfFile = new PdfReader(files[0]); 

     doc = new Document(); 
     pCopy = new PdfSmartCopy(doc, msOutput); 

     doc.Open(); 

     for (int k = 0; k < files.Count; k++) 
     { 
      pdfFile = new PdfReader(files[k]); 
      for (int i = 1; i < pdfFile.NumberOfPages + 1; i++) 
      { 
       ((PdfSmartCopy)pCopy).AddPage(pCopy.GetImportedPage(pdfFile, i)); 
      } 
      pCopy.FreeReader(pdfFile); 
     } 

     pdfFile.Close(); 
     pCopy.Close(); 
     doc.Close(); 

     return msOutput; 
    } 
    else if (files.Count == 1) 
    { 
     return new MemoryStream(files[0]); 
    } 

    return null; 
} 
+0

Thanks @Jonathan,如果我不能調用Document.Open(),因爲文檔已經打開並且已經添加了很多元素,該怎麼辦? – Troy 2012-07-27 00:57:36

+0

@Nevyn您應該能夠關閉文檔,然後通過合併過程重新打開它。您只需關閉文檔,然後將文檔的字節數組傳遞給合併方法,該方法將返回新合併文檔的內存流。 – Jonathan 2012-07-27 13:07:42