1

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?

2
  • So you are doing do things, combining multiple PDFs and drawing text on them in the process, right? See my edit here. Basically, the easiest way is to just do two passes, the first combines and the second draws the text. stackoverflow.com/a/9845722/231316
    – Chris Haas
    Jun 5, 2012 at 19:31
  • Hey, thanks, I totally would do it that way, but unfortunately the document is stored in memory so it does not have a physical path after the first pass. It is just stored and kept in the memory stream. BTW you should answer the question instead of leaving a comment so I can give you some more points. Jun 11, 2012 at 19:01

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.