如何在 PDF 中添加页码

查克尼特·宾
查克尼特·宾
2023年八月8日
更新 2024年十二月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

页面编号是分配给PDF文档中每个单独页面的连续数字。 页面编号是文档导航中最重要的组件之一。 他们帮助读者找到特定的页面,并确定他们正在阅读的是哪一页。 此外,页码便于在文档中引用和参考内容。 使用IronPDF,为您的PDF添加页码是一个简单的过程。

开始使用IronPDF

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

第一步:
green arrow pointer



添加页码示例

使用占位符字符串 {page} 和 {total-pages},通过 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")
$vbLabelText   $csharpLabel

上述代码输出的 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")
$vbLabelText   $csharpLabel

向特定页面添加页码示例

使用 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)
$vbLabelText   $csharpLabel

偶数页索引

在之前的代码示例的基础上,以下代码将只对偶数页索引应用页码。 由于我们正在筛选偶数页索引,因此生成的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")
$vbLabelText   $csharpLabel

奇数页索引

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

: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")
$vbLabelText   $csharpLabel

仅最后一页

仅在最后一页添加页码。

: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")
$vbLabelText   $csharpLabel

仅首页

仅在第一页添加页码。

: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")
$vbLabelText   $csharpLabel

跳过第一页

在应用页眉时跳过第一页。

: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")
$vbLabelText   $csharpLabel

跳过第一页,不计其数

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

: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")
$vbLabelText   $csharpLabel

要探索所有元数据选项,请访问IronPDF 页眉和页脚指南

查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。