Add Classic Text Headers & Footers

This code example shows how IronPDF allows developers to define headers and footers for PDF documents loaded from a file system or generated from HTML content.

The TextHeaderFooter class enables developers to specify the text that should be included in the left, the right, or the central region of a PDF document's header or footer. As shown above, developers can quickly put together headers and footers using IronPDF's built-in templating tags (such as {date}, {time}, and {page}). Users do not need to use these tags exclusively; IronPDF gives users the freedom to truly incorporate any kind and amount of text at these locations.

Consider using the HtmlHeaderFooter class in place of the TextHeaderFooter class for far greater design flexibility within IronPDF to enhance your document presentation.

using IronPdf;

// Initialize a new instance of PdfDocument
PdfDocument pdf = new PdfDocument();

// Load an existing PDF, or specify HTML to convert
// For demonstration, generate a simple HTML document
html = "<html><body><h1>Hello, IronPDF!</h1><p>This is a paragraph.</p></body></html>";
pdf = HtmlToPdf.StaticRenderHtmlAsPdf(html);

// Define a text header
TextHeaderFooter header = new TextHeaderFooter()
{
    CenterText = "Title of the document - {date} {time}",
    RightText = "Page {page} of {total-pages}"
};

// Apply the header to each page of the PDF
pdf.Header = header;

// Define a text footer
TextHeaderFooter footer = new TextHeaderFooter()
{
    CenterText = "Confidential Document",
    RightText = "Page {page} of {total-pages}"
};

// Apply the footer to each page of the PDF
pdf.Footer = footer;

// Save the PDF to a file
pdf.SaveAs("OutputDocument.pdf");

/* 
This code demonstrates setting up headers and footers with textual content in a PDF.
Templating tags like {date}, {time}, {page}, and {total-pages} are used to insert dynamic content.
The 'TextHeaderFooter' class is used here to define headers and footers. Alternatively, the 
'HtmlHeaderFooter' class can be used for more intricate styling using HTML/CSS.
*/
using IronPdf;

// Initialize a new instance of PdfDocument
PdfDocument pdf = new PdfDocument();

// Load an existing PDF, or specify HTML to convert
// For demonstration, generate a simple HTML document
html = "<html><body><h1>Hello, IronPDF!</h1><p>This is a paragraph.</p></body></html>";
pdf = HtmlToPdf.StaticRenderHtmlAsPdf(html);

// Define a text header
TextHeaderFooter header = new TextHeaderFooter()
{
    CenterText = "Title of the document - {date} {time}",
    RightText = "Page {page} of {total-pages}"
};

// Apply the header to each page of the PDF
pdf.Header = header;

// Define a text footer
TextHeaderFooter footer = new TextHeaderFooter()
{
    CenterText = "Confidential Document",
    RightText = "Page {page} of {total-pages}"
};

// Apply the footer to each page of the PDF
pdf.Footer = footer;

// Save the PDF to a file
pdf.SaveAs("OutputDocument.pdf");

/* 
This code demonstrates setting up headers and footers with textual content in a PDF.
Templating tags like {date}, {time}, {page}, and {total-pages} are used to insert dynamic content.
The 'TextHeaderFooter' class is used here to define headers and footers. Alternatively, the 
'HtmlHeaderFooter' class can be used for more intricate styling using HTML/CSS.
*/
Imports IronPdf

' Initialize a new instance of PdfDocument
Private pdf As New PdfDocument()

' Load an existing PDF, or specify HTML to convert
' For demonstration, generate a simple HTML document
html = "<html><body><h1>Hello, IronPDF!</h1><p>This is a paragraph.</p></body></html>"
pdf = HtmlToPdf.StaticRenderHtmlAsPdf(html)

' Define a text header
Dim header As New TextHeaderFooter() With {
	.CenterText = "Title of the document - {date} {time}",
	.RightText = "Page {page} of {total-pages}"
}

' Apply the header to each page of the PDF
pdf.Header = header

' Define a text footer
Dim footer As New TextHeaderFooter() With {
	.CenterText = "Confidential Document",
	.RightText = "Page {page} of {total-pages}"
}

' Apply the footer to each page of the PDF
pdf.Footer = footer

' Save the PDF to a file
pdf.SaveAs("OutputDocument.pdf")

' 
'This code demonstrates setting up headers and footers with textual content in a PDF.
'Templating tags like {date}, {time}, {page}, and {total-pages} are used to insert dynamic content.
'The 'TextHeaderFooter' class is used here to define headers and footers. Alternatively, the 
''HtmlHeaderFooter' class can be used for more intricate styling using HTML/CSS.
'
$vbLabelText   $csharpLabel