新增 HTML 標題與頁尾

配置渲染選項,以便在使用 IronPDF(Iron Software 的一個庫,允許高級 PDF 生成和操作)渲染 PDF 文件時包含 HTML 標題和頁腳。

使用 IronPdf 靈活的 PDF 渲染工具,以分割線、HTML 片段定義標題內容,並指定標題的最大高度。 同樣地,使用 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.12 剛發表