如何正確地從現有的磁盤上的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文檔?提前爲您對此的沉思
感謝和讚賞。請讓我知道我是否可以提供進一步的信息。
Thanks @Jonathan,如果我不能調用Document.Open(),因爲文檔已經打開並且已經添加了很多元素,該怎麼辦? – Troy 2012-07-27 00:57:36
@Nevyn您應該能夠關閉文檔,然後通過合併過程重新打開它。您只需關閉文檔,然後將文檔的字節數組傳遞給合併方法,該方法將返回新合併文檔的內存流。 – Jonathan 2012-07-27 13:07:42