一、背景与需求在文档管理场景中为PDF文件添加页码是一项常见需求。无论是财务报表、项目文档还是合同协议规范的页码都能显著提升文档的可读性和专业性。手动为每页添加页码不仅效率低下还容易出错。对于 .NET 开发者而言利用 C# 实现 PDF 页码的自动化插入是提升文档处理效率的有效途径。本文将介绍如何使用 Free Spire.PDF for .NET 免费库通过 C# 码为现有PDF文档批量添加页码。二、环境准备2.1 安装方式Free Spire.PDF for .NET 可通过NuGet直接安装dotnet add package FreeSpire.PDF或在Visual Studio的包管理器控制台中执行Install-Package FreeSpire.PDF2.2 命名空间引用代码中需要引入以下命名空间using Spire.Pdf;using Spire.Pdf.Graphics;using Spire.Pdf.AutomaticFields;using System.Drawing;三、核心实现动态字段方式利用 PdfPageNumberField、PdfPageCountField 和 PdfCompositeField 三个类组合实现页码的自动生成与渲染。PdfPageNumberField显示当前页码PdfPageCountField显示文档总页数PdfCompositeField将多个动态字段组合为一个复合字段以下是完整的实现代码using Spire.Pdf;using Spire.Pdf.AutomaticFields;using Spire.Pdf.Graphics;using System.Drawing;namespace AddPageNumbers{class Program{static void Main(string[] args){// 1. 加载PDF文档PdfDocument pdf new PdfDocument();pdf.LoadFromFile(“示例文档.pdf”);// 2. 创建字体建议使用系统中存在的通用字体 PdfTrueTypeFont font new PdfTrueTypeFont(宋体, 10f, PdfFontStyle.Regular, true); // 3. 创建页码字段和总页数字段 PdfPageNumberField pageNumber new PdfPageNumberField(); PdfPageCountField pageCount new PdfPageCountField(); // 4. 组合字段格式为第X页共Y页支持中文 PdfCompositeField compositeField new PdfCompositeField( font, PdfBrushes.Black, 第 {0} 页 / 共 {1} 页, pageNumber, pageCount); // 5. 设置页码位置基于第一页尺寸通常各页尺寸一致 // 定位到页面底部居中靠左距左边距72点1英寸距底边45点 float leftMargin 72f; float bottomMargin 45f; SizeF pageSize pdf.Pages[0].Size; // 获取基准页面尺寸 compositeField.Location new PointF(leftMargin, pageSize.Height - bottomMargin); // 6. 遍历每一页在页面底部绘制页码 for (int i 0; i pdf.Pages.Count; i) { PdfPageBase page pdf.Pages[i]; // 使用无参 Draw自动应用 Location compositeField.Draw(page.Canvas); } // 7. 保存文档 pdf.SaveToFile(添加页码后的文档.pdf); pdf.Close(); pdf.Dispose(); } }}四、高级定制4.1 页码格式自定义通过修改 PdfCompositeField 的格式化字符串可以灵活调整页码的显示格式// 中文格式“第 {0} 页 / 共 {1} 页”// 英文格式“Page {0} of {1}”// 简单格式“{0}/{1}”4.2 页码位置控制页码的位置可通过 Location 属性精确控制。示例1底部居中// 需要先测量文本宽度再计算居中坐标string sampleText “第 999 页 / 共 999 页”; // 最宽情况SizeF textSize font.MeasureString(sampleText);float x (pageSize.Width - textSize.Width) / 2;float y pageSize.Height - bottomMargin;compositeField.Location new PointF(x, y);示例2底部右侧float rightMargin 72f;string sampleText “第 999 页 / 共 999 页”;SizeF textSize font.MeasureString(sampleText);float x pageSize.Width - textSize.Width - rightMargin;float y pageSize.Height - bottomMargin;compositeField.Location new PointF(x, y);示例3页眉居中float topMargin 30f;string sampleText “第 999 页 / 共 999 页”;SizeF textSize font.MeasureString(sampleText);float x (pageSize.Width - textSize.Width) / 2;float y topMargin;compositeField.Location new PointF(x, y);4.3 页码样式调整字体、颜色、大小等样式均可自定义// 设置字体为 Arial、12号、粗体PdfTrueTypeFont font new PdfTrueTypeFont(“Arial”, 12f, PdfFontStyle.Bold, true);// 设置颜色为蓝色PdfBrush brush PdfBrushes.Blue;4.4 处理不同尺寸的页面如果PDF中各页面尺寸不一致应逐页获取尺寸并计算位置for (int i 0; i pdf.Pages.Count; i){PdfPageBase page pdf.Pages[i];SizeF pageSize page.Size;// 根据当前页尺寸计算位置float x (pageSize.Width - textWidth) / 2;float y pageSize.Height - bottomMargin;compositeField.Location new PointF(x, y);compositeField.Draw(page.Canvas);}五、注意事项免费版限制Free Spire.PDF 社区版对处理页数有一定限制通常为10页以内适用于小型文档。字体支持使用 PdfTrueTypeFont 时需要确保指定的字体在系统中可用否则可能出现渲染异常。坐标系统页面画布的坐标系原点位于左上角X轴向右为正Y轴向下为正。使用 Location 属性时默认以文本的左上角为定位点。因此Location.Y 值越大文本位置越靠下。本文示例中通过 pageSize.Height - bottomMargin 计算底部位置正是基于这一坐标系。文本宽度测量当需要居中或右对齐时使用 font.MeasureString() 获取文本宽度建议以最长的页码格式如“第 999 页 / 共 999 页”为基准进行测量避免因页码位数变化导致位置偏移。文档保存建议使用 SaveToFile 方法保存为新文件避免覆盖原始文档造成数据丢失。资源释放处理完成后调用 Dispose() 方法释放文档占用的资源确保内存安全。