We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How do I insert page numbers into an existing pdf?

Thread ID:

Created:

Updated:

Platform:

Replies:

117958 Jan 13,2015 01:58 PM Feb 5,2015 10:43 PM Windows Forms 5
loading
Tags: PDF
Andrew
Asked On January 13, 2015 01:58 PM

Hello,

I'm new to syncfusion, so this might be something simple, but I just can't seem to find out how to do it.

Currently I'm able to load an existing pdf to a byte array (byte[] ba), and then put it in a PdfDocument object. From here I thought that I had to perhaps modify a template somehow to make the page numbers appear, but after an hour of looking at this I'm starting to think I'm heading in the wrong direction. Any help would be appreciated. Thanks in advance.

The code below is all I have so far.

//ba is a byte array containing an existing pdf
 Syncfusion.Pdf.Parsing.PdfLoadedDocument loadedDoc = new Syncfusion.Pdf.Parsing.PdfLoadedDocument(ba);
 PdfDocument document = new PdfDocument();
 document.Append(loadedDoc);


Abirami Selvan [Syncfusion]
Replied On January 14, 2015 02:35 AM

Hi Andrew ,

Thank you for using Syncfusion Products.

We can add the page number into Pdf using PdfPageNumberField. Please refer the following code snippet  to add the page number in existing pdf .

//Creates a new PDF document.

PdfDocument document = new PdfDocument();

//Append the loaded document

document.Append(loadedDoc);

//Sets the font.

PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);

//Creates a brush.

PdfBrush brush = PdfBrushes.Black;

//Creates a page number field.

PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

for (int i = 0; document.pages.Count; i++)

{

 

  //Draws the page number in the document.

  pageNumber.Draw(document.Pages[i].Graphics, new PointF(250,500));

 }

Please refer  the following link for more details

http://help.syncfusion.com/ug/js/default.htm#!documents/pagenumberfield.htm

Please try this and let us know if you have any further assistance.

 

Thanks,

Abirami.


Andrew
Replied On January 14, 2015 07:10 PM


Is it possible to get it in the following format: Page X of Y where X is the current page and Y is the total number of pages?

Please take a look below. I can't seem to get anything correctly drawn below document.Pages[i].Size.Height - 130. I've slowly reduced this value to document.Pages[i].Size.Height - 126 and at every decrement, a fraction of the bottom of the text I'm drawing goes away.

//ba is a byte array containing an existing pdf
 Syncfusion.Pdf.Parsing.PdfLoadedDocument loadedDoc = new Syncfusion.Pdf.Parsing.PdfLoadedDocument(ba);
 PdfDocument document = new PdfDocument();
 document.Append(loadedDoc);
 string pageNums = "";

Syncfusion.Pdf.Graphics.PdfBrush brush = Syncfusion.Pdf.Graphics.PdfBrushes.Red;
Syncfusion.Pdf.Graphics.PdfFont font = new Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Courier, 8);

//amount of space each character will take- courier (monospace font)
 float spacePerChar = (34 / (float)7.0);
//arbitrary space buffer
 int arbitraryBuffer = 10;
            for (int i = 0; i < document.Pages.Count; i++)
            {

                pageNums = "Page " + (i+1).ToString() + " of " + document.Pages.Count.ToString();
                System.Drawing.PointF startingPoint = new System.Drawing.PointF(document.Pages[i].Size.Width - (spacePerChar * pageNums.Length) - arbitraryBuffer, document.Pages[i].Size.Height - 126);
                document.Pages[i].Graphics.DrawString(pageNums, font, brush, startingPoint);
               
            }

Abirami Selvan [Syncfusion]
Replied On January 19, 2015 02:22 AM

Hi Andrew ,

 

We can draw the page number as  Page X(number) of Y (count) using the class  PdfCompositeField. Please refer the following code snippet :

 

//Adds the fields in composite fields.

PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

 

for (int i = 0; i < document.Pages.Count; i++)

{

              

//Draws the composite field.

compositeField.Draw(document.Pages[i].Graphics, new PointF (document.Pages[i].GetClientSize().Width/2-20 ,document.Pages[i].GetClientSize().Height-20));

 

}

We have attached the sample for your reference . Please refer the following link :

http://www.syncfusion.com/downloads/support/directtrac/general/PdfPageNumberSample1587927551.zip


Please try this and let us know  if you have any further assistance .

 

Thanks ,

Abirami.


Andrew
Replied On February 4, 2015 08:08 PM

Thanks for the response. This solution worked great!

Praveenkumar H [Syncfusion]
Replied On February 5, 2015 10:43 PM

Hi Andrew,
 
We are happy know that the last solution worked to you.
 
Please let us know if you need further assistance.
 
With Regards,
Praveen



CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;