Articles in this section
Category / Section

How to add the starting page number from 50 in the footer of the PDF document using C# and VB.NET

7 mins read

The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit the PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure the PDF files. Using this library, you can add the starting page number from 50 in the footer of the PDF document.

Steps to add the starting page number from 50 in the footer of the PDF document using C# programmatically

1. Create a new C# console application project.

Create a console application

2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.

Install a NuGet packages

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

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics

 

4. The following code example shows how to add the starting page number from 50 in the footer of the PDF document using C#.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
 
//Add a pages to the PDF document
for (int j = 0; j < 50; j++)
{
pdfDocument.Pages.Add();
}
 
//Include header
pdfDocument.Template.Top = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo");
 
//Set the first page number as 50
int offset = 50;
 
//Add footer
AddFooter(pdfDocument, offset);
 
//Save and closes the document
pdfDocument.Save(@"../../Sample.pdf");
pdfDocument.Close(true);

 

public static PdfPageTemplateElement AddHeader(PdfDocument doc, string title, string description)
{
RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
 
//Create a page template
PdfPageTemplateElement header = new PdfPageTemplateElement(rect);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 24);
float doubleHeight = font.Height * 2;
Color activeColor = Color.FromArgb(44, 71, 120);
PdfSolidBrush brush = new PdfSolidBrush(activeColor);
 
PdfPen pen = new PdfPen(Color.DarkBlue, 3f);
font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);
 
//Set formattings for the text
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Middle;
 
//Draw title
header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format);
brush = new PdfSolidBrush(Color.Gray);
font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);
 
format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Left;
format.LineAlignment = PdfVerticalAlignment.Bottom;
 
//Draw description
header.Graphics.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format);
 
//Draw some lines in the header
pen = new PdfPen(Color.DarkBlue, 0.7f);
header.Graphics.DrawLine(pen, 0, 0, header.Width, 0);
pen = new PdfPen(Color.DarkBlue, 2f);
header.Graphics.DrawLine(pen, 0, 03, header.Width + 3, 03);
pen = new PdfPen(Color.DarkBlue, 2f);
header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3);
header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height);
 
return header;
}

 

public static void AddFooter(PdfDocument doc, int offset)
{
//Set the font and brush
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7);
PdfBrush brush = new PdfSolidBrush(Color.Black);
 
for (int i = 0; i < doc.Pages.Count; i++)
{
    int count = i;
    string value = offset.ToString();
 
    //Create a PDF Template
    PdfTemplate template = new PdfTemplate(doc.Pages[i].GetClientSize().Width, 50);
    template.Graphics.DrawString("Page " + value, font, brush, new PointF(470, 40));
    //Draw the template on the page graphics of the document
    doc.Pages[i].Graphics.DrawPdfTemplate(template, new PointF(0, 600));
 
    //Draw string
    doc.Pages[i].Graphics.DrawString("Hello World!!!", new PdfStandardFont(PdfFontFamily.TimesRoman, 20, PdfFontStyle.Bold), brush, new PointF(0, 0));
 
    offset += 1;
}
}

 

VB.NET

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add a pages to the PDF document
For j As Integer = 0 To 50 - 1
   pdfDocument.Pages.Add()
Next
'Include header
pdfDocument.Template.Top = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo")
'Set the first page number as 50
Dim offset As Integer = 50
'Add footer
AddFooter(pdfDocument, offset)
'Save and closes the document
pdfDocument.Save("../../Sample.pdf")
pdfDocument.Close(True)

 

Public Function AddHeader(ByVal doc As PdfDocument, ByVal title As String, ByVal description As String) As PdfPageTemplateElement
   Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50)
   Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(rect)
   Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 24)
   Dim doubleHeight As Single = font.Height * 2
   Dim activeColor As Color = Color.FromArgb(44, 71, 120)
   Dim brush As PdfSolidBrush = New PdfSolidBrush(activeColor)
   Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F)
   font = New PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold)
   Dim format As PdfStringFormat = New PdfStringFormat()
   format.Alignment = PdfTextAlignment.Center
   format.LineAlignment = PdfVerticalAlignment.Middle
   header.Graphics.DrawString(title, font, brush, New RectangleF(0, 0, header.Width, header.Height), format)
   brush = New PdfSolidBrush(Color.Gray)
   font = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold)
   format = New PdfStringFormat()
   format.Alignment = PdfTextAlignment.Left
   format.LineAlignment = PdfVerticalAlignment.Bottom
   header.Graphics.DrawString(description, font, brush, New RectangleF(0, 0, header.Width, header.Height - 8), format)
   pen = New PdfPen(Color.DarkBlue, 0.7F)
   header.Graphics.DrawLine(pen, 0, 0, header.Width, 0)
   pen = New PdfPen(Color.DarkBlue, 2.0F)
   header.Graphics.DrawLine(pen, 0, 3, header.Width + 3, 3)
   pen = New PdfPen(Color.DarkBlue, 2.0F)
   header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3)
   header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height)
   Return header
End Function

 

Public Sub AddFooter(ByVal doc As PdfDocument, ByVal offset As Integer)
   Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7)
   Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
 
   For i As Integer = 0 To doc.Pages.Count - 1
       Dim count As Integer = i
       Dim value As String = offset.ToString()
       Dim template As PdfTemplate = New PdfTemplate(doc.Pages(i).GetClientSize().Width, 50)
       template.Graphics.DrawString("Page " & value, font, brush, New PointF(470, 40))
       doc.Pages(i).Graphics.DrawPdfTemplate(template, New PointF(0, 600))
       doc.Pages(i).Graphics.DrawString("Hello World!!!", New      PdfStandardFont(PdfFontFamily.TimesRoman, 20, PdfFontStyle.Bold), brush, New PointF(0, 0))
       offset += 1
    Next
End Sub

 

A complete working sample can be downloaded from PdfSample.zip.

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

Output documents

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

Note:

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

 

See Also:

https://help.syncfusion.com/file-formats/pdf/working-with-watermarks

https://support.syncfusion.com/kb/article/7973/how-to-add-watermark-to-pdf-file-in-c-vb-net

https://support.syncfusion.com/kb/article/8673/how-to-add-a-watermark-to-a-pdf-page-with-an-opaque-image

https://support.syncfusion.com/kb/article/3933/how-to-add-a-watermark-below-the-graphics-and-text-in-the-existing-pdf

Conclusion

I hope you enjoyed learning about how to add the starting page number from 50 in the footer of the PDF document using C# and VB.NET.

You can refer to our 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 forumsDirect-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