嗨我正在嘗試在使用了壓模之後向pdf中添加表格。iTextSharp在使用PDFStamper之後將表格附加到內存流中的文檔
// 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。 – 2011-03-08 19:07:30
好主意。事實上,我應該做同樣的事情,但當時沒有發生在我身上。 +1 – Maverik 2011-03-09 09:40:41