Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Page number in Footer in PDF by Itextsharp

I am using iTextSharp DLL in ASP.NET code. I am fetching a data into dataset and Add the dataset into PDF table.

If my dataset have more 100 rows then 100 rows will be added into PDF table and 3 or 4 page will be created in PDF file.

How can I add the footer in each page?

like image 281
himanshu soni Avatar asked May 09 '12 11:05

himanshu soni


People also ask

How to add header and footer in PDF using iText in Java?

This page will provide the tutorial for how to add header and footer in every page of PDF using iText in java. iText provides PdfPageEventHelper class that has different event methods that is called while writing PDF. To add header and footer we need to create a class extending PdfPageEventHelper and override onStartPage and onEndPage methods.

How to add page numbers to a PDF file using iTextSharp?

using iTextSharp.text.pdf; The PDF file to which the page numbers needs to be added is read into a Byte array from the disk. Then the byte array is read into a PdfReader object and a loop is executed over the pages of the PDF and the page number is added to each page using the PdfStamper class. Finally the MemoryStream is saved to disk as PDF file.

How to add header and footer in pdfpageeventhelper?

To add header and footer we need to create a class extending PdfPageEventHelper and override onStartPage and onEndPage methods. Register this class with PdfWriter. Find the complete explanation as below. PdfPageEventHelper class has different event methods for PDF.

How to add page numbers to a PDF file?

The PDF file to which the page numbers needs to be added is read into a Byte array from the disk. Then the byte array is read into a PdfReader object and a loop is executed over the pages of the PDF and the page number is added to each page using the PdfStamper class. Finally the MemoryStream is saved to disk as PDF file. Need Help?


3 Answers

You need to inherit from PdfPageEventHelper class and override the methods shown in the code snippet below:

Document doc = new Document(PageSize.A4.Rotate());        

using (MemoryStream ms = new MemoryStream())
{
  PdfWriter writer = PdfWriter.GetInstance(doc, ms);
  PageEventHelper pageEventHelper = new PageEventHelper();
  writer.PageEvent = pageEventHelper;
}

 public class PageEventHelper : PdfPageEventHelper
    {
        PdfContentByte cb;
        PdfTemplate template;


        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            cb = writer.DirectContent;
            template = cb.CreateTemplate(50, 50);
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            int pageN = writer.PageNumber;
            String text = "Page " + pageN.ToString() + " of ";
            float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

            iTextSharp.text.Rectangle pageSize = document.PageSize;

            cb.SetRGBColorFill(100, 100, 100);

            cb.BeginText();
            cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
            cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
            cb.ShowText(text);

            cb.EndText();

            cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
        }

        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            base.OnCloseDocument(writer, document);

            template.BeginText();
            template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
            template.SetTextMatrix(0, 0);
            template.ShowText("" + (writer.PageNumber - 1));
            template.EndText();
        }
    }
like image 186
bleeeah Avatar answered Oct 31 '22 13:10

bleeeah


You must inherit pageeventhepler to add page number to dynamically generated pdf pages.

public void printPdf()
{
    Document doc = new Document(PageSize.A4.Rotate());
    using (MemoryStream ms = new MemoryStream())
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        PageEventHelper pageEventHelper = new PageEventHelper();
        writer.PageEvent = pageEventHelper;
    }
}

public class PageEventHelper : PdfPageEventHelper
{
    PdfContentByte cb;
    PdfTemplate template;


    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        cb = writer.DirectContent;
        template = cb.CreateTemplate(50, 50);
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);

        int pageN = writer.PageNumber;
        String text = "Page " + pageN.ToString() + " of ";
        float len = this.RunDateFont.BaseFont.GetWidthPoint(text, this.RunDateFont.Size);

        iTextSharp.text.Rectangle pageSize = document.PageSize;

        cb.SetRGBColorFill(100, 100, 100);

        cb.BeginText();
        cb.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
        cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin));
        cb.ShowText(text);

        cb.EndText();

        cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin));
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);

        template.BeginText();
        template.SetFontAndSize(this.RunDateFont.BaseFont, this.RunDateFont.Size);
        template.SetTextMatrix(0, 0);
        template.ShowText("" + (writer.PageNumber - 1));
        template.EndText();
    }
}
like image 28
maulik sakhare Avatar answered Oct 31 '22 14:10

maulik sakhare


If you just need a simple page number, then this does the job:

public class PageHeaderFooter : PdfPageEventHelper
{
    private readonly Font _pageNumberFont = new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.BLACK);

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        AddPageNumber(writer, document);
    }

    private void AddPageNumber(PdfWriter writer, Document document)
    {
        var text = writer.PageNumber.ToString();

        var numberTable = new PdfPTable(1);
        var numberCell = new BorderlessPdfPCell(new Phrase(text, _pageNumberFont)) {HorizontalAlignment = Element.ALIGN_RIGHT};

        numberTable.AddCell(numberCell);
        numberTable.TotalWidth = 50;
        numberTable.WriteSelectedRows(0, -1, document.Right - 80, document.Bottom + 20, writer.DirectContent);
    }
}

And you use it this way:

var stream = new MemoryStream();
var document = new Document(PageSize.A4, 20, 20, 30, 30);

var pdfWriter = PdfWriter.GetInstance(document, stream);
pdfWriter.PageEvent = new PageHeaderFooter();
// rest of PDF generation
like image 27
Episodex Avatar answered Oct 31 '22 15:10

Episodex