页眉和页脚中iTextSharp的 [英] Header and Footer in ITextSharp
问题描述
我知道这个问题已经被问了一千次,但我还没有找到一个简单的答案。我是比较新的iTextSharp的,所以请解释一下,如果你在谈论到一个蹒跚学步。 ?我如何添加一个简单的纯文本的页眉和页脚到我创建的文档
I know this question has been asked a thousand times, but I am yet to find a straight forward answer. I am relatively new to ITextSharp, so please explain as if you were talking to a toddler. How can I add a simple text only header and footer to the document I am creating?
我创建用下面的代码一个简单的PDF文档:
I am creating a simple pdf document with the following code:
void Button1Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(@"File Path", FileMode.Create));
doc.Open();
Paragraph par1 = new Paragraph("Hello World!");
doc.Add(par1);
//Code to add header/footer
doc.Close();
MessageBox.Show("Your PDF has been created!");
}
我已经做了很多关于如何添加页眉和页脚的研究,但它们都具有复杂的页面事件做。是否有更简单的方法?如果没有,你可以走我走过一步的工艺步骤?我会很感激的任何帮助,你都可以给。 !谢谢
I have done a lot of research on how to add headers and footers, but they all have to do with complicated page events. Is there an easier way? If there isn't, can you walk me through the process step by step? I would really appreciate any help that you all can give. Thanks!
推荐答案
您正在创建你的文件
是这样的:
You are creating your Document
like this:
Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 42, 35);
这意味着你有42个用户单位上边距和35个用户单位底部边缘。您可以使用此边距来添加一个页面事件额外的内容
This means that you have a top margin of 42 user units and a bottom margin of 35 user units. You can use this margin to add extra content in a page event.
官方网站上有大量的例子和一个全面Q&安培; A部分。所有示例和答案的标签。如果你点击头标记,你可以找到大量的例子。
The official web site has plenty of examples and a comprehensive Q&A section. All examples and answers are tagged. If you click on the header tag, you can find plenty of examples.
由于已被别人的意见所指出的,你需要创建一个 PdfPageEvent
的实施。要做到这一点最简单的方法,就是通过扩展 PdfPageEventHelper
类。
As already indicated by others in the comments, you need to create a PdfPageEvent
implementation. The easiest way to do this, is by extending the PdfPageEventHelper
class.
class MyHeaderFooterEvent : PdfPageEventHelper {
Font FONT = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
public override void OnEndPage(PdfWriter writer, Document document) {
PdfContentByte canvas = writer.DirectContent;
ColumnText.ShowTextAligned(
canvas, Element.ALIGN_LEFT,
new Phrase("Header", FONT), 10, 810, 0
);
ColumnText.ShowTextAligned(
canvas, Element.ALIGN_LEFT,
new Phrase("Footer", FONT), 10, 10, 0
);
}
}
重要须知:
Important to know:
- 禁止添加在
的OnStartPage()
事件的内容。加入你的头,当刚iTextSharp的移动到一个新的页面之前的所有内容已经添加到页面页脚。更具体地讲:添加在的OnEndPage()
事件的内容 - 禁止添加内容到
传递到事件文件
对象。这个对象可以只用于只读目的。
- It is forbidden to add content in the
OnStartPage()
event. Add your header and footer when all the content has been added to the page just before iTextSharp moves to a new page. More specifically: add content in theOnEndPage()
event. - It is forbidden to add content to the
Document
object that is passed to the event. This object can be used for read-only purposes only.
如果您检查的OnEndPage()
方法在 MyHeaderFooterEvent
类,你看,我们得到了 DirectContent
从作家和我们添加内容使用 ShowTextAligned
该方法画布
。还有许多其他的方式来添加内容,但明确要求的最简单方法。这种方法有其局限性,但它很容易
If you examine the OnEndPage()
method in the MyHeaderFooterEvent
class, you see that we get the DirectContent
from the writer and that we add content to this canvas
using ShowTextAligned
methods. There are many other ways to add content, but you explicitly asked for the easiest way. This way has its limitations, but it's easy.
我用了几个硬编码值的:我用 10
页眉和页脚的 X
值。那是因为你定义10个用户单位左边距。页眉和页脚只剩下你要添加到网页的实际内容相一致。我用 810
为是
头的价值,因为你要创建具有42顶边距一张A4纸。您的页面顶部是
坐标为842顶部是
上边距坐标为842 - 42 = 800.我加10个用户单位,使你的头是不是粘在实际内容。底部是
页面坐标是你的情况0和底是
35.在我的保证金坐标使用 10
页脚的底线。
I used a couple of hard-coded values: I used 10
as the x
value for the header and the footer. That's because you defined a left margin of 10 user units. The header and footer are left aligned with the actual content you're adding to the page. I used 810
for the y
value of the header because you're creating an A4 page with a top margin of 42. The top y
coordinate of your page is 842. The top y
coordinate of the top margin is 842 - 42 = 800. I added 10 user units so that your header isn't glued to the actual content. The bottom y
coordinate of the page is 0 in your case and the bottom y
coordinate of the margin in 35. I used 10
for the base line of the footer.
您创建的 WRI
并创建该 PdfWriter
实例之后,打开文件
实例。对于页面事件生效,您应该添加链接,打开文件
的前面:
You created wri
and right after creating this PdfWriter
instance, you open the Document
instance. For the page event to take effect, you should add the following link, right before opening the Document
:
wri.PageEvent = new MyHeaderFooterEvent();
现在的的OnEndPage()
方法将。每次调用您的主要工艺定型页面时
Now the OnEndPage()
method will be invoked every time your main process finalizes a page.
重要:您添加 //代码添加页眉/页脚
在错误的地方。 iTextSharp的将尝试尽快刷新页面的内容。如果您添加的代码添加页眉/页脚的之后的你添加的内容,你不能回去页眉和页脚添加到已经刷新到输出流页面。
Important: you added //Code to add header/footer
at the wrong place. iTextSharp will try to flush the content of a page as soon as possible. If you add code to add a header/footer after you've added content, you can not go back to add a header and footer to pages that were already flushed to the output stream.
这篇关于页眉和页脚中iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!