Articles in this section
Category / Section

How to Update Page Numbers in a Merged 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.
You can use this library to merge multiple PDF documents and update the page numbers in the merged PDF.

Steps to update the page numbers in the merged PDF document using C# and VB.NET:

  1. Create a new C# console application project.

Create .NET Framework console application

2. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Standard applications from NuGet.org.

NuGet package installation

3. Include the following namespaces in Program.cs file.
C#

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

VB.NET

Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Redaction
Imports Syncfusion.Pdf.Parsing
  1. Use the following code example to update the page numbers in a merged PDF document.
    C#
//Creates a PDF document.
           PdfDocument finalPDFDocument = new PdfDocument();
           //Creates a string array of source files to be merged.
           string[] source = { "../../Data/File1.pdf", "../../Data/File2.pdf" };
           //Merges PDFDocument.
           PdfDocument.Merge(finalPDFDocument, source);

           //Save the PDF document to memory stream.
           MemoryStream stream = new MemoryStream();
           finalPDFDocument.Save(stream);
           //Close the document.
           finalPDFDocument.Close(true);

           //Load the PDF document from memory stream.
           PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);

           //Set the font.
           PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);
           //Create page number field.
           PdfPageNumberField pageNumber = new PdfPageNumberField(font, PdfBrushes.Black);
           //Create page count field.
           PdfPageCountField count = new PdfPageCountField(font, PdfBrushes.Black);
           //Add the fields in composite fields.
           PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.DarkGray, "Page {0} of {1}", pageNumber, count);

           for (int i = 0; i < loadedDocument.Pages.Count; i++)
           {
               //Get the existing pages from the document.
               PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage;

               //Create PDF redaction for the page.
               PdfRedaction redaction = new PdfRedaction(new RectangleF(270, 800, 100, 20), Color.White);
               //Adds redaction to the loaded page.
               loadedPage.Redactions.Add(redaction);
               //Draw the composite field.
               compositeField.Draw(loadedDocument.Pages[i].Graphics, new PointF(loadedDocument.Pages[i].Size.Width / 2 - 20, loadedDocument.Pages[i].Size.Height - 20));

           }

           //Save the PDF document.
           loadedDocument.Save("../../Output.pdf");
           //Close the document. 
           loadedDocument.Close(true);

VB.NET

' Creates a PDF document.
Dim finalPDFDocument As New PdfDocument()
' Creates a string array of source files to be merged.
Dim source() As String = {"../../Data/File1.pdf", "../../Data/File2.pdf"}
' Merges PDFDocument.
PdfDocument.Merge(finalPDFDocument, source)

' Save the PDF document to memory stream.
Dim stream As New MemoryStream()
finalPDFDocument.Save(stream)
' Close the document.
finalPDFDocument.Close(True)

' Load the PDF document from memory stream.
Dim loadedDocument As New PdfLoadedDocument(stream)

' Set the font.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12F)
' Create page number field.
Dim pageNumber As New PdfPageNumberField(font, PdfBrushes.Black)
' Create page count field.
Dim count As New PdfPageCountField(font, PdfBrushes.Black)
' Add the fields in composite fields.
Dim compositeField As New PdfCompositeField(font, PdfBrushes.DarkGray, "Page {0} of {1}", pageNumber, count)

For i As Integer = 0 To loadedDocument.Pages.Count - 1
   ' Get the existing pages from the document.
   Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage)

   ' Create PDF redaction for the page.
   Dim redaction As New PdfRedaction(New RectangleF(270, 800, 100, 20), Color.White)
   ' Adds redaction to the loaded page.
   loadedPage.Redactions.Add(redaction)
   ' Draw the composite field.
   compositeField.Draw(loadedDocument.Pages(i).Graphics, New PointF(loadedDocument.Pages(i).Size.Width / 2 - 20, loadedDocument.Pages(i).Size.Height - 20))
Next

' Save the PDF document.
loadedDocument.Save("../../Output.pdf")
' Close the document. 
loadedDocument.Close(True)

A complete work sample can be downloaded from the Update-page-numbers-in-merged-PDF-file.

By executing the program, you will get the PDF document as follows.

Output PDF screenshot

Take a moment to peruse the documentation, where you will find the options like add header and footer to the PDF document, merge multiple PDF documents, Importing pages from multiple PDF documents, etc. with code examples.

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

An online sample link to add header and footer to the PDF document and merge multiple PDF documents.

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

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