如何使用 C# 在 PDF 中添加页码
IronPDF让您可以在C#中使用占位符字符串{total-pages}将页码添加到PDF中页眉/页脚中,并可以选择将它们应用于特定页面或部分,以增强文档导航。
页码是分配给 PDF 文档中每一页的顺序号。 它们是导航的重要组成部分,可以帮助读者找到特定页面并跟踪其位置。 页码也便于内容的引用和参考。 有了 IronPDF,为 PDF 添加页码就变得简单易行。
快速入门:轻松为 PDF 添加页码
using IronPDF 为 PDF 文档添加页码。 只需使用最少的 C# 代码,您就可以在页眉或页脚中插入动态页码,自定义其位置,并指定其出现在哪些页面上。 无论是处理新的 PDF 还是现有的 PDF,IronPDF 都能提供灵活的解决方案来增强文档导航和组织。
-
1Install IronPDF with NuGet Package Manager
-
2复制并运行这段代码。
new IronPdf.ChromePdfRenderer { RenderingOptions = { HtmlFooter = new IronPdf.HtmlHeaderFooter { HtmlFragment = "<center>{page}of{total-pages}</center>", DrawDividerLine = true } } } .RenderHtmlAsPdf("<h1>My multi-page document</h1><div style='page-break-after:always;'></div><h1>Page 2</h1>") .SaveAs("numbered-pages.pdf");C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPDF
最小工作流程(5 个步骤)
- 从 NuGet 下载 C# PDF 库。
- 加载现有的 PDF 文档或渲染新的文档
- 在页眉和页脚中使用占位符字符串
{page}和{total-pages}插入页码 - 将页码应用于特定页面或部分
- 查看生成的 PDF 文档
如何在 PDF 中添加基本页码?
使用占位符字符串HtmlHeaderFooter类一起,您可以添加当前页码和总页数。 在创建新的 PDF 或将需要专业格式的 HTML 转换为 PDF 文档时,该功能至关重要。
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")上面的代码输出的PDF如下所示:
您还可以将包含页码占位符字符串的页眉和页脚直接添加到ChromePdfRenderer的渲染选项中。 在使用渲染选项定制 PDF 输出时,这种方法非常有用。
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")在处理复杂文档时,请考虑分页符和自定义页边距,以确保正确显示页码。
如何仅为特定页面添加页码?
using IronPDF,您可以控制页码的显示位置。 使其从特定页面开始,或将其应用于特定组,如偶数索引页面。 这种灵活性在 生成 PDF 报告或处理包含不同部分的文档时非常有价值。
首先,准备 PDF 文档,应用页码:
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)如何仅对偶数页进行编号?
在前一个示例的基础上,该代码将页码专门应用于偶数页索引。 由于筛选适用于偶数页索引,因此生成的 PDF 将在奇数页码上显示数字。 页面索引从零开始,而页码从一开始。 这种技术适合遵循传统书籍格式的文档,或者在处理 合并 PDF 文档时使用。
// 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")如何仅对奇数页进行编号?
为索引号为奇数的页面添加页码。 这种方法在双面打印的情况下很常见,因为页码只能出现在打印文档的一面。
// 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")如何仅在最后一页添加页码?
仅在最后一页添加页码。 这对于摘要页面或表示文档结束时非常有用。
// 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")如何仅在第一页添加页码?
仅在第一页添加页码。 这对于封面页或只有第一页需要标识的文件非常有效。
// 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")添加页码时如何跳过第一页?
应用页眉时跳过第一页。 这一常见要求适用于不显示页码的封面或扉页文件。 在处理 IronPDF 表格或包括求职信在内的文档时,该技术可确保格式的专业性。
// 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")如何跳过第一页并在第二页从 1 开始编号?
跳过第一页,从第二页开始编号,将其视为第 1 页。这种方法适用于封面页不计入页码顺序的文件,如学术论文或正式报告。
// 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")要了解所有元数据选项,请访问 IronPDF 页眉和页脚指南。
高级页面编号技术
除了基本的页码,IronPDF 还支持包括以下内容的高级方案:
- 基于节的编号:不同文档部分的不同编号方案
- 罗马数字:自定义格式化序言或附录页
- 多重编号系统: 在单个文档中结合不同的编号格式
- 动态页面计数:在添加或复制页面时更新总页数
在创建复杂的文档(如技术手册、学术论文或需要复杂页码方案的法律文件)时,这些技术非常有价值。
页码的最佳实践
在 PDF 中使用页码时,请考虑以下做法:
- 一致性: 在整个文档中保持一致的定位和格式化
- 可读性: 确保页码清晰可见且不要与内容重叠
- 可访问性: 使用适当的字体大小和对比度以便于阅读 4.专业外观:使页码风格与文档的整体设计相匹配
遵循这些准则并利用 IronPDF 灵活的页码编排功能,您就能创建出专业的、条理清晰的 PDF 文档,满足您的特定要求。
常见问题解答
如何在 C# 中为 PDF 添加基本页码?
using IronPDF,您可以在 TextHeaderFooter 或 HtmlHeaderFooter 类中使用占位符字符串 {page} 和 {total-pages} 添加页码。只需在页眉或页脚 HTML 片段中包含这些占位符,IronPDF 就会在渲染 PDF 时自动将其替换为当前页码和总页数。
能否通过呈现选项直接添加页码?
是的,您可以直接在 IronPDF 的 ChromePdfRenderer 的渲染选项中添加页码。这种方法允许您将页码占位符字符串作为渲染配置的一部分来设置页眉和页脚,这在通过渲染选项自定义 PDF 输出时特别有用。
有哪些占位字符串可用于页码?
IronPDF 为页码提供了两个主要占位字符串:{page} 显示当前页码,而 {total-pages} 则显示文档的总页数。这些占位符会在渲染 PDF 时自动替换为实际值。
我可以只在特定页面上使用页码吗?
是的,IronPDF 允许您控制页码在文档中的显示位置。您可以指定哪些页面应显示页码,这样就可以将页码排除在封面页之外,或仅在 PDF 的某些部分应用页码。
是否可以自定义页码的位置和样式?
当然可以。通过 IronPDF 的 HtmlHeaderFooter 类,您可以使用 HTML 和 CSS 自定义页码的位置、样式和格式。您可以控制页码的位置、字体、大小、颜色,甚至添加分隔线或其他装饰元素。
能否在现有 PDF 文档中添加页码?
是的,IronPDF 支持在新 PDF 和现有 PDF 文档中添加页码。您可以加载现有的 PDF 文档并应用带有页码的页眉或页脚,而无需从头开始创建整个文档。
Can I add page numbers to only even or odd pages in a PDF with IronPDF?
Yes, IronPDF enables you to filter and apply page numbers exclusively to even or odd page indexes, which is ideal for documents needing traditional book formatting.
How can I skip the first page when adding page numbers with IronPDF?
To skip the first page, you can simply adjust the page range indices and apply page numbers starting from the second page onward using IronPDF.
Can IronPDF handle custom page numbering formats like Roman numerals?
Yes, IronPDF can handle various custom formatting options, including Roman numerals, which can be useful for sections like prefaces or appendices.
What are the best practices for implementing page numbers in PDFs using IronPDF?
Ensure consistency in positioning and formatting, maintain readability with clear visibility, use appropriate font sizes for accessibility, and match the style with the document's design for a professional look.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。