如何添加頁眉和頁腳

This article was translated from English: Does it need improvement?
Translated
View the article in English

喬迪·巴迪亞

需要在 PDF 文件的每一頁中的頂部或底部包含頁碼、公司標誌或日期嗎?使用頁眉和頁腳,您可以做到! 使用 IronPDF,在您的 C# 專案中應用 PDF 的頁眉和頁尾是非常簡單的。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

添加文本頁首/頁尾範例

要僅使用文字來建立頁首/頁尾,請實例化一個 TextHeaderFooter 對象,添加所需的文本,然後將該對象添加到您的 PDF 中。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-add-textheaderfooter.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

// Create text footer
TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader);
pdf.AddTextFooters(textFooter);

pdf.SaveAs("addTextHeaderFooter.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

或者,您可以直接使用渲染器的渲染選項添加頁首/頁尾。在渲染過程中,這將添加文本頁首和頁尾。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-textheaderfooter.cs
using IronPdf;

// Instantiate renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create header and add to rendering options
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};


// Create footer and add to rendering options
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Render PDF with header and footer
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
pdf.SaveAs("renderWithTextHeaderFooter.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

自訂文字和分隔線屬性

TextHeaderFooter 類中,您可以設置左、中、右位置的文本。此外,您還可以自訂文字的字體類型和大小,並透過配置相關屬性來添加具有自訂顏色的分隔線。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-options.cs
using IronPdf;
using IronPdf.Font;
using IronSoftware.Drawing;

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "Center text", // Set the text in the center
    LeftText = "Left text", // Set left-hand side text
    RightText = "Right text", // Set right-hand side text
    Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic, // Set font
    FontSize = 16, // Set font size
    DrawDividerLine = true, // Draw Divider Line
    DrawDividerLineColor = Color.Red, // Set color of divider line
};
Imports IronPdf
Imports IronPdf.Font
Imports IronSoftware.Drawing

' Create text header
Private textHeader As New TextHeaderFooter With {
	.CenterText = "Center text",
	.LeftText = "Left text",
	.RightText = "Right text",
	.Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic,
	.FontSize = 16,
	.DrawDividerLine = True,
	.DrawDividerLineColor = Color.Red
}
VB   C#

輸出文本標題

文本標題

您可以查看預設提供的字體類型 API 參考文獻.

設置文本頁眉/頁腳邊距

默認情況下,IronPDF中的文本頁眉和頁腳具有預設的邊距。如果您希望文本頁眉橫跨PDF文件的整個寬度,可以將邊距值指定為0。這可以通過直接在 AddTextHeadersAddTextFooters 函數中設置邊距,或者通過 ChromePdfRenderer 中的 RenderingOptions 來實現。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

pdf.AddTextHeaders(header, 35, 30, 25); // Left Margin = 35, Right Margin  = 30, Top Margin = 25
pdf.AddTextFooters(footer, 35, 30, 25); // Margin values are in mm
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

如果您在 ChromePdfRendererRenderingOptions 中添加邊距值,這些邊距值也會應用於頁眉和頁腳。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-rendering-options-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Margin values are in mm
renderer.RenderingOptions.MarginRight = 30;
renderer.RenderingOptions.MarginLeft = 30;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All;

// Add header and footer to renderer
renderer.RenderingOptions.TextHeader = header;
renderer.RenderingOptions.TextFooter = footer;

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

動態邊距調整

靜態邊距在標頭內容因文檔不同而變化時會引發問題。不僅需要調整標頭和頁尾邊距,還需要調整主 HTML 邊距以適應不同的標頭和頁尾大小。因此,我們實施了一個動態邊距調整功能,其中標頭和頁尾的高度將根據內容動態調整,主 HTML 也將相應地重新定位。請使用以下代碼查看此功能:

// 示例代碼
// 示例代碼
HTML
:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-dynamic-marigns.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = @"<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",
    // Enable the dynamic height feature
    MaxHeight = HtmlHeaderFooter.FragmentHeight,
};

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");
pdf.SaveAs("dynamicHeaderSize.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
	.HtmlFragment = "<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",
	.MaxHeight = HtmlHeaderFooter.FragmentHeight
}

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")
pdf.SaveAs("dynamicHeaderSize.pdf")
VB   C#

