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. Image for the cookie policy date

How do I insert page numbers into an existing pdf?

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


5 Replies

AS Abirami Selvan Syncfusion Team January 14, 2015 07:35 AM UTC

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.



AN Andrew January 15, 2015 12:10 AM UTC


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);
               
            }


AS Abirami Selvan Syncfusion Team January 19, 2015 07:22 AM UTC

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.



AN Andrew February 5, 2015 01:08 AM UTC

Thanks for the response. This solution worked great!


PH Praveenkumar H Syncfusion Team February 6, 2015 03:43 AM UTC

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



Loader.
Live Chat Icon For mobile
Up arrow icon