既存のディスク上の PDF ファイルから、現在生成中のメモリ内 PDF ドキュメントにページを正しく追加するにはどうすればよいですか?
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();
その結果、 で NullReferenceException が発生しcopier.AddPage(newpage)
ます。
私も試しました:
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 の質問と回答から得ています。誰も対処していないように見えることの 1 つは、既存の PDF ファイルから、ディスク上の PDF ファイルにまだ書き込まれていない既存のメモリ内ドキュメントに新しいページを追加することです。このドキュメントは既に開かれており、ページ内にデータが書き込まれています。この利用規約の手順を省略したり、(元のように) 画像であると書いたりすると、結果の PDF は問題なく出力されます。
開始したとおりに終了するには: 既存のディスク上の PDF ファイルから、現在生成中のメモリ内 PDF ドキュメントにページを正しく追加するにはどうすればよいですか?
これについてのあなたの熟考を前もって感謝し、感謝します。さらに情報を提供できるかどうかお知らせください。