我正在开发一个生成 PDF 文件(从 PDF 模板)的桌面应用程序。
生成 PDF 的方法接收这两个类型的变量DataTable
:
DataTable
tbl_template_variables:
DataTable
tbl_details_invoice:
这是模板:
使用itextsharp 我使用此代码生成 PDF:
private void GenerateInvoice(DataTable tbl_template_variables, DataTable tbl_details_invoice)
{
using (PdfReader pdfReader = new PdfReader(plantilla__Invoice__manual))
{
try
{
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(location_output_file, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// Loop DataTable and set the value in the specified field.
for (int i = 0; i < tbl_template_variables.Rows.Count; i++)
{
pdfFormFields.SetField(tbl_template_variables.Rows[i][0].ToString(), tbl_template_variables.Rows[i][1].ToString(), true);// set form pdfFormFields
}
#region Diseño grid _Invoice_
PdfPCell cell = null;
PdfPTable table = null;
table = new PdfPTable(9);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 22f, 22f, 22f, 22f, 22f, 22f, 22f, 22f, 22f });
//table.SpacingBefore = 5;
table.TotalWidth = 800f;
for (int i = 0; i < tbl_details_invoice.Rows.Count; i++)
{
DataRow row = tbl_details_invoice.Rows[i];
object Invoice_PDFColumn0_value = row.Field<string>("PROVIDER") == null ? string.Empty : row.Field<string>("PROVIDER").ToString();
object Invoice_PDFColumn1_value = row.Field<string>("DESCRIPTION") == null ? string.Empty : row.Field<string>("DESCRIPTION").ToString();
object Invoice_PDFColumn2_value = row.Field<string>("PPTO") == null ? string.Empty : row.Field<string>("PPTO").ToString();
object Invoice_PDFColumn3_value = row.Field<string>("JOB_MEDIA_TYPE") == null ? string.Empty : row.Field<string>("JOB_MEDIA_TYPE").ToString();
object Invoice_PDFColumn4_value = row.Field<string>("VEND_INV_NO") == null ? string.Empty : row.Field<string>("VEND_INV_NO").ToString();
//object Invoice_PDFColumn5_value = row.Field<string>("ORDER_MEDIA") == null ? string.Empty : row.Field<string>("ORDER_MEDIA").ToString();
//object Invoice_PDFColumn6_value = row.Field<string>("ACTIVITY_MONTH") == null ? string.Empty : row.Field<string>("ACTIVITY_MONTH").ToString();
object Invoice_PDFColumn7_value = row.Field<string>("COMMISSIONABLE") == null ? string.Empty : row.Field<string>("COMMISSIONABLE").ToString();
object Invoice_PDFColumn8_value = row.Field<string>("NON_COMMISSIONABLE") == null ? string.Empty : row.Field<string>("NON_COMMISSIONABLE").ToString();
string Invoice_PDFColumn9_value = row.Field<string>("IVA_PROVEEDOR") == null ? string.Empty : row.Field<string>("IVA_PROVEEDOR").ToString();
string Invoice_PDFColumn10_value = row.Field<string>("TOTAL") == null ? string.Empty : row.Field<string>("TOTAL").ToString();
//Columns table
cell = PhraseCell(new Phrase(Invoice_PDFColumn0.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn1.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn2.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn3.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn4.ToString(), GettypeStyle()));
table.AddCell(cell);
//cell = PhraseCell(new Phrase(Invoice_PDFColumn5.ToString(), GettypeStyle()));
//table.AddCell(cell);
//cell = PhraseCell(new Phrase(Invoice_PDFColumn6.ToString(), GettypeStyle()));
//table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn7.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn8.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn9.ToString(), GettypeStyle()));
table.AddCell(cell);
cell = PhraseCell(new Phrase(Invoice_PDFColumn10.ToString(), GettypeStyle()));
table.AddCell(cell);
}
ColumnText ct = new ColumnText(pdfStamper.GetOverContent(1));
ct.AddElement(table);
//iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(18, 370, 800, 36);
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(16, 320, 900, 16);
rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
rect.BorderWidth = 15;
rect.BorderColor = new BaseColor(0, 0, 0);
rect.Border = iTextSharp.text.Rectangle.BOX;
ct.SetSimpleColumn(rect);
ct.Go();
#endregion
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = true;
pdfStamper.FreeTextFlattening = true;
pdfStamper.Writer.CloseStream = true;
pdfStamper.Close();// close the pdf
}
catch (Exception ex)
{
// No errors (yet).
}
}
}
这是我得到的结果的一个例子:
我遇到的问题是,当我在附加的示例中以红色突出显示的信息更大(即超过 4 行)时,此信息被放置在 PDF 中,但它不会生成更多页面.
这是因为在打印“详细信息”表中的信息时,是在特定坐标处打印的。
如何使用 itextsharp 以便“使用我正在使用的模板” 1我可以正确生成 PDF 文件?
1在 Adobe Acrobat Pro 中创建的模板。
由于时间问题,在互联网上研究了一个星期后,我得出了这个解决方案,我将在这些步骤中描述:
如果有人对如何在正在构建的 PDF 文件中生成多个页面感兴趣,可以使用以下代码:
描述:在 PDF 文件中创建 8 页,A4 大小,横向:
这是我在 Stack Overflow 上找到的将 PDF 文件“或合并”成单个 PDF 文件的代码:
就我而言,我需要生成一个基本上是发票的 PDF 文件。
在那里,我将发票结果分成 10 条记录块(感谢这个答案),并为每块 10 条记录生成一个页面。
这是我为生成统一的 PDF 文件而调整的代码: