添加HTML页眉和页脚

配置渲染选项以在使用IronPDF进行PDF文档渲染时包含HTML页眉和页脚。IronPDF是Iron Software推出的库,允许进行高级PDF生成和操作。

通过分隔线和HTML片段定义页眉内容,并使用IronPDF灵活的PDF渲染工具指定页眉的最大高度。 类似地,使用IronPDF提供的htmlHeader属性定义页脚内容。

请注意,页眉和页脚的高度不会自动检测,这意味着它们可能会与主要HTML内容重叠。 自定义边距以确保页眉和页脚适当定位。

有关如何实现页眉、页脚或探索其他功能的更多详细信息,请访问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

在GitHub上探索HTML页眉和页脚代码示例

准备开始了吗?
版本: 2025.11 刚刚发布