如何在 PDF 中添加页码

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

约尔迪

页码是分配给 PDF 文档中每一页的顺序号。页码是文档导航中最重要的组成部分之一。页码可以帮助读者找到特定的页面,并确定他们是从哪一页开始阅读的。此外,页码还便于引用和参考文档中的内容。使用 IronPDF,在 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

添加页码示例

使用占位符字符串 {页码} 和 {总页数} 使用 TextHeaderFooterHtmlHeaderFooter 类,可以添加当前页码和总页数。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-basic.cs
using IronPdf;

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter()
{
    CenterText = "{page} of {total-pages}"
};

// Create html footer
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

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

// Add header and footer
pdf.AddTextHeaders(textHeader);
pdf.AddHtmlFooters(htmlFooter);

pdf.SaveAs("pdfWithPageNumber.pdf");
Imports IronPdf

' Create text header
Private textHeader As New TextHeaderFooter() With {.CenterText = "{page} of {total-pages}"}

' Create html footer
Private htmlFooter As New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

' Render a new PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Add header and footer
pdf.AddTextHeaders(textHeader)
pdf.AddHtmlFooters(htmlFooter)

pdf.SaveAs("pdfWithPageNumber.pdf")
VB   C#

上述代码输出的 PDF 文件如下所示:

您也可以直接将页眉和页脚以及页码占位符字符串添加到ChromePdfRenderer的渲染选项中。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-chromerenderer.cs
using IronPdf;

// Add header and footer to rendering options
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
    CenterText = "{page} of {total-pages}"
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

string html = @"
    <h1>Hello World!</h1>
<div style='page-break-after: always;'/>
    <h1>2nd Page!</h1>";

// Render new PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

pdf.SaveAs("applyPageNumberWithRenderingOptions.pdf");
Imports IronPdf

' Add header and footer to rendering options
Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {.CenterText = "{page} of {total-pages}"}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

Dim html As String = "
    <h1>Hello World!</h1>
<div style='page-break-after: always;'/>
    <h1>2nd Page!</h1>"

' Render new PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

pdf.SaveAs("applyPageNumberWithRenderingOptions.pdf")
VB   C#

为特定页面添加页码 示例

使用 IronPDF,您可以决定在何处添加页码。您可以让页码从某一页开始,也可以让页码从某些页面组开始,例如偶数索引页面。

让我们准备好 PDF 文档,以便应用页码。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-preparation.cs
using IronPdf;
using System.Linq;
using System.Collections.Generic;

string multi_page_html = @"
    <p>This is the 1st Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 4th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 5th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 6th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 7th Page</p>";

// Create header
HtmlHeaderFooter header = new HtmlHeaderFooter()
{
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"
};

// Render PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(multi_page_html);

// Create a Page Range 0..6
var allPageIndices = Enumerable.Range(0, pdf.PageCount);
Imports IronPdf
Imports System.Linq
Imports System.Collections.Generic

Private multi_page_html As String = "
    <p>This is the 1st Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 2nd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 3rd Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 4th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 5th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 6th Page</p>
<div style = 'page-break-after: always;' ></div>
    <p>This is the 7th Page</p>"

' Create header
Private header As New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>"}

' Render PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(multi_page_html)

' Create a Page Range 0..6
Private allPageIndices = Enumerable.Range(0, pdf.PageCount)
VB   C#

偶数页索引

在前面代码示例的基础上,下面的代码将只在偶数页索引中应用页码。由于我们筛选的是偶数页索引,因此生成的 PDF 只适用于奇数页码。页面索引从 0 开始,而页码从 1 开始。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-even-pages.cs
// Get even page indexes (resulting in odd page numbers)
var evenPageIndices = allPageIndices.Where(i => i % 2 == 0);

pdf.AddHtmlHeaders(header, 1, evenPageIndices);
pdf.SaveAs("EvenPages.pdf");
' Get even page indexes (resulting in odd page numbers)
Dim evenPageIndices = allPageIndices.Where(Function(i) i Mod 2 = 0)

pdf.AddHtmlHeaders(header, 1, evenPageIndices)
pdf.SaveAs("EvenPages.pdf")
VB   C#

奇数页索引

专门为索引号为奇数的页面添加页码。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-odd-pages.cs
// Get odd page indexes (resulting in even page numbers)
var oddPageIndexes = allPageIndices.Where(i => i % 2 != 0);

pdf.AddHtmlHeaders(header, 1, oddPageIndexes);
pdf.SaveAs("OddPages.pdf");
' Get odd page indexes (resulting in even page numbers)
Dim oddPageIndexes = allPageIndices.Where(Function(i) i Mod 2 <> 0)

pdf.AddHtmlHeaders(header, 1, oddPageIndexes)
pdf.SaveAs("OddPages.pdf")
VB   C#

仅限最后一页

仅在最后一页添加页码。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-last-page-only.cs
// Last page only
var lastPageIndex = new List<int>() { pdf.PageCount - 1 };

pdf.AddHtmlHeaders(header, 1, lastPageIndex);
pdf.SaveAs("LastPageOnly.pdf");
' Last page only
Dim lastPageIndex = New List(Of Integer)() From {pdf.PageCount - 1}

pdf.AddHtmlHeaders(header, 1, lastPageIndex)
pdf.SaveAs("LastPageOnly.pdf")
VB   C#

仅第一页

仅在第一页添加页码。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-first-page-only.cs
// First page only
var firstPageIndex = new List<int>() { 0 };

pdf.AddHtmlHeaders(header, 1, firstPageIndex);
pdf.SaveAs("FirstPageOnly.pdf");
' First page only
Dim firstPageIndex = New List(Of Integer)() From {0}

pdf.AddHtmlHeaders(header, 1, firstPageIndex)
pdf.SaveAs("FirstPageOnly.pdf")
VB   C#

跳过第一页

应用页眉时跳过第一页。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-skip-first-page.cs
// Skip the first page
var skipFirstPage = allPageIndices.Skip(1);

pdf.AddHtmlHeaders(header, 1, skipFirstPage);
pdf.SaveAs("SkipFirstPage.pdf");
' Skip the first page
Dim skipFirstPage = allPageIndices.Skip(1)

pdf.AddHtmlHeaders(header, 1, skipFirstPage)
pdf.SaveAs("SkipFirstPage.pdf")
VB   C#

跳过第一页,不计其数

跳过第一页,从第二页开始编号,将其视为第一页。

:path=/static-assets/pdf/content-code-examples/how-to/page-numbers-skip-first-page-and-dont-count-it.cs
// Skip the first page and start numbering the second page as page 1
var skipFirstPageAndDontCountIt = allPageIndices.Skip(1);

pdf.AddHtmlHeaders(header, 0, skipFirstPageAndDontCountIt);
pdf.SaveAs("SkipFirstPageAndDontCountIt.pdf");
' Skip the first page and start numbering the second page as page 1
Dim skipFirstPageAndDontCountIt = allPageIndices.Skip(1)

pdf.AddHtmlHeaders(header, 0, skipFirstPageAndDontCountIt)
pdf.SaveAs("SkipFirstPageAndDontCountIt.pdf")
VB   C#

要了解所有元数据选项,请访问 添加页眉和页脚 指南。