元資料到文字標題/頁尾

您可以輕鬆地通過在文本中加入佔位符字符串來添加頁碼、日期和 PDF 標題等元資料。以下是所有可用的元資料選項:

  • {頁面}:當前頁碼。 {總頁數}: 總頁數。
  • {網址}: 從其中渲染 PDF 文件的網頁 URL。
  • {日期}:當前日期。
  • {時間}: 當前時間。

  • {html-標題}: HTML 標題在 HTML 中的 title 標籤中指定。 {pdf-title}PDF元數據中指定的PDF標題。

瞭解更多有關 {頁面} 和 {總頁數},請訪問 添加頁碼 操作指南

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-mail-merge.cs
using IronPdf;

// Create header and footer
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "{page} of {total-pages}",
    LeftText = "Today's date: {date}",
    RightText = "The time: {time}",
};

TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "Current URL: {url}",
    LeftText = "Title of the HTML: {html-title}",
    RightText = "Title of the PDF: {pdf-title}",
};
Imports IronPdf

' Create header and footer
Private textHeader As New TextHeaderFooter With {
	.CenterText = "{page} of {total-pages}",
	.LeftText = "Today's date: {date}",
	.RightText = "The time: {time}"
}

Private textFooter As New TextHeaderFooter With {
	.CenterText = "Current URL: {url}",
	.LeftText = "Title of the HTML: {html-title}",
	.RightText = "Title of the PDF: {pdf-title}"
}
VB   C#

添加 HTML 頁首/頁尾範例

您可以通過利用 HTML 和 CSS 進一步自定義您的頁首/頁尾。要創建 HTML 頁首/頁尾,請使用 HtmlHeaderFooter 類。如果您希望保留 CSS 樣式表中的 CSS 樣式,請確保在類屬性中設置 LoadStylesAndCSSFromMainHtmlDocument = true

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create header and footer
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Add to PDF
pdf.AddHtmlHeaders(htmlHeader);
pdf.AddHtmlFooters(htmlFooter);
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Create header and footer
Private htmlHeader As New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

Private htmlFooter As New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

' Add to PDF
pdf.AddHtmlHeaders(htmlHeader)
pdf.AddHtmlFooters(htmlFooter)
VB   C#

類似於文本的頁首和頁尾,上述的 AddHtmlHeadersAddHtmlFooters 方法已應用預定義的邊距。要應用自定義邊距,請使用具有指定邊距值的重載函數。若要無邊距地跨越整個內容,請在重載函數中將邊距設置為0。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter-margins.cs
// Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0);
pdf.AddHtmlFooters(footer, 0, 0, 0);
' Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0)
pdf.AddHtmlFooters(footer, 0, 0, 0)
VB   C#

還可以通過渲染器的渲染選項直接添加頁首和頁尾。這將在渲染過程中添加 HTML 頁首和頁尾。

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create header and footer and add to rendering options
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Render PDF with header and footer
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer
Private renderer As New ChromePdfRenderer()

' Create header and footer and add to rendering options
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

' Render PDF with header and footer
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
VB   C#

在文字與 HTML 頁眉/頁腳之間進行選擇

在決定使用文字或 HTML 頁眉/頁腳時,需考慮其權衡。如果您優先考慮更快的 PDF 呈現速度,請選擇文字頁眉/頁腳。另一方面,如果自定義和樣式對您來說至關重要,則選擇 HTML 頁眉/頁腳。當 HTML 頁眉/頁腳的內容不多時,文字與 HTML 頁眉/頁腳之間的渲染時間差異並不大。然而,隨著 HTML 頁眉/頁腳中資產的大小和數量增加,渲染時間也會隨之增加。

喬迪·巴迪亞

軟體工程師

Jordi 最擅長 Python、C# 和 C++,當他不在 Iron Software 發揮技能時,他會進行遊戲編程。他負責產品測試、產品開發和研究,為持續產品改進增添了巨大的價值。多樣化的經驗使他感到挑戰和投入,他說這是與 Iron Software 合作的最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,並在佛羅里達大學學習計算機科學和統計學。