Articles in this section
Category / Section

How to add page numbers starting from the second page of the PDF document?

5 mins read

The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.

With this library, you can add page numbers starting from the second page of the PDF document in C# and VB.NET.

Steps to add page numbers starting from the second page of the PDF document programmatically:

  1. Create a new C# console application project.
    Console-app-creation1.png
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
    NuGet_package.png
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
  1. Use the following code example to add page numbers started from the second page of the PDF document.

C#

//Create the PDF document
PdfDocument document = new PdfDocument();
//Initializing Font
Font font = new System.Drawing.Font("Times new roman", 36, FontStyle.Regular);
PdfFont trueTypeFont = new PdfTrueTypeFont(font, true);
//Adding new section for 1st page alone where the page number not to be added.
PdfSection section1 = document.Sections.Add();
PdfPage page = section1.Pages.Add();
//Drawing some text at first page. 
page.Graphics.DrawString("Hello World!!!", trueTypeFont, PdfPens.Black, PdfBrushes.Black, new PointF(10, 10));
//Adding new section for other pages to add page number.
PdfSection section2 = document.Sections.Add();
//Adding 3 pages with content in section2.
for (int i = 2; i <= 4; i++)
{
   //Adding a new page to PDF document.
   page = section2.Pages.Add();

   //Drawing some text at new page.
   page.Graphics.DrawString("Hello World!!!", trueTypeFont, PdfPens.Black, PdfBrushes.Black, new PointF(10, 10));
}
//Adding footer only to 2nd section.
AddFooter(section2, "@Copyright 2007");
//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer.
Process.Start("Sample.pdf");

Helper function to add footer in PDF document using C#:

private static void AddFooter(PdfSection section, string footerText)
{
   //Create bounds for the footer. 
   RectangleF rect = new RectangleF(0, 0, section.Pages[0].GetClientSize().Width, 50);
   //Create a page template that can be used as footer. 
   PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);
   //Set font and brush. 
   PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
   PdfPen pen = new PdfPen(Color.DarkBlue, 3f);
   PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);              
   //Set the string format for line alignment. 
   PdfStringFormat format = new PdfStringFormat();
   format.Alignment = PdfTextAlignment.Center;
   format.LineAlignment = PdfVerticalAlignment.Middle;   
   //Draw footer text in PDF page.
   footer.Graphics.DrawString(footerText, font, brush, new RectangleF(0, 18, footer.Width, footer.Height), format);
   format = new PdfStringFormat();
   format.Alignment = PdfTextAlignment.Right;
   format.LineAlignment = PdfVerticalAlignment.Bottom;
   //Create page number field.
   PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);
   //Create page count field.
   PdfPageCountField count = new PdfPageCountField(font, brush);
   //Add the fields in composite fields.
   PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);
   compositeField.Bounds = footer.Bounds;
   //Draw the composite field in footer.
   compositeField.Draw(footer.Graphics, new PointF(470, 40));
   //Add the footer template at the bottom
   section.Template.Bottom = footer;
}

VB.NET

'Create the PDF document. 
Dim document As PdfDocument = New PdfDocument()
'Initializing Font.
Dim font As Font = New System.Drawing.Font("Times new roman", 36, FontStyle.Regular)
Dim trueTypeFont As PdfFont = New PdfTrueTypeFont(font, True)
'Adding new section for 1st page alone where the page number not to be added.
Dim section1 As PdfSection = document.Sections.Add()
Dim page As PdfPage = section1.Pages.Add()
'Drawing some text at first page. 
page.Graphics.DrawString("Hello World!!!", trueTypeFont, PdfPens.Black, PdfBrushes.Black, New PointF(10, 10))
'Adding new section for other pages to add page number.
Dim section2 As PdfSection = document.Sections.Add()
'Adding 3 pages with content in section2.
For i As Integer = 2 To 4
   'Adding a new page to PDF document.
   page = section2.Pages.Add()
   'Drawing some text at new page.
   page.Graphics.DrawString("Hello World!!!", trueTypeFont, PdfPens.Black, PdfBrushes.Black, New PointF(10, 10))
Next
'Adding footer only to 2nd section.
AddFooter(section2, "@Copyright 2007")
'Save and close the document.
document.Save("Sample.pdf")
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer.
Process.Start("Sample.pdf")

Helper function to add footer in PDF document using VB.NET:

Private Sub AddFooter(ByVal section As PdfSection, ByVal footerText As String)
   'Create bounds for the footer. 
   Dim rect As RectangleF = New RectangleF(0, 0, section.Pages(0).GetClientSize().Width, 50)
   'Create a page template that can be used as footer. 
   Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(rect)
   'Set font and brush. 
   Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Gray)
   Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F)
   Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold)
   'Set the string format for line alignment. 
   Dim format As PdfStringFormat = New PdfStringFormat()
   format.Alignment = PdfTextAlignment.Center
   format.LineAlignment = PdfVerticalAlignment.Middle
   'Draw footer text in PDF page.
   footer.Graphics.DrawString(footerText, font, brush, New RectangleF(0, 18, footer.Width, footer.Height), format)
   format = New PdfStringFormat()
   format.Alignment = PdfTextAlignment.Right
   format.LineAlignment = PdfVerticalAlignment.Bottom
   'Create page number field.
   Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush)
   'Create page count field.
   Dim count As PdfPageCountField = New PdfPageCountField(font, brush)
   'Add the fields in composite fields.
   Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count)
   compositeField.Bounds = footer.Bounds
   'Draw the composite field in footer.
   compositeField.Draw(footer.Graphics, New PointF(470, 40))
   'Add the footer template at the bottom.
   section.Template.Bottom = footer
End Sub

You can download a complete working sample from PDF-footer-sample.zip.

Take a moment to peruse the documentation, where you can find drawing the headers and footers in a PDF file and other features like adding shapes to PDF, inserting image in a PDF, and drawing text to PDF with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to add headers and footers with images, text, and shapes in all pages of document.

Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Conclusion
I hope you enjoyed learning about how to add page numbers starting from the second page of the PDF document.

You can refer to our .NET PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied