添加 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 剛剛發布