How to add header and footer to dynamic pdf using iTextLibrary?

I created a PDF file dynamically using the iText library. Now I want to add the Header and Footer in the PDF pages, for this I added this code:

document.addHeader("My Header Title", "My Header Details"); 

But on my pdf pages this header cannot be set. What a problem that I donโ€™t know about, if you have any idea related to it, share your thoughts.

+4
source share
4 answers

If you are using the current version of iText (i.e. 5.4.x at the moment), look at the MovieHistory2 sample from iText in action - the second edition , which shows how to add headings (different for odd and even pages) to PDF when it creating.

Most important is the implementation of PdfPageEventHelper

 /** Inner class to add a header and a footer. */ class HeaderFooter extends PdfPageEventHelper { /** Alternating phrase for the header. */ Phrase[] header = new Phrase[2]; /** Current page number (will be reset for every chapter). */ int pagenumber; /** * Initialize one of the headers. * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onOpenDocument(PdfWriter writer, Document document) { header[0] = new Phrase("Movie history"); } /** * Initialize one of the headers, based on the chapter title; * reset the page number. * @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float, * com.itextpdf.text.Paragraph) */ public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { header[1] = new Phrase(title.getContent()); pagenumber = 1; } /** * Increase the page number. * @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onStartPage(PdfWriter writer, Document document) { pagenumber++; } /** * Adds the header and the footer. * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); switch(writer.getPageNumber() % 2) { case 0: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0); break; case 1: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(), rect.getTop(), 0); break; } ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); } } 

which is registered as follows:

  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); HeaderFooter event = new HeaderFooter(); writer.setBoxSize("art", new Rectangle(36, 54, 559, 788)); writer.setPageEvent(event); 

EDIT: As requested in the comments, a simpler version of the onEndPage method with a static header instead of alternating:

  public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("My static header text"), rect.getRight(), rect.getTop(), 0); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); } 
+10
source
+2
source

Please refer to the accepted answer to this question first.
This answer is very helpful (and it helped me).
Just in case, if you are programming in C #, here is the ONLY accepted answer, but in the C # version

 /// <summary> /// Inner class to add a header and a footer. /// </summary> internal class HeaderFooter : PdfPageEventHelper { private Phrase[] header = new Phrase[2]; private int pageNumber; public override void OnOpenDocument(PdfWriter writer, Document document) { header[0] = new Phrase("Smares in Header"); } public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { header[1] = new Phrase(title.Content); pageNumber = 1; } public override void OnStartPage(PdfWriter writer, Document document) { pageNumber++; } public override void OnEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.GetBoxSize("art"); switch (writer.PageNumber % 2) { case 0: ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_RIGHT, header[0], rect.Right, rect.Top, 0); break; case 1: ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_LEFT, header[1], rect.Left, rect.Top, 0); break; } ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(String.Format("page {0}", pageNumber)), (rect.Left + rect.Right) / 2, rect.Bottom - 18, 0); } } 

and event registration will be:

 using (MemoryStream ms = new MemoryStream()) { using (Document doc = new Document(PageSize.A4, -30, -30, 45, 45)) { using (PdfWriter writer = PdfWriter.GetInstance(doc, ms)) { HeaderFooter ev = new HeaderFooter(); writer.SetBoxSize("art", new Rectangle(36, 54, 559, 788)); writer.PageEvent = ev; // continue your code here } } } 

NOTE. This is just converting the accepted answer from java to C #. but you can customize it to suit your needs, as well as with it.

+1
source

You can add like

 HeaderFooter header = new HeaderFooter(new Phrase("Add Header Part Here"), false); HeaderFooter footer = new HeaderFooter(new Phrase("Add Footer Here"), new Phrase(".")); document.setHeader(header); document.setFooter(footer); 
-3
source

Source: https://habr.com/ru/post/1487297/


All Articles