使用iTextSharp的pdf中的页脚 [英] Footer in pdf with iTextSharp

查看:147
本文介绍了使用iTextSharp的pdf中的页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add a footer in every page of a PDF document. I want to have a name and the current date. I'm using this code, but it's only printing in the last page. I need it in every page. What am I missing here?

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

推荐答案

我设法解决此问题. 在创建pdf的班级中,我添加了这一行.

I manage to fix doing this. In my class that create the pdf i add this line.

pdfWriter.PageEvent = new PDFFooter();

然后我创建了另一个名为PDFFooter的类.

and i created another class called 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);
        }
    }
}

这篇关于使用iTextSharp的pdf中的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