Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:16:13+00:00 2026-06-05T20:16:13+00:00

I am adding page numbers to the bottom of a pdf document using ITextSharp

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:16:14+00:00Added an answer on June 5, 2026 at 8:16 pm

    Found it guys 😀

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am adding collection of radio buttons to my page using jQuery below $(document).ready(function()
After adding elements to page using something like this, $('#blah').append('<div id=bugsbunny></div>'); how can I
I have a .aspx page that loads three separate .ascx controls to represent adding,
I'm adding css-based tab navigation to a site that is still using table-based layout.
I'm having trouble adding page numbers to PDFs. Here's how I'm inserting pages /
Hi I am writng some pdf by parsing html to it using itextsharp and
I have a small database and have been adding entries through a Rails page.
I am running Backbone js 0.9.2 on Rails 3.2.2,I have a page for adding
each day im adding around 50 links + to an intranet page , is
I am adding a user control dynamically on a page, the user control has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.