Articles in this section
Category / Section

How to Add Section Based on Page Numbers in WinForms PDF Footer?

4 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 allows you to customize page numbering in the footer of a PDF document using C#.

Steps to add section-based page numbers in a PDF footer

  1. Create a new console application project.

ConsoleApp.png

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your console application from Nuget.org.

image.png

  1. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Diagnostics;
using System.Drawing; 
  1. Implement the following code in Program.cs to add section-wise page numbers in the PDF footer.

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

   // Create Section 1 (First Section: No Page Numbers)
   PdfSection section1 = document.Sections.Add();
   section1.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle0;
   section1.PageSettings.Size = PdfPageSize.A4;
   // Add more pages to Section 1
   for (int i = 1; i <= 3; i++)
   {
       PdfPage page = section1.Pages.Add();
       page.Graphics.DrawString($"Section 1 - Page {i}", trueTypeFont, PdfPens.Black, PdfBrushes.Black, new PointF(10, 10));
   }

   // Create Section 2 (Second Section: With Footer and Page Numbers)
   PdfSection section2 = document.Sections.Add();
   section2.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
   section2.PageSettings.Size = PdfPageSize.A4;
   // Add pages to Section 2 with page numbers
   for (int i = 1; i <= 3; i++)
   {
       PdfPage page = section2.Pages.Add();
       page.Graphics.DrawString($"Section 2 - Page {i}", trueTypeFont, PdfPens.Black, PdfBrushes.Black, new PointF(10, 10));
   }

   // Add footer with page numbers to Section 2
   AddFooter(section2, "@Copyright 2025");

   // Create Section 3 (Third Section: With Footer and Page Numbers)
   PdfSection section3 = document.Sections.Add();
   section3.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
   section3.PageSettings.Size = PdfPageSize.A3;

   // Add pages to Section 3 with page numbers
   for (int i = 1; i <= 3; i++)
    {
        PdfPage page = section3.Pages.Add();
        page.Graphics.DrawString($"Section 3 - Page {i}", trueTypeFont, PdfPens.Black, PdfBrushes.Black, new PointF(10, 10));
    }

    // Add footer with page numbers to Section 3
    AddFooter(section3, "@Copyright 2025");

   //Create file stream.
   using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the PDF document to file stream.
        document.Save(outputFileStream);
    }
    document.Close(true);
#region Helper Methods
/// <summary>
/// Add footer to the PDF document.
/// </summary>
/// <param name="section"></param>
/// <param name="footerText"></param>
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.
   PdfSectionPageNumberField pageNumber = new PdfSectionPageNumberField(font, brush);
   //Create page count field.
   PdfSectionPageCountField count = new PdfSectionPageCountField(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;
}
#endregion 

A complete working sample can be downloaded from GitHub.

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

Output.png

Take a moment to peruse the documentation for PDF Header Footer

Conclusion

I hope you enjoyed learning about how to add section based on page number in WinForms PDF Footer.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example 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  to leave a comment
Access denied
Access denied