Add Headers and Footers in PDF using C#

Header and footer sections in a document show document information, such as the title of the document, logo, the chapter heading, page numbers, etc. We can add any text or image in the headers/footers of the PDF document programmatically. In this article, we will learn how to add headers and footers in PDF documents using C#.

The following topics shall be covered in this article:

C# API to Add Headers and Footers in PDF Documents

For adding headers and footers in PDF files, we will be using Aspose.PDF for .NET API. It allows us to generate, modify, convert, render, secure and print supported documents without using Adobe Acrobat. Please either download the DLL of the API or install it using NuGet.

Install-Package Aspose.PDF

Add Text in Header of PDF using C#

We can add text in the header of an existing PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class with input file path as an argument. It is the main class that represents a PDF document and allows performing various functionalities.
  2. Next, create an instance of the TextStamp class with a text to show in the header of the document.
  3. Then, set various properties such as TopMargin, HorizontalAlignment, and VerticalAlignment as Top, etc.
  4. Optionally, set ForegroundColor, Font, FontStyle, FontSize, BackgroundColor, RotateAngle and Zoom level for the text.
  5. After that, loop through all the pages and add header using the Page.AddStamp() method with TextStamp object.
  6. Finally, call the Document.Save() method with the output file path as an argument to save the output file.

The following code sample shows how to add text in the header of a PDF document using C#.

// This code example demonstrates how to add text in the header of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create header
TextStamp textStamp = new TextStamp("Header Text");
// Set properties of the stamp
textStamp.TopMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Top;
// Specify the font style
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = Color.Red;
textStamp.TextState.FontSize = 14;
textStamp.TextState.BackgroundColor = Color.Pink;
textStamp.TextState.Font = FontRepository.FindFont("Verdana");
// Add header on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
Add Text in Header of PDF using C#.

Add Text in Header of PDF using C#.

We can add text in the footer of PDF documents programmatically by following the steps mentioned earlier. However, we need to set BottomMargin and VerticalAlignment as Bottom to show the text in the footer.

The following code sample shows how to add text in the footer of a PDF document using C#.

// This code example demonstrates how to add text in the footer of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create footer
TextStamp textStamp = new TextStamp("Footer Text");
// Set properties of the stamp
textStamp.BottomMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
Add Text in Footer of PDF using C#.

Add Text in Footer of PDF using C#.

Insert Image in Header of PDF using C#

We can also add an image in the header of an existing PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class with input file path as an argument.
  2. Next, create an instance of the ImageStamp class with image file path as an argument.
  3. Then, set various properties such as TopMargin, HorizontalAlignment, and VerticalAlignment as Top, etc.
  4. After that, loop through all the pages and add header using the Page.AddStamp() method with ImageStamp object.
  5. Finally, call the Document.Save() method with the output file path as an argument to save the output file.

The following code sample shows how to add an image in the header of a PDF document using C#.

// This code example demonstrates how to add an image in the header of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create header
ImageStamp imageStamp = new ImageStamp(@"C:\Files\conholdate-logo.jpg");
// Set properties of the stamp
imageStamp.TopMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
imageStamp.VerticalAlignment = VerticalAlignment.Top;
// Add header on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
Insert Image in Header of PDF using C#.

Insert Image in Header of PDF using C#.

We can add images in the footer of PDF documents programmatically by following the steps mentioned earlier. However, we need to set BottomMargin and VerticalAlignment as Bottom to show the image in the footer.

The following code sample shows how to add an image in the footer of a PDF document using C#.

// This code example demonstrates how to add an image in the footer of an existing PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create footer
ImageStamp imageStamp = new ImageStamp(@"C:\Files\conholdate-logo.jpg");
// Set properties of the stamp
imageStamp.BottomMargin = 10;
imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
imageStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");
Insert Image in Footer of PDF using C#.

Insert Image in Footer of PDF using C#.

