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?
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: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.
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.
As already indicated by others in the comments, you need to create a
PdfPageEvent
implementation. The easiest way to do this, is by extending thePdfPageEventHelper
class.Important to know:
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.Document
object that is passed to the event. This object can be used for read-only purposes only.If you examine the
OnEndPage()
method in theMyHeaderFooterEvent
class, you see that we get theDirectContent
from the writer and that we add content to thiscanvas
usingShowTextAligned
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.I used a couple of hard-coded values: I used
10
as thex
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 used810
for they
value of the header because you're creating an A4 page with a top margin of 42. The topy
coordinate of your page is 842. The topy
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 bottomy
coordinate of the page is 0 in your case and the bottomy
coordinate of the margin in 35. I used10
for the base line of the footer.You created
wri
and right after creating thisPdfWriter
instance, you open theDocument
instance. For the page event to take effect, you should add the following link, right before opening theDocument
:Now the
OnEndPage()
method will be invoked every time your main process finalizes a page.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.