如何使用C#中的iTextsharp版本5.2.0在pdf的所有页面中添加页眉和页脚

问题描述 投票:2回答:2

我正在使用itextsharp创建一个pdf文件。我想在pdf文档中为每个页面添加页眉和页脚。谁能告诉我怎么能这样做?

我正在使用itext 5.2.0在此,我无法找到使用HeadeFooter类的选项,该类在早期版本中可用。

提前致谢..

c# asp.net pdf-generation itextsharp
2个回答
7
投票

请使用此代码。

public partial class Footer : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document doc)
    {
       Paragraph footer= new Paragraph("THANK YOU", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
       footer.Alignment = Element.ALIGN_RIGHT;
       PdfPTable footerTbl = new PdfPTable(1);
       footerTbl.TotalWidth = 300;
       footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

       PdfPCell cell = new PdfPCell(footer);
       cell.Border = 0;
       cell.PaddingLeft = 10;

       footerTbl.AddCell(cell);
       footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
    }
}

请查看我的博客,了解更多详情http://gopalkaroli.blogspot.in/2011/11/how-to-add-header-and-footer-on-pdf.html

https://gopalkaroli.wordpress.com/2011/11/12/how-to-add-header-and-footer-on-pdf-file-using-itextsharp-5-1/


-1
投票

对于iTextSharp版本5+,页眉/页脚属性已被删除。现在这可以由我们PageEventHandler类完成。虽然它现在不是向前发展,但好处是现在你可以在页眉和页脚中添加更多的计划文本。 Please check this link可以在iTextSharp中完成页眉/页脚的完整训练。

© www.soinside.com 2019 - 2024. All rights reserved.