Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header and Footer in ITextSharp

Tags:

c#

itext

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!

like image 625
1Programming1 Avatar asked Mar 14 '23 18:03

1Programming1


1 Answers

You are creating your Document like this:

Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 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.

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 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:

  • 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 the OnEndPage() 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.

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.

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.

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();

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.

like image 90
Bruno Lowagie Avatar answered Mar 26 '23 23:03

Bruno Lowagie