您好我在使用压模后尝试将表添加到pdf中。
// 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。
非常感谢任何帮助。
Alex
答案 0 :(得分:1)
不确定你是如何让这两件作品一起工作的,但这就是我为我的应用做的。
基本上我将标记与添加结构化内容分开,然后将结果合并为一个大的pdf。您可以尝试使用table.WriteSelectedRows
(将PdfContentByte
作为其参数之一)在一个重要步骤中一起完成所有操作)但是我必须花费在手动布局上的时间往往超过让这一切都在一步完成的好处(除了它成为一个难以维护/重用的巨大方法的事实)
澄清&测试目的,我已经使用了FileStream,当然,如果您最终在开发代码中创建一个单片函数,那么同样适用于MemoryStream和重用流。
以下是我用于确认代码并运行场景的测试应用程序:
步骤:
注意:我没有使用图片加盖代码,如果using
关闭基础Document
,您可能需要在第一部分PdfStamper
上调整Document
(我不记得它是否从头顶开始,但如果你需要调整using
陈述,它会抛出一个有用的例外)
private static void Main(string[] args)
{
using (FileStream ms = new FileStream("C:\\Test2.pdf", FileMode.Create))
using (FileStream formFile = new FileStream("C:\\Test.pdf", FileMode.Open))
{
PdfReader reader = new PdfReader(formFile);
using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
{
//PdfStamper outStamper = new PdfStamper(reader, ms);
//PdfContentByte body = outStamper.GetOverContent(reader.NumberOfPages);
//document.Open(); //Open document to work with
//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);
//body.AddImage(headerlogo);
//outStamper.Close();
}
}
using (FileStream ms = new FileStream("C:\\Test3.pdf", FileMode.Create))
using (FileStream formFile = new FileStream("C:\\Test2.pdf", FileMode.Open))
{
PdfReader reader = new PdfReader(formFile);
using (Document document = new Document(reader.GetPageSizeWithRotation(1)))
{
PdfWriter pdfWriter = PdfWriter.GetInstance(document, ms);
document.Open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns")) { Colspan = 3, BorderWidth = 5, HorizontalAlignment = 1 };
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");
table.CompleteRow(); //Added - table won't add the final row if its cells are incomplete - safe to have it ending a table
document.Add(table);
}
}
using (FileStream ms = new FileStream("C:\\Test4.pdf", FileMode.Create))
using (FileStream stampedfile = new FileStream("C:\\Test2.pdf", FileMode.Open))
using (FileStream appendfile = new FileStream("C:\\Test3.pdf", FileMode.Open))
{
PdfReader stampedContentReader = new PdfReader(stampedfile);
PdfReader appendContentReader = new PdfReader(appendfile);
using (Document document = new Document(stampedContentReader.GetPageSizeWithRotation(1)))
{
PdfCopy pdfCopy = new PdfCopy(document, ms);
document.Open();
for (int i = 1; i <= stampedContentReader.NumberOfPages; i++)
pdfCopy.AddPage(pdfCopy.GetImportedPage(stampedContentReader, i));
for (int i = 1; i <= appendContentReader.NumberOfPages; i++)
pdfCopy.AddPage(pdfCopy.GetImportedPage(appendContentReader, i));
}
}
}
希望这有帮助。