如何在 C# 中使用 iTextsharp 5.2.0 版在 pdf 的所有页面中添加页眉和页脚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10193761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add header and footer in all page of pdf using iTextsharp version 5.2.0 in C#
提问by Mamtora
I am creating a pdf file using itextsharp. I want to add header and footer for each page in the pdf document. Can anyone tell me how can I do this?
我正在使用 itextsharp 创建一个 pdf 文件。我想为pdf文档中的每个页面添加页眉和页脚。谁能告诉我我该怎么做?
I am using itext 5.2.0 In this, I am unable to find the option to use HeadeFooter class, which is available in the earlier versions.
我使用的是 itext 5.2.0 在这里,我找不到使用 HeadeFooter 类的选项,该选项在早期版本中可用。
Thanks in advance..
提前致谢..
回答by ABH
For iTextSharp version 5+, Header/Footer property has been removed. Now this can be done by us PageEventHandlerclass. Though it's not strait forward now but the upside is that now you can add more than just plan text in header and footer. Please check this linkfor complete workout of header/footer and more in iTextSharp.
对于 iTextSharp 版本 5+,页眉/页脚属性已被删除。现在这可以由我们PageEventHandler班来完成。虽然现在还不是很严格,但好处是现在您可以添加更多内容,而不仅仅是在页眉和页脚中添加计划文本。请查看此链接以了解 iTextSharp 中页眉/页脚等的完整练习。
回答by G.S. Shekhawat
Please use this code.
请使用此代码。
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);
}
}
please check my blog for more details http://gopalkaroli.blogspot.in/2011/11/how-to-add-header-and-footer-on-pdf.html
请查看我的博客以获取更多详细信息 http://gopalkaroli.blogspot.in/2011/11/how-to-add-header-and-footer-on-pdf.html

