问题描述
我通过添加可容纳多个页面的单个PdfPTable来生成PDF。
现在我想为所有这些页面添加页眉和页脚,但它只显示在第一页上。
我正在重写类PdfPageEventHelper的OnStartPage / OnEndPage事件。
请提出建议最好的方式来合并页眉和页脚。
谢谢
Rule #1:不要使用 OnStartPage()
添加页眉或页脚。规则#2:不要使用 OnEndPage()将内容添加到作为参数传递给事件方法的
Document
对象中。改为使用 PdfWriter
的 DirectContent
。
规则#3:阅读并查看标有和(Java example)
...
回答你的问题:建议最好的方法来合并页眉和页脚。
你的问题关于边距不能正常工作 但是,说边距不能正常工作不是一个真正的问题。如果我告诉我的医生:我感觉不舒服,请帮助我!如果我不给他更多信息,我不能指望他帮助我。当我添加页眉和页脚时,页边距工作正常。如果它不适合你,你做错了......
对于你的指称,页眉和页脚是唯一的在第一页显示。从我们的观点来看,这是不正确的。如果您将事件添加到 PdfWriter
中,请执行以下操作:
PdfWriter作者= PdfWriter.getInstance(document,new FileOutputStream(filename));
标题事件=新的标题();
writer.setPageEvent(event);
然后调用 OnEndPage()
方法每次页面完成时。
I'm generating PDF by adding single PdfPTable which accommodates multiple pages.Now I want to add header and footer to all these pages but it is only displaying on first page. Also margins are not working correctly.
I am overriding OnStartPage/OnEndPage events of class PdfPageEventHelper.
Kindly suggest best way to incorporate header and footers.
Thanks
解决方案 Rule #1: don't use OnStartPage()
to add a header or a footer. Use the OnEndPage()
method only to add both the header and the footer.
Rule #2: don't add content to the Document
object passed as a parameter to the event method. Use the DirectContent
of the PdfWriter
instead.
Rule #3: read the documentation and look at the examples and Q&As marked header and footer
You'll notice that your question is a duplicate of:
- How to add HTML headers and footers to a page? (Java example)
- How to generate a report with dynamic header in PDF using itextsharp? (C# example)
- How to add text as a header or footer?
- ...
This answers your question: Kindly suggest best way to incorporate header and footers.
Your question about "margins not working correctly" is probably answered here: Why is my content overlapping with my footer? However, saying "margins are not working correctly" isn't an actual question. If I tell my doctor: "I don't feel well, please help me!" I can't expect him to help me if I don't give him more info. When I add headers and footers, the margins work correctly. If it doesn't work for you, you are doing it wrong...
The same is true for your allegation that the header and footer "is only displaying on first page." That's simply not true from our point of view. If you add the event to a PdfWriter
like this:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
Header event = new Header();
writer.setPageEvent(event);
Then the OnEndPage()
method is invoked every time a page is finalized.
这篇关于iTextSharp - 所有页面的页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!