Add Different Headers and Footers on Different Pages

We can add different headers/footers for different pages in a single PDF document by following the steps given below:

  1. Firstly, load a PDF document using the Document class with input file path as an argument.
  2. Next, create multiple instances of the ImageStamp class with image file path and/or the TextStamp class with a text to show.
  3. Then, set various properties such as TopMargin, HorizontalAlignment, and VerticalAlignment as Top for the header and BottomMargin and VerticalAlignment as Bottom for the footer.
  4. After that, add header or footer using the Page.AddStamp() method with ImageStamp or TextStamp object for a page.
  5. Finally, call the Document.Save() method with the output file path as an argument to save the output file.

The following code sample shows how to add multiple headers and footers in a single PDF document using C#.

// This code example demonstrates how to add different headers for different pages in a single PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Create three stamps
ImageStamp stamp1 = new ImageStamp(@"C:\Files\PDF\conholdate-logo.jpg");
TextStamp stamp2 = new TextStamp("Header Stamp 2");
TextStamp stamp3 = new TextStamp("Header Stamp 3");
// Set stamp alignment for stamp1
stamp1.VerticalAlignment = VerticalAlignment.Top;
stamp1.HorizontalAlignment = HorizontalAlignment.Center;
// Set stamp alignment for stamp2
stamp2.VerticalAlignment = VerticalAlignment.Top;
// Set Horizontal alignment information for stamp as Center aligned
stamp2.HorizontalAlignment = HorizontalAlignment.Center;
// Set the zooming factor for stamp object
stamp2.Zoom = 10;
// Set stamp alignment for stamp3
stamp3.VerticalAlignment = VerticalAlignment.Top;
// Set the Horizontal alignment inforamtion for stamp object as Center aligned
stamp3.HorizontalAlignment = HorizontalAlignment.Center;
// Set the rotation angle for stamp object
stamp3.RotateAngle = 35;
// Add first stamp on first page;
pdfDocument.Pages[1].AddStamp(stamp1);
// Add second stamp on second page;
pdfDocument.Pages[2].AddStamp(stamp2);
// Add third stamp on third page.
pdfDocument.Pages[3].AddStamp(stamp3);
// Save the updated document
pdfDocument.Save(@"C:\Files\output.pdf");

We can add page numbers in the footer section of PDF documents by following the steps given below:

  1. Firstly, load a PDF document using the Document class with input file path as an argument.
  2. Next, do the following for each page in the Document.Pages collection.
    • Create an instance of the TextStamp class with a text concatenated with the current page number.
    • Then, set various properties such as BottomMargin, HorizontalAlignment, and VerticalAlignment as Bottom, etc.
    • After that, call the Page.AddStamp() method with the TextStamp object to add page number in the footer.
  3. Finally, call the Document.Save() method with the output file path as an argument to save the output file.

The following code sample shows how to add a page number for each page in the footer of a PDF document using C#.

// This code example demonstrates how to add page number in the footer of each page of a PDF document.
// Load the PDF document
Document pdfDocument = new Document(@"C:\Files\sample.pdf");
// Add footer on all pages
foreach (Page page in pdfDocument.Pages)
{
// Create footer
TextStamp textStamp = new TextStamp("Page " + page.Number + " of " + pdfDocument.Pages.Count + " pages.");
// Set properties of the stamp
textStamp.BottomMargin = 10;
textStamp.HorizontalAlignment = HorizontalAlignment.Center;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Add stamp
page.AddStamp(textStamp);
}
// Save the updated document
pdfDocument.Save(@"C:\Files\PDF\output.pdf");
Add Page Numbers in the Footer.

Add Page Numbers in Footer of PDF using C#.

Get a Free API License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to add a text or an image in the headers/footers of existing PDF files using C#. We have also seen how to add different headers on different pages in a PDF document and how to add a page number in the footer of a document. Besides, you can learn more about Aspose.PDF for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also