Agregar Encabezados y Pies HTML

Configure las opciones de renderizado para incluir un encabezado y pie de página HTML al renderizar un documento PDF con IronPDF, una biblioteca de Iron Software que permite una avanzada generación y manipulación de PDF.

Defina el contenido del encabezado con una línea divisoria, un fragmento HTML y especifique la altura máxima del encabezado utilizando las herramientas flexibles de renderizado de PDF de IronPDF. De manera similar, defina el contenido del pie de página utilizando la propiedad htmlHeader proporcionada por IronPDF.

Tenga en cuenta que la altura del encabezado y el pie de página no se detecta automáticamente, lo que significa que pueden superponerse con el contenido HTML principal. Personalice los márgenes para asegurar que el encabezado y el pie de página estén colocados adecuadamente.

Para más detalles sobre cómo implementar encabezados, pies de página o para explorar otras características, visite el Sitio Web Oficial de IronPDF.

using IronPdf;

class Program
{
    static void Main()
    {
        // Create a Renderer object for PDF creation
        var renderer = new HtmlToPdf();

        // Define the header content including an HTML fragment
        string htmlHeader = "<div style='width:100%; border-bottom:1px solid black; text-align:center;'>Header Content</div>";

        // Define the footer content including an HTML fragment
        string htmlFooter = "<div style='width:100%; border-top:1px solid black; text-align:center;'>Footer Content</div>";

        // Configure the header and footer with desired heights
        renderer.PrintOptions.Header = new SimpleHeaderFooter()
        {
            HtmlFragment = htmlHeader,
            MaxHeight = 50  // Set the maximum height of the header
        };

        renderer.PrintOptions.Footer = new SimpleHeaderFooter()
        {
            HtmlFragment = htmlFooter,
            MaxHeight = 50  // Set the maximum height of the footer
        };

        // Customize page margins to prevent overlap of the header/footer with content
        renderer.PrintOptions.MarginTop = 60;   // Margin to accommodate the header
        renderer.PrintOptions.MarginBottom = 60; // Margin to accommodate the footer

        // Render HTML to PDF
        var pdf = renderer.RenderHtmlAsPdf("<h1>Main Content</h1><p>This is some example content.</p>");

        // Save the PDF file
        pdf.SaveAs("output.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Create a Renderer object for PDF creation
        var renderer = new HtmlToPdf();

        // Define the header content including an HTML fragment
        string htmlHeader = "<div style='width:100%; border-bottom:1px solid black; text-align:center;'>Header Content</div>";

        // Define the footer content including an HTML fragment
        string htmlFooter = "<div style='width:100%; border-top:1px solid black; text-align:center;'>Footer Content</div>";

        // Configure the header and footer with desired heights
        renderer.PrintOptions.Header = new SimpleHeaderFooter()
        {
            HtmlFragment = htmlHeader,
            MaxHeight = 50  // Set the maximum height of the header
        };

        renderer.PrintOptions.Footer = new SimpleHeaderFooter()
        {
            HtmlFragment = htmlFooter,
            MaxHeight = 50  // Set the maximum height of the footer
        };

        // Customize page margins to prevent overlap of the header/footer with content
        renderer.PrintOptions.MarginTop = 60;   // Margin to accommodate the header
        renderer.PrintOptions.MarginBottom = 60; // Margin to accommodate the footer

        // Render HTML to PDF
        var pdf = renderer.RenderHtmlAsPdf("<h1>Main Content</h1><p>This is some example content.</p>");

        // Save the PDF file
        pdf.SaveAs("output.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Create a Renderer object for PDF creation
		Dim renderer = New HtmlToPdf()

		' Define the header content including an HTML fragment
		Dim htmlHeader As String = "<div style='width:100%; border-bottom:1px solid black; text-align:center;'>Header Content</div>"

		' Define the footer content including an HTML fragment
		Dim htmlFooter As String = "<div style='width:100%; border-top:1px solid black; text-align:center;'>Footer Content</div>"

		' Configure the header and footer with desired heights
		renderer.PrintOptions.Header = New SimpleHeaderFooter() With {
			.HtmlFragment = htmlHeader,
			.MaxHeight = 50
		}

		renderer.PrintOptions.Footer = New SimpleHeaderFooter() With {
			.HtmlFragment = htmlFooter,
			.MaxHeight = 50
		}

		' Customize page margins to prevent overlap of the header/footer with content
		renderer.PrintOptions.MarginTop = 60 ' Margin to accommodate the header
		renderer.PrintOptions.MarginBottom = 60 ' Margin to accommodate the footer

		' Render HTML to PDF
		Dim pdf = renderer.RenderHtmlAsPdf("<h1>Main Content</h1><p>This is some example content.</p>")

		' Save the PDF file
		pdf.SaveAs("output.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explorar el ejemplo de código HTML Headers & Footers en GitHub

¿Listo para empezar?
Versión: 2025.12 recién lanzado