Header and Footer in ITextSharp

前端 未结 1 675
粉色の甜心
粉色の甜心 2021-01-19 09:47

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 tal

相关标签:
1条回答
  • 2021-01-19 09:53

    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.

    0 讨论(0)
提交回复
热议问题