Add Headers/Footers on Specific Pages
IronPDF has the ability to add headers and footers to existing PDF files.
We can also use methods of IronPDF to add customized Headers and Footers to old or new PDF documents. To do so, we wait until after the PDF is rendered using the following methods:
- Add Text Headers Using IronPDF
- Add Text Footers Using IronPDF
- Add HTML Headers Using IronPDF
- Add HTML Footers Using IronPDF
Below is an example of how you can add headers and footers to a PDF using the IronPDF library in C#:
using IronPdf;
class Program
{
static void Main()
{
// Load an existing PDF document
var pdfDocument = PdfDocument.FromFile("existing-document.pdf");
// Create a text header
var textHeader = new TextHeaderFooter()
{
CenterText = "My Company",
DrawDividerLine = true // Draw a line beneath the header
};
// Add the text header to the PDF
pdfDocument.AddHeaders(textHeader);
// Create a text footer
var textFooter = new TextHeaderFooter()
{
RightText = "Page {page} of {total-pages}", // Adds page number to the footer
DrawDividerLine = true
};
// Add the text footer to the PDF
pdfDocument.AddFooters(textFooter);
// Save the PDF with new headers and footers
pdfDocument.SaveAs("document-with-headers-and-footers.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Load an existing PDF document
var pdfDocument = PdfDocument.FromFile("existing-document.pdf");
// Create a text header
var textHeader = new TextHeaderFooter()
{
CenterText = "My Company",
DrawDividerLine = true // Draw a line beneath the header
};
// Add the text header to the PDF
pdfDocument.AddHeaders(textHeader);
// Create a text footer
var textFooter = new TextHeaderFooter()
{
RightText = "Page {page} of {total-pages}", // Adds page number to the footer
DrawDividerLine = true
};
// Add the text footer to the PDF
pdfDocument.AddFooters(textFooter);
// Save the PDF with new headers and footers
pdfDocument.SaveAs("document-with-headers-and-footers.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Load an existing PDF document
Dim pdfDocument = PdfDocument.FromFile("existing-document.pdf")
' Create a text header
Dim textHeader = New TextHeaderFooter() With {
.CenterText = "My Company",
.DrawDividerLine = True
}
' Add the text header to the PDF
pdfDocument.AddHeaders(textHeader)
' Create a text footer
Dim textFooter = New TextHeaderFooter() With {
.RightText = "Page {page} of {total-pages}",
.DrawDividerLine = True
}
' Add the text footer to the PDF
pdfDocument.AddFooters(textFooter)
' Save the PDF with new headers and footers
pdfDocument.SaveAs("document-with-headers-and-footers.pdf")
End Sub
End Class
$vbLabelText $csharpLabel
Explanation:
- Loading the PDF: An existing PDF document is loaded from a file using
PdfDocument.FromFile()
. - Creating Headers and Footers:
- TextHeaderFooter(): This object is instantiated to define the content and style of text headers and footers.
- For the header,
CenterText
is set to display the company name, with an optional divider line. - For the footer,
RightText
is set to display the current page number and total pages.
- Adding Headers and Footers: The
AddHeaders()
andAddFooters()
methods are used to apply headers and footers to the PDF document. - Saving the PDF: The modified document is saved with a new name.
By following the example above, you can easily insert headers and footers into PDF documents using IronPDF.
View on GitHub
Code
https://github.com/iron-software/IronPdf.Examples/tree/main/examples/adding-headers-and-footers-advanced