こんにちは私はスタンパーを使用した後でpdfにテーブルを追加しようとしています。PDFStamperを使用した後、iTextSharpをメモリストリーム内のドキュメントに追加する
// CREATE MEMORY STRING
MemoryStream ms = new MemoryStream();
string formFile = Server.MapPath("testImg.pdf");
PdfReader reader = new PdfReader(formFile);
PdfStamper outStamper = new PdfStamper(reader, ms);
AcroFields fields = outStamper.AcroFields;
// UPDATE THE FORM FIELDS
fields.SetField("Text1", "John Smith");
fields.SetField("Text2", "1234567890");
fields.SetField("Text3", "1234567890");
//ADD LOGO
iTextSharp.text.Image headerlogo = iTextSharp.text.Image.GetInstance(Server.MapPath("logo.jpg"));
headerlogo.ScaleToFit(140, 399);
headerlogo.Alignment = iTextSharp.text.Image.UNDERLYING;
headerlogo.SetAbsolutePosition(200, 500);
int pageCount = reader.NumberOfPages;
PdfContentByte body = outStamper.GetOverContent(pageCount);
body.AddImage(headerlogo);
outStamper.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=test");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
ドキュメントをテーブルに追加できます。
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.BorderWidth = 5;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
document.Add(table);
テーブルを追加するドキュメントの定義に問題があります。誰かが、メモリストリームのPDFにテーブルを追加する方法をアドバイスしてください。
ご協力いただければ幸いです。
アレックス
実際には、「スタンプされたPDFを中間ファイルに保存する」段階をスキップできます。 1:PDFで表を作成します。 2:そのPDFを開き、あなたのテーブルページを 'PdfImportedPage'に吸い込みます。 3:それを元のPDFにスタンプして、他の変更を加えます。 –
良いアイデア。確かに私は同じことをしていたはずでしたが、当時私には起こりませんでした。 +1 – Maverik