如何在 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将仅应用于奇数页。 页面索引从零开始,而页面编号则从一开始。

: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#

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