如何添加页眉和页脚

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

乔尔迪·巴尔迪亚

需要在PDF文档的每一页顶部或底部添加页码、公司标志或日期吗? 通过添加页眉和页脚,您可以! 使用IronPDF,在您的C#项目中为PDF应用页眉和页脚非常简单。



适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安装和部署。已经超过800万次下载,它正在用C#改变PDF。

适用于PDF的C# NuGet库 nuget.org/packages/IronPdf/
Install-Package IronPdf

考虑安装 IronPDF DLL 直接。下载并手动安装到您的项目或GAC表单中: IronPdf.zip

Dll Img related to 如何添加页眉和页脚

手动安装到你的项目中

下载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#

输出文本标题

文本标题

您可以在 IronPDF 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将相应地重新定位。 请使用以下代码来检查该功能:

: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-title}在HTML中,title 标签指定了HTML标题。
  • {pdf标题}:在PDF元数据中指定的PDF标题。

    要了解更多关于{页码}和{总页数}请访问IronPDF 页码指南指南。

: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 related to 选择文本和HTML页眉/页脚之间的区别

乔尔迪·巴尔迪亚

软件工程师

Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 运用技能时,他会进行游戏编程。作为产品测试、产品开发和研究的负责人之一,Jordi 为持续的产品改进增添了极大的价值。多样化的经验让他充满挑战和参与感,他说这是他在 Iron Software 工作中最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。