c# - 使用 iTextSharp 生成 pdf 页脚

标签 c# pdf itext

我正在尝试在 PDF 文档的每一页中添加页脚。我想要一个名字和当前日期。我正在使用这段代码,但它只在最后一页打印。我在每一页都需要它。我在这里缺少什么?

DateTime horario = DateTime.MinValue;
document.Add(new iText.Paragraph(document.BottomMargin, "TEST FOOTER" + horario));

最佳答案

我设法解决了这个问题。 在我创建 pdf 的类(class)中​​,我添加了这一行。

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);
        }
    }
}

关于c# - 使用 iTextSharp 生成 pdf 页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603619/

相关文章:

java - 文本 : Add an image on a header with an absolute position

c# - 动态设置PDF文件名

c# - 在编辑 WPF DataGrid 单元格期间显示弹出窗口

c - 如何创建一个函数来解密加密文件?

java - 如何阅读使用未知的随机所有者密码创建的 PDF?

java - iText7,无法设置 formField 值而不出现错误

java - 是什么让 PdfStamper 在 cleanup() 之后从 pdf 中删除图像,尽管它不应该?

c# - Watin DialogHandler 关闭 SaveFileDialog

c# - 使用 LINQ SQL 查明更新是否成功?

java - 为什么我不能用 iText 垂直打印这个字符串?