添加經典文本頁眉和頁腳

了解如何在呈現HTML內容時向PDF文檔添加文本頁眉和頁腳。

要包含文本頁眉和頁腳,您需要在呈現選項對象中指定它們。 用分隔線、顯示HTML標題的居中文本、字體設置和字體大小來定義頁眉內容。同樣,使用textFooter屬性定義頁腳。 您可以利用像{page}{total-pages}{url}{date}{time}{html-title}{pdf-title}等字段來個性化內容。

自定義頁邊距以適當放置頁眉和頁腳。

使用PdfDocument.fromHtml方法將您的HTML內容轉換為PDF。 將renderOptions對象作為選項傳遞。

有關從HTML創建PDF的詳細文檔,請訪問IronPDF Documentat​​ion

生成的PDF文檔,帶有完整的頁眉和頁腳,保存為"header_footer.pdf"。

// Import the necessary namespace
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your HTML content
        string htmlContent = @"<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>";

        // Create the rendering options for the PDF
        var renderOptions = new PdfPrintOptions()
        {
            // Specify header settings with fields
            Header = new SimpleHeaderFooter()
            {
                CenterText = "{pdf-title}",
                FontFamily = "Arial",
                FontSize = 14,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            // Specify footer settings with fields
            Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                FontFamily = "Arial",
                FontSize = 12,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            MarginTop = 50, // Adjust top margin for header
            MarginBottom = 50 // Adjust bottom margin for footer
        };

        // Create a PDF document from HTML with the specified options
        var pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions);

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("header_footer.pdf");
    }
}
// Import the necessary namespace
using IronPdf;

class Program
{
    static void Main()
    {
        // Define your HTML content
        string htmlContent = @"<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>";

        // Create the rendering options for the PDF
        var renderOptions = new PdfPrintOptions()
        {
            // Specify header settings with fields
            Header = new SimpleHeaderFooter()
            {
                CenterText = "{pdf-title}",
                FontFamily = "Arial",
                FontSize = 14,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            // Specify footer settings with fields
            Footer = new SimpleHeaderFooter()
            {
                CenterText = "Page {page} of {total-pages}",
                FontFamily = "Arial",
                FontSize = 12,
                DrawDividerLine = true,
                Height = 50 // Customize height based on needs
            },
            MarginTop = 50, // Adjust top margin for header
            MarginBottom = 50 // Adjust bottom margin for footer
        };

        // Create a PDF document from HTML with the specified options
        var pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions);

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("header_footer.pdf");
    }
}
' Import the necessary namespace
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Define your HTML content
		Dim htmlContent As String = "<html><body><h1>Hello, World!</h1><p>This is a PDF document with headers and footers.</p></body></html>"

		' Create the rendering options for the PDF
		Dim renderOptions = New PdfPrintOptions() With {
			.Header = New SimpleHeaderFooter() With {
				.CenterText = "{pdf-title}",
				.FontFamily = "Arial",
				.FontSize = 14,
				.DrawDividerLine = True,
				.Height = 50
			},
			.Footer = New SimpleHeaderFooter() With {
				.CenterText = "Page {page} of {total-pages}",
				.FontFamily = "Arial",
				.FontSize = 12,
				.DrawDividerLine = True,
				.Height = 50
			},
			.MarginTop = 50,
			.MarginBottom = 50
		}

		' Create a PDF document from HTML with the specified options
		Dim pdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions)

		' Save the PDF document with headers and footers
		pdfDocument.SaveAs("header_footer.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

在GitHub上探索頁眉和頁腳示例代碼

準備好開始了嗎?
版本: 2025.11 剛剛發布