Question

I am adding page numbers to the bottom of a pdf document using ITextSharp The thing is, it is made up of 4 or 5 different PDF's that are combined through the process, and there are some dynamically added pages so the PDF can vary in size. I cannot call document.PageCount because it "lacks the get accessor".

I made a counter to keep track of what page I am on, and can get the final page to say "Page 9 of 9" but the rest only say "Page 7 of ". I was thinking that I could run the whole document through a PdfReader to call reader.NumberOfPages and rerun each page and add the final number at the bottom but I do not know how to make one from a type iTextSharp.text.Document, nor how to go about it afterwards.

This is a project started by someone else, and I've barely touched iTextSharp before, I cannot remake class because it is too huge.

Does anyone know what I can do? Or how to go about adding that 1 number to each page of an iTextSharp.text.Document?

Was it helpful?

Solution

Found it guys :D

So whenever you make your writer:

using (MemoryStream MS = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, MS);
            writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
            PdfFooter eventHandler = new PdfFooter();
            writer.PageEvent = eventHandler;

ect.
}

For the PdfFooter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;

public class PdfFooter : PdfPageEventHelper
{
    // This is the contentbyte object of the writer
    PdfContentByte cb;

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

// this is the BaseFont we are going to use for the header / footer
BaseFont bf = null;

// This keeps track of the creation time
DateTime PrintTime = DateTime.Now;

#region Properties
private string _Title;
public string Title
{
    get { return _Title; }
    set { _Title = value; }
}

private string _HeaderLeft;
public string HeaderLeft
{
    get { return _HeaderLeft; }
    set { _HeaderLeft = value; }
}

private string _HeaderRight;
public string HeaderRight
{
    get { return _HeaderRight; }
    set { _HeaderRight = value; }
}

private Font _HeaderFont;
public Font HeaderFont
{
    get { return _HeaderFont; }
    set { _HeaderFont = value; }
}

private Font _FooterFont;
public Font FooterFont
{
    get { return _FooterFont; }
    set { _FooterFont = value; }
}
#endregion

// we override the onOpenDocument method
public override void OnOpenDocument(PdfWriter writer, Document document)
{
    try
    {
        PrintTime = DateTime.Now;
        bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb = writer.DirectContent;
        template = cb.CreateTemplate(50, 50);
    }
    catch (DocumentException de)
    {
    }
    catch (System.IO.IOException ioe)
    {
    }
}

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

    int pageN = writer.PageNumber;
    String text = "Page " + pageN + " of ";
    float len = bf.GetWidthPoint(text, 8);

    Rectangle pageSize = document.PageSize;

    cb.SetRGBColorFill(100, 100, 100);

    cb.BeginText();
    cb.SetFontAndSize(bf, 8);
    cb.SetTextMatrix(pageSize.GetRight(70), pageSize.GetBottom(15));
    cb.ShowText(text);
    cb.EndText();

    cb.AddTemplate(template, pageSize.GetRight(70) + len, pageSize.GetBottom(15));
}

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

    template.BeginText();
    template.SetFontAndSize(bf, 8);
    template.SetTextMatrix(0, 0);
    template.ShowText("" + (writer.PageNumber - 1));
    template.EndText();
}

}

Hope this helps someone in the future :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top