Click here to Skip to main content
15,068,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I export my HTML page to pdf and add a footer to pdf by using "PdfPageEventHelper" but it adds the footer on every page, but I want to add footer on the last page only?

What I have tried:

code to convert html to pdf :
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);            
div1.RenderControl(h_textw);//Name of the Panel
Document doc = new Document();
doc = new Document(PageSize.A4, 50, 50, 50, 50);
FontFactory.GetFont("Times New Roman", 8);
StringReader s_tr = new StringReader(s_tw.ToString());
// HTMLWorker html_worker = new HTMLWorker(doc);
using (MemoryStream memoryStream = new MemoryStream())
    PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
    writer.PageEvent = new PDFFooter();
    XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, s_tr);
    byte[] bytes = memoryStream.GetBuffer();

code for event helper:
public class PDFFooter : PdfPageEventHelper

    PdfContentByte cb;
    PdfTemplate template;
    // we will put the final number of pages in a template

    // write on top of document
    /// <inheritdoc>
    public override void OnOpenDocument(PdfWriter writer, Document document)


    /// <inheritdoc>
    public override void OnCloseDocument(PdfWriter writer, Document document)
base.OnCloseDocument(writer, document);

          PdfPTable tab = new PdfPTable(1);
          PdfPCell cell = new PdfPCell(new Phrase(writer.PageNumber));
          cell.Border = 0;
          tab.TotalWidth = 300F;
          tab.WriteSelectedRows(0, -1, 300, 30, writer.DirectContent);

    // write on start of each page
    /// <inheritdoc>
    public override void OnStartPage(PdfWriter writer, Document document)

    /// <inheritdoc>
    public override void OnEndPage(PdfWriter writer, Document document)
        if (document.PageNumber == writer.PageNumber - 1)
            PdfContentByte cb = writer.DirectContent;
            ColumnText ct = new ColumnText(cb);
            cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 8.0f);
            cb.SetTextMatrix(document.LeftMargin, document.BottomMargin);
            // cb.ShowText("");
            cb.ShowText( "Name: " + username));


Updated 7-Oct-20 20:19pm

1 solution

Reference: How to add text as a header or footer?[^]

Difficult to go through all of your code. It would be easier for you. Use this reference and try something below. BTW, itext 7 uses Canvas, reference here[^].
class MyFooter : PdfPageEventHelper 
    Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);
    public override void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        Phrase header = new Phrase("this is a header", ffont);
        Phrase footer = new Phrase("this is a footer", ffont);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
                (document.right() - document.left()) / 2 + document.leftMargin(),
       + 10, 0);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
                (document.right() - document.left()) / 2 + document.leftMargin(),
                document.bottom() - 10, 0);

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setPageEvent(new MyFooter());
Member 14192879 8-Oct-20 19:36pm
I already implemented this code but it add-footer on all pages but I want to add only on the last page.
Sandeep Mewara 9-Oct-20 3:19am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900