Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer


To create a header/footer out of only text, instantiate a TextHeaderFooter object, add your desired text, and add the object to your PDF.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-add-textheaderfooter.cs
using IronPdf;

// The below code generates a PDF document with a custom text header and footer

// Instantiate a Chrome PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a simple HTML as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create a text header for the PDF
HtmlHeaderFooter textHeader = new HtmlHeaderFooter
{
    // Text to appear at the center of the header
    Html = "<div style='text-align: center;'>This is the header!</div>",
    MaxHeight = 20 // Specifies maximum header height
};

// Create a text footer for the PDF
HtmlHeaderFooter textFooter = new HtmlHeaderFooter
{
    // Text to appear at the center of the footer
    Html = "<div style='text-align: center;'>This is the footer!</div>",
    MaxHeight = 20 // Specifies maximum footer height
};

// Add text header and footer to the PDF document
pdf.AddHtmlHeader(textHeader);
pdf.AddHtmlFooter(textFooter);

// Save the PDF document with the specified file name
pdf.SaveAs("addTextHeaderFooter.pdf");
Imports IronPdf



' The below code generates a PDF document with a custom text header and footer



' Instantiate a Chrome PDF renderer

Private renderer As New ChromePdfRenderer()



' Render a simple HTML as a PDF document

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' Create a text header for the PDF

Private textHeader As New HtmlHeaderFooter With {

	.Html = "<div style='text-align: center;'>This is the header!</div>",

	.MaxHeight = 20

}



' Create a text footer for the PDF

Private textFooter As New HtmlHeaderFooter With {

	.Html = "<div style='text-align: center;'>This is the footer!</div>",

	.MaxHeight = 20

}



' Add text header and footer to the PDF document

pdf.AddHtmlHeader(textHeader)

pdf.AddHtmlFooter(textFooter)



' Save the PDF document with the specified file name

pdf.SaveAs("addTextHeaderFooter.pdf")
$vbLabelText   $csharpLabel

Alternatively, you can directly add a header/footer using the renderer's rendering options. This will add the text header and footer during rendering process.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-textheaderfooter.cs
using IronPdf;

// Create an instance of ChromePdfRenderer which is used to render HTML as a PDF.
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure the TextHeader for the PDF rendering options.
// This header will appear at the top-center of each page in the PDF.
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
    FontSize = 12,       // Optional: specify font size for the header text
    FontFamily = "Arial" // Optional: specify font family for the header text
};

// Configure the TextFooter for the PDF rendering options.
// This footer will appear at the bottom-center of each page in the PDF.
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
    FontSize = 10,       // Optional: specify font size for the footer text
    FontFamily = "Arial" // Optional: specify font family for the footer text
};

// Render the HTML as a PDF document with the specified header and footer.
// The HTML content "<h1>Hello World!</h1>" will be the main body of the PDF.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Save the rendered PDF document to a file.
pdf.SaveAs("renderWithTextHeaderFooter.pdf");
Imports IronPdf



' Create an instance of ChromePdfRenderer which is used to render HTML as a PDF.

Private renderer As New ChromePdfRenderer()



' Configure the TextHeader for the PDF rendering options.

' This header will appear at the top-center of each page in the PDF.

renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {

	.CenterText = "This is the header!",

	.FontSize = 12,

	.FontFamily = "Arial"

}



' Configure the TextFooter for the PDF rendering options.

' This footer will appear at the bottom-center of each page in the PDF.

renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {

	.CenterText = "This is the footer!",

	.FontSize = 10,

	.FontFamily = "Arial"

}



' Render the HTML as a PDF document with the specified header and footer.

' The HTML content "<h1>Hello World!</h1>" will be the main body of the PDF.

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' Save the rendered PDF document to a file.

pdf.SaveAs("renderWithTextHeaderFooter.pdf")
$vbLabelText   $csharpLabel

Customize Text and Divider Properties

In the TextHeaderFooter class, you have the ability to set the text for the left, center, and right positions. Additionally, you can customize the font type and size of the text and add a divider with a custom color by configuring the relevant properties.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-options.cs
using IronPdf;
using IronPdf.Font;
using IronSoftware.Drawing;

// This code snippet demonstrates how to create a PDF text header using the IronPdf library.
// The TextHeaderFooter object is used to define the appearance and layout of the header.

TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "Center text", // Specifies the text that will be displayed in the center of the header.
    LeftText = "Left text",     // Specifies the text that will be displayed on the left side of the header.
    RightText = "Right text",   // Specifies the text that will be displayed on the right side of the header.
    Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic, // Sets the font style to Arial Bold Italic.
    FontSize = 16,              // Sets the font size to 16.
    DrawDividerLine = true,     // Enables the drawing of a divider line between the header and the document content.
    DrawDividerLineColor = IronSoftware.Drawing.Color.Red // Sets the color of the divider line to red.
};

// Note: Make sure to have the IronPdf and IronSoftware.Drawing packages installed and referenced correctly 
// in your project to use the functionalities provided by them.
Imports IronPdf

Imports IronPdf.Font

Imports IronSoftware.Drawing



' This code snippet demonstrates how to create a PDF text header using the IronPdf library.

' The TextHeaderFooter object is used to define the appearance and layout of the header.



Private textHeader As New TextHeaderFooter With {

	.CenterText = "Center text",

	.LeftText = "Left text",

	.RightText = "Right text",

	.Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic,

	.FontSize = 16,

	.DrawDividerLine = True,

	.DrawDividerLineColor = IronSoftware.Drawing.Color.Red

}



' Note: Make sure to have the IronPdf and IronSoftware.Drawing packages installed and referenced correctly 

' in your project to use the functionalities provided by them.
$vbLabelText   $csharpLabel

Output Text Header

Text Header

You can see what font types are available by default in the IronPDF API Reference.

By default, the Text header and footer in IronPDF come with predefined margins. If you want the text header to span the entire width of the PDF document, you can specify margin values of 0. This can be achieved by either setting the margins directly in the AddTextHeaders and AddTextFooters functions or through the RenderingOptions in ChromePdfRenderer.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-margins.cs
using IronPdf;

// Instantiate a renderer for creating PDF documents
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render an HTML string into a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create a header with custom centered text
HtmlHeaderFooter header = new HtmlHeaderFooter
{
    CenterText = "This is the header!"
};

// Create a footer with custom centered text
HtmlHeaderFooter footer = new HtmlHeaderFooter
{
    CenterText = "This is the footer!"
};

// Add the header to the PDF with specified margins
// Left Margin = 35 mm, Right Margin = 30 mm, Top Margin = 25 mm, Bottom Margin = 25 mm
pdf.AddHtmlHeaders(header, new HtmlHeaderFooterMargins 
{
    Left = 35,
    Right = 30,
    Top = 25,
});

// Add the footer to the PDF with specified margins
// Left Margin = 35 mm, Right Margin = 30 mm, Top Margin = 25 mm, Bottom Margin = 25 mm
pdf.AddHtmlFooters(footer, new HtmlHeaderFooterMargins 
{
    Left = 35,
    Right = 30,
    Bottom = 25,
});

// Save the PDF to a file
pdf.SaveAs("HelloWorldWithHeaderAndFooter.pdf");
Imports IronPdf



' Instantiate a renderer for creating PDF documents

Private renderer As New ChromePdfRenderer()



' Render an HTML string into a PDF document

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' Create a header with custom centered text

Private header As New HtmlHeaderFooter With {.CenterText = "This is the header!"}



' Create a footer with custom centered text

Private footer As New HtmlHeaderFooter With {.CenterText = "This is the footer!"}



' Add the header to the PDF with specified margins

' Left Margin = 35 mm, Right Margin = 30 mm, Top Margin = 25 mm, Bottom Margin = 25 mm

pdf.AddHtmlHeaders(header, New HtmlHeaderFooterMargins With {

	.Left = 35,

	.Right = 30,

	.Top = 25

})



' Add the footer to the PDF with specified margins

' Left Margin = 35 mm, Right Margin = 30 mm, Top Margin = 25 mm, Bottom Margin = 25 mm

pdf.AddHtmlFooters(footer, New HtmlHeaderFooterMargins With {

	.Left = 35,

	.Right = 30,

	.Bottom = 25

})



' Save the PDF to a file

pdf.SaveAs("HelloWorldWithHeaderAndFooter.pdf")
$vbLabelText   $csharpLabel

If you add margin values in the RenderingOptions of ChromePdfRenderer, these margins will also be applied to the header and footer.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-rendering-options-margins.cs
using IronPdf;

// Initialize a new ChromePdfRenderer to create PDF documents
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create and configure a header for the PDF
TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!"
};

// Create and configure a footer for the PDF
TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!"
};

// Set margin values in millimeters for the rendered PDF
renderer.RenderingOptions.MarginRight = 30;
renderer.RenderingOptions.MarginLeft = 30;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Assign the configured header and footer to the rendering options
renderer.RenderingOptions.TextHeader = header;
renderer.RenderingOptions.TextFooter = footer;

// Render an HTML string as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// The PDF is now stored in the variable 'pdf' and can be saved to a file or stream
Imports IronPdf



' Initialize a new ChromePdfRenderer to create PDF documents

Private renderer As New ChromePdfRenderer()



' Create and configure a header for the PDF

Private header As New TextHeaderFooter With {.CenterText = "This is the header!"}



' Create and configure a footer for the PDF

Private footer As New TextHeaderFooter With {.CenterText = "This is the footer!"}



' Set margin values in millimeters for the rendered PDF

renderer.RenderingOptions.MarginRight = 30

renderer.RenderingOptions.MarginLeft = 30

renderer.RenderingOptions.MarginTop = 25

renderer.RenderingOptions.MarginBottom = 25



' Assign the configured header and footer to the rendering options

renderer.RenderingOptions.TextHeader = header

renderer.RenderingOptions.TextFooter = footer



' Render an HTML string as a PDF document

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' The PDF is now stored in the variable 'pdf' and can be saved to a file or stream
$vbLabelText   $csharpLabel

The UseMarginsOnHeaderAndFooter property on RenderingOptions is not suitable for this use case. It applies the same margin values to the header, footer, and main content, which can cause the header to overlap with the document body. This property is primarily intended for applying headers and footers to existing PDFs using the AddTextHeadersAndFooters method.

Dynamic Margin Sizing

The static margin posed an issue when the header content varied between documents. Adjustments were required not only for the header and footer margins but also for the main HTML margin to accommodate different header and footer sizes. Consequently, we have implemented a Dynamic Margin Sizing feature where the height of the header and footer will dynamically adjust based on the content, and the main HTML will reposition accordingly. Please use the code below to check the feature out:

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-dynamic-marigns.cs
using IronPdf;

// Initialize a PDF renderer using Chrome's rendering engine.
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set up an HTML header with styling and content.
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    // Define the HTML content for the header with CSS styling.
    HtmlFragment = @"<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",

    // Set the header's maximum height (in pixels) to ensure it doesn't exceed a certain size.
    MaxHeight = 100
};

// Render the provided HTML content into a PDF document.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");

// Save the generated PDF to a file with the specified filename.
pdf.SaveAs("dynamicHeaderSize.pdf");
Imports IronPdf



' Initialize a PDF renderer using Chrome's rendering engine.

Private renderer As New ChromePdfRenderer()



' Set up an HTML header with styling and content.

renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {

	.HtmlFragment = "<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>

                    <h1>Example header</h1> <br>

                    <p>Header content</p>

                    </div>",

	.MaxHeight = 100

}



' Render the provided HTML content into a PDF document.

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")



' Save the generated PDF to a file with the specified filename.

pdf.SaveAs("dynamicHeaderSize.pdf")
$vbLabelText   $csharpLabel

You can easily add metadata such as page numbers, the date, and the title of a PDF, by incorporating placeholder strings in your text. Here are all the available metadata options:

  • {page}: Current page number.
  • {total-pages}: Total page number.
  • {url}: Web URL from which the PDF document was rendered.
  • {date}: Current date.
  • {time}: Current time.
  • {html-title}: HTML title specified in the title tag in HTML.
  • {pdf-title}: PDF title specified in the PDF metadata.

To learn more about {page} and {total-pages}, please visit the IronPDF Page Numbers Guide How-To guide.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-mail-merge.cs
using IronPdf;

// An instance of IronPdf's TextHeaderFooter class is created to represent the header of the PDF.
// The header will display the current page and total pages at the center,
// the current date on the left, and the current time on the right.
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "{page} of {total-pages}", // Displays current page number and total page count
    LeftText = "Today's date: {date}",      // Displays the current date
    RightText = "The time: {time}",         // Displays the current time
    FontSize = 12,                           // Specifies the font size for the header text
    FontFamily = "Arial"                     // Specifies the font family for the header text
};

// Another instance of TextHeaderFooter class is created to represent the footer of the PDF.
// The footer contains information like the current URL, title of the HTML document, and the title of the PDF.
TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "Current URL: {url}",       // Displays the current URL of the document
    LeftText = "Title of the HTML: {html-title}", // Displays the title of the HTML document
    RightText = "Title of the PDF: {pdf-title}",  // Displays the title of the PDF document
    FontSize = 10,                           // Specifies the font size for the footer text
    FontFamily = "Times New Roman"           // Specifies the font family for the footer text
};

// Note: Additional properties such as Margin, DrawDividerLine, etc., could be configured
// according to the specific needs of the PDF header and footer.
Imports IronPdf



' An instance of IronPdf's TextHeaderFooter class is created to represent the header of the PDF.

' The header will display the current page and total pages at the center,

' the current date on the left, and the current time on the right.

Private textHeader As New TextHeaderFooter With {

	.CenterText = "{page} of {total-pages}",

	.LeftText = "Today's date: {date}",

	.RightText = "The time: {time}",

	.FontSize = 12,

	.FontFamily = "Arial"

}



' Another instance of TextHeaderFooter class is created to represent the footer of the PDF.

' The footer contains information like the current URL, title of the HTML document, and the title of the PDF.

Private textFooter As New TextHeaderFooter With {

	.CenterText = "Current URL: {url}",

	.LeftText = "Title of the HTML: {html-title}",

	.RightText = "Title of the PDF: {pdf-title}",

	.FontSize = 10,

	.FontFamily = "Times New Roman"

}



' Note: Additional properties such as Margin, DrawDividerLine, etc., could be configured

' according to the specific needs of the PDF header and footer.
$vbLabelText   $csharpLabel

You can further customize your header/footer by utilizing HTML and CSS. To create an HTML header/footer, use the HtmlHeaderFooter class. If you would like to retain CSS styles from a CSS style sheet, make sure to set LoadStylesAndCSSFromMainHtmlDocument = true in the class properties.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

// HTML content for the header
string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

// HTML content for the footer
string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate the PDF renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a simple HTML string into a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create a header for the PDF with HTML content
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true // Ensure that styles from the HTML document are applied
};

// Create a footer for the PDF with HTML content
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true // Ensure that styles from the HTML document are applied
};

// Add the header to the PDF document
pdf.AddHtmlHeaders(htmlHeader);

// Add the footer to the PDF document
pdf.AddHtmlFooters(htmlFooter);

// (Optional) save the document to the file system
// Uncomment the following line to enable saving
// pdf.SaveAs("output.pdf");
Imports IronPdf



' HTML content for the header

Private headerHtml As String = "

    <html>

    <head>

        <link rel='stylesheet' href='style.css'>

    </head>

    <body>

        <h1>This is a header!</h1>

    </body>

    </html>"



' HTML content for the footer

Private footerHtml As String = "

    <html>

    <head>

        <link rel='stylesheet' href='style.css'>

    </head>

    <body>

        <h1>This is a footer!</h1>

    </body>

    </html>"



' Instantiate the PDF renderer

Private renderer As New ChromePdfRenderer()



' Render a simple HTML string into a PDF document

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' Create a header for the PDF with HTML content

Private htmlHeader As New HtmlHeaderFooter With {

	.HtmlFragment = headerHtml,

	.LoadStylesAndCSSFromMainHtmlDocument = True

}



' Create a footer for the PDF with HTML content

Private htmlFooter As New HtmlHeaderFooter With {

	.HtmlFragment = footerHtml,

	.LoadStylesAndCSSFromMainHtmlDocument = True

}



' Add the header to the PDF document

pdf.AddHtmlHeaders(htmlHeader)



' Add the footer to the PDF document

pdf.AddHtmlFooters(htmlFooter)



' (Optional) save the document to the file system

' Uncomment the following line to enable saving

' pdf.SaveAs("output.pdf");
$vbLabelText   $csharpLabel

Similar to the text header and footer, the AddHtmlHeaders and AddHtmlFooters methods shown above have pre-defined margins applied to them. To apply custom margins, use an overload of the functions with the specified margin values. To span the whole content without any margins, set the margins in the overload function to 0.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter-margins.cs
// This code snippet demonstrates how to add headers and footers to a PDF document
// using assumed objects `pdf`, `header`, and `footer`.

// It's assumed that `pdf`, `header`, and `footer` are predefined and valid objects in the code base,
// where `pdf` likely represents a PDF document object from a library capable of handling PDF manipulation.
// The `header` and `footer` are assumed to be objects containing the desired HTML content.

try
{
    // Add HTML headers to the PDF with specified margins (0, 0, 0).
    // The method `AddHtmlHeaders` is assumed to be defined in the library being used to handle PDF documents.
    // Parameters provided include the header content and the margins (left, right, and top).
    pdf.AddHtmlHeaders(header, 0, 0, 0);

    // Add HTML footers to the PDF with specified margins (0, 0, 0).
    // Similar to headers, `AddHtmlFooters` is a method to insert footer content in the PDF document.
    // Parameters include the footer content and margins (left, right, and bottom).
    pdf.AddHtmlFooters(footer, 0, 0, 0);
}
catch (Exception ex)
{
    // Handle any exceptions that may arise during the operation.
    // This could include issues such as null references, invalid formatting in the HTML, etc.
    Console.WriteLine("An error occurred: " + ex.Message);
}
' This code snippet demonstrates how to add headers and footers to a PDF document

' using assumed objects `pdf`, `header`, and `footer`.



' It's assumed that `pdf`, `header`, and `footer` are predefined and valid objects in the code base,

' where `pdf` likely represents a PDF document object from a library capable of handling PDF manipulation.

' The `header` and `footer` are assumed to be objects containing the desired HTML content.



Try

	' Add HTML headers to the PDF with specified margins (0, 0, 0).

	' The method `AddHtmlHeaders` is assumed to be defined in the library being used to handle PDF documents.

	' Parameters provided include the header content and the margins (left, right, and top).

	pdf.AddHtmlHeaders(header, 0, 0, 0)



	' Add HTML footers to the PDF with specified margins (0, 0, 0).

	' Similar to headers, `AddHtmlFooters` is a method to insert footer content in the PDF document.

	' Parameters include the footer content and margins (left, right, and bottom).

	pdf.AddHtmlFooters(footer, 0, 0, 0)

Catch ex As Exception

	' Handle any exceptions that may arise during the operation.

	' This could include issues such as null references, invalid formatting in the HTML, etc.

	Console.WriteLine("An error occurred: " & ex.Message)

End Try
$vbLabelText   $csharpLabel

Adding headers and footers could also be done directly through the renderer's rendering options. This will add the HTML header and footer during rendering process.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-htmlheaderfooter.cs
using IronPdf;

// Define the HTML content for the header. This can include styles linked via external CSS.
string headerHtml = @"
<html>
<head>
    <link rel='stylesheet' href='style.css'>
</head>
<body>
    <h1>This is a header!</h1>
</body>
</html>";

// Define the HTML content for the footer. Like the header, this can also include external CSS.
string footerHtml = @"
<html>
<head>
    <link rel='stylesheet' href='style.css'>
</head>
<body>
    <h1>This is a footer!</h1>
</body>
</html>";

// Instantiate a PDF renderer using the IronPdf library
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure the header for the PDF using HTML content
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true, // Ensures that styles from the main document are included
};

// Configure the footer for the PDF using HTML content
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true, // Ensures that styles from the main document are included
};

// Render the main content of the PDF. This content will include the specified header and footer.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// At this point, the generated PDF document is stored in the 'pdf' variable.
// To save the PDF file to disk, use the following code:
pdf.SaveAs("GeneratedPDF.pdf");

// The 'SaveAs' method takes the desired file path as a parameter and saves the PDF to that location.
Imports IronPdf



' Define the HTML content for the header. This can include styles linked via external CSS.

Private headerHtml As String = "

<html>

<head>

    <link rel='stylesheet' href='style.css'>

</head>

<body>

    <h1>This is a header!</h1>

</body>

</html>"



' Define the HTML content for the footer. Like the header, this can also include external CSS.

Private footerHtml As String = "

<html>

<head>

    <link rel='stylesheet' href='style.css'>

</head>

<body>

    <h1>This is a footer!</h1>

</body>

</html>"



' Instantiate a PDF renderer using the IronPdf library

Private renderer As New ChromePdfRenderer()



' Configure the header for the PDF using HTML content

renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {

	.HtmlFragment = headerHtml,

	.LoadStylesAndCSSFromMainHtmlDocument = True

}



' Configure the footer for the PDF using HTML content

renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {

	.HtmlFragment = footerHtml,

	.LoadStylesAndCSSFromMainHtmlDocument = True

}



' Render the main content of the PDF. This content will include the specified header and footer.

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")



' At this point, the generated PDF document is stored in the 'pdf' variable.

' To save the PDF file to disk, use the following code:

pdf.SaveAs("GeneratedPDF.pdf")



' The 'SaveAs' method takes the desired file path as a parameter and saves the PDF to that location.
$vbLabelText   $csharpLabel

Choosing Between Text and HTML Headers/Footers

In deciding between Text and HTML headers/footers, consider the trade-offs. If you prioritize faster PDF rendering, opt for Text headers/footers. On the other hand, if customizability and styling are essential, go for HTML headers/footers. The difference in rendering times between Text and HTML headers/footers is not too significant when the HTML headers/footers do not contain much content. However, it does increase as the size and number of assets in the HTML headers/footers increase.

Frequently Asked Questions

How do I add headers and footers to a PDF in C#?

To add headers and footers to a PDF in C# using IronPDF, you can use methods like AddTextHeaders, AddTextFooters, AddHtmlHeaders, and AddHtmlFooters. Start by downloading the IronPDF library, load or create a PDF, then apply the desired header or footer using the appropriate methods.

Can I customize the text and divider properties of headers and footers?

Yes, you can customize text and divider properties in IronPDF. The TextHeaderFooter class allows you to set text alignment, font type, size, and add dividers with custom colors to your headers and footers.

How can I set margins for headers and footers?

Margins for headers and footers can be set directly in the AddTextHeaders and AddTextFooters functions or through RenderingOptions in ChromePdfRenderer. You can specify margin values to control the width coverage of the header/footer.

What is Dynamic Margin Sizing?

Dynamic Margin Sizing in IronPDF adjusts header and footer heights based on their content, ensuring that the main HTML content repositions accordingly. Enable this feature by setting DynamicMarginsEnabled to true in PdfRenderOptions.

How can I include metadata in headers and footers?

You can include metadata such as page numbers, date, and PDF title by using placeholder strings like {page}, {total-pages}, {date}, and {pdf-title} within the text of headers and footers.

What are the benefits of using HTML headers and footers?

HTML headers and footers in IronPDF offer greater customization and styling options compared to text headers and footers. They allow the use of HTML and CSS for enhanced design flexibility.

Can I apply custom margins to HTML headers and footers?

Yes, you can apply custom margins to HTML headers and footers by using an overload of AddHtmlHeaders and AddHtmlFooters functions with specified margin values. Set margins to zero to span the entire content width.

How do I choose between text and HTML headers/footers?

Choose text headers/footers for faster PDF rendering. Opt for HTML headers/footers for better customizability and styling. Consider the content size and number of assets as they can affect rendering times.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.