How to add Header and Footer in dynamic pdf using iTextLibrary?

how to add header and footer in pdf using itext java example
itext add footer to existing pdf
how to add image in pdf header using itext
itext table header on every page
how to add header and footer in existing pdf using itextsharp
how to add title in pdf using itext
itext pdf format
itext pdf chart

I have created a PDF file dynamically using iText Library, Now I want to add Header and Footer in PDF's pages, for this one I have added given code:

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

But in my PDF's pages this header couldn't set. What is issue that I don't know, If you have any idea related to it,please share your thoughts.

In case you use a current iText version (i.e. 5.4.x as of now) have a look at the sample MovieHistory2 from iText in Action — 2nd Edition which shows how to add headers (different ones for odd and even pages) to a PDF while creating it.

Most essential is a PdfPageEventHelper implementation

/** 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 like this:

    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 variant of the onEndPage method with a static header instead of the alternating one:

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

Page events for headers and footers, How to generate a report with dynamic header in PDF using itextsharp? How to add HTML headers and footers to a page? 2-rows-in-pdf-footer-using-itext */ package com.itextpdf.samples.sandbox.events; import com.itextpdf.kernel.colors. I have created a PDF file dynamically using iText Library, Now I want to add Header and Footer in PDF's pages, for this one I have added given code: document.addHeader("My Header Title", "My Header Details"); But in my PDF's pages this header couldn't set. What is issue that I don't know, If you have any idea related to it,please share your

please refer this site.......

https://www.coderanch.com/how-to/java/ItextExample

Book page : How to add text as a header or footer?, Book page : I'm creating a pdf with iText 5 and want to add a footer. I did everything like the book "iText in action" says. There are no errors but the footer doesn't  We can add the header and footer to every page inside the PDF document. onCloseDocument is called before the document’ll be closed. This is the event where we inject the total number of pages in to the previously created template.

Please first refer to the accepted answer of this question. That answer is very helpful (and It helped me to). Just in case you are programming in C#, here is the SAME accepted answer but in 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 the registration of the event 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 a conversion of the accepted answer from java to C#. but you can customize this according to your needs, as I did with it.

iText 5-legacy : Page events for headers and footers, How to generate a report with dynamic header in PDF using itextsharp? How to add a table as a How to add HTML headers and footers to a page? TextFooter.​java com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf. In this core java tutorial we will learn How To Set Header and Footer in pdf in java using Itext Example using iText library - core java tutorial with program and examples. We will generate PDF files in java using iText library.

You can add like that

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

How to generate a report with dynamic header in PDF using iText 7?, Book page : I'm generating a PDF report with iTextSharp, the header of the How do I generate a header dynamically for each client? FACTORS OF %s", i​)); if (300 != i) { doc.add(new AreaBreak()); } Technical support page events header footer Try our iText 7 Library and add-ons FREE for 30 days. I am creating PDF with 3 pages, all 3 pages should have 3 different headers. I came across one example in documentation VaraibleHeader. However its not working below is my code.

How to generate a report with dynamic header in PDF using , as.a.resource? Up · Legal questions ›. Tags. Technical support page events header footer iText 5  Add Header and Footer to PDF Use event class to add header and footer. Using PdfWriter.setPageEvent , we can register our event class. Create a rectangle and set it to PdfWriter using PdfWriter.setBoxSize() method. In the example we are creating two pages. The event will automatically be called to add header and footer. CreateHeaderFooter.java

Adding Header and Footer in PDF using iText in Java , Adding Header, Footer and Total Pages. The com.itextpdf.text.pdf.​PdfPageEventHelper contains all the events that occur when iText is writing a  How to add header and footer in pdf using adobe Acrobat Live Cycle - Duration: 2:39. PDFEditing 4,041 views. 2:39. Adobe Acrobat XI Tutorial

How To Set Header and Footer in pdf in java using Itext Example, PDF files can be generated dynamically in java using iText library. Steps to Set Header and Footer in pdf in java using Itext >. Create class  Open the PDF file containing header and footer. Then choose Tools > Edit PDF > Header & Footer > Remove. To remove headers and footers from multiple PDFs, close any open documents and choose Tools > Edit PDF > Header & Footer > Remove. In the dialog box, click Add Files, choose Add Files, and select the files.

Comments
  • While you may have experienced that issue on an android device, your question contains nothing specific to android, neither does the accepted answer. Thus, there is no need for the android tag.
  • Thanks for you answer, i added page no as a page footer. now i want to add header in my page. If u have idea, than pls help me. Thanks...
  • The iText sample code in my answer already shows how to set both headers and footers. It shows in turns a hard coded document title ("Movie history") and the current chapter title. If you don't use Chapter in your document, you may want to boil down that switch statement to a single ColumnText.showTextAligned.
  • I just edited the answer to also contain a simpler implementation of onEndPage with a static header instead of the alternating one. The code was there before, too, it merely was more flexible
  • I have added your updated code and finally i got Header. Thanks for you communication.....Thanks a lot.
  • How to change header and footer for every page
  • Please don't dump a link because links may die. Summarize the relevant part of that page and post it here.