我正在尝试在多个页面中添加一个表格,但PDF的其余部分保持不变。这是送货单。只是想知道我做错了什么,我猜它是PdfCopy
没有它,AcroFields工作。感谢。
string templateFile = @"c:\DeliveryNote.pdf";
using (Document doc = new Document())
{
using (MemoryStream ms = new MemoryStream())
{
using (PdfReader pdfReader = new PdfReader(templateFile))
{
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, ms, '\0', true))
{
pdfStamper.Writer.CloseStream = false;
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("CompanyName", "Test Limited");
pdfFormFields.SetField("CompanyTelephoneNumber", "Test Limited");
pdfFormFields.SetField("CompanyFaxNumber", "+44");
Rectangle rect = pdfFormFields.GetFieldPositions("ItemTable")[0].position;
ColumnText column = new ColumnText(pdfStamper.GetOverContent(1));
column.SetSimpleColumn(rect);
PdfPTable table = new PdfPTable(3);
table.AddCell("Qty");
table.AddCell("Product Code");
table.AddCell("Description");
for (int i = 0; i < 100; i++)
{
table.AddCell($"Value {i}");
table.AddCell($"Value {i}");
table.AddCell($"Value {i}");
}
column.AddElement(table);
while (true)
{
column.SetSimpleColumn(rect);
if (!ColumnText.HasMoreText(column.Go()))
break;
PdfCopy copy = new PdfCopy(doc, ms);
copy.CloseStream = false;
doc.Open();
copy.SetMergeFields();
copy.AddDocument(new PdfReader(templateFile));
}
}
}
doc.Close();
File.WriteAllBytes(@"C:\Temp\Lol.pdf", ms.ToArray());
Process.Start(@"C:\Temp\Lol.pdf");
}
}
1: