Footer in pdf with iTextSharp
本文关键字:iTextSharp with pdf in Footer | 更新日期: 2023-09-27 18:07:53
我想在PDF文档的每一页添加一个页脚。我想知道姓名和当前日期。我正在使用这个代码,但它只打印最后一页。我需要每一页都有。我遗漏了什么?
DateTime horario = DateTime.MinValue;
document.Add(new iText.Paragraph(document.BottomMargin, "TEST FOOTER" + horario));
我设法解决这个问题。在我创建pdf的课程中,我添加了这一行。
pdfWriter.PageEvent = new PDFFooter();
和我创建了另一个类PDFFooter
public class PDFFooter : PdfPageEventHelper
{
// write on top of document
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.SpacingAfter = 10F;
PdfPCell cell;
tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase(""));
cell.Border = Rectangle.NO_BORDER;
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
}
// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
}
// write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
DateTime horario = DateTime.Now;
base.OnEndPage(writer, document);
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
PdfPCell cell;
tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase("TEST"+" - " + horario));
cell.Border = Rectangle.NO_BORDER;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
}
//write on close of document
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
}