使用C#代码在PDF文档添加页码的操作指南

 更新时间:2025年11月23日 11:55:59   作者:2501_93070778  
向 PDF 文档添加页码不仅实用,而且具有美观效果,因为它能使文档呈现出类似专业出版物的精致外观,在本文中,您将学习如何在 C# 中使用 Spire.PDF for .NET 向 PDF 文档添加页码,需要的朋友可以参考下

向 PDF 文档添加页码不仅实用,而且具有美观效果,因为它能使文档呈现出类似专业出版物的精致外观。无论您处理的是小说的电子版、报告,还是其他类型的长篇文档,添加页码都能显著提高其可读性和使用价值。在本文中,您将学习如何在 C# 中使用 Spire.PDF for .NET 向 PDF 文档添加页码。

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过此链接下载,或者通过 NuGet 进行安装。

PM> Install-Package Spire.PDF

PDF 坐标系统

在使用 Spire.PDF for .NET 操作现有 PDF 文档时,需要注意坐标系统的原点位于页面的左上角。x 轴向右延伸,y 轴向下延伸。

通常,页码会放置在文档的页眉或页脚区域。因此,在确定页码的合适位置时,必须考虑页面的尺寸和边距。

在 C# 中在页脚添加左对齐的页码

在 Spire.PDF for .NET 库中,有两个可用的类:PdfPageNumberField 和 PdfPageCountField。将它们添加到 PDF 文档的页面时,可以获取并显示当前页码以及总页数。如果您希望插入类似 “第 X 页” 或 “第 X 页,共 Y 页” 的文本,可以使用 PdfCompositeField 类,它允许您将所需文本与一个或多个字段组合成单一字段。

具体示例代码如下:

using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
 
namespace AddPageNumbersToLeftCorner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 应用许可证密钥
            LicenseProvider.SetLicenseKey("License Key");
 
            // 创建 PdfDocument 对象
            PdfDocument doc = new PdfDocument();
 
            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
 
            // 创建字体、画刷和画笔,用于设置页码的显示样式
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
            PdfBrush brush = PdfBrushes.Black;
            PdfPen pen = new PdfPen(brush, 1.0f);
 
            // 创建 PdfPageNumberField 对象和 PdfPageCountField 对象
            PdfPageNumberField pageNumberField = new PdfPageNumberField();
            PdfPageCountField pageCountField = new PdfPageCountField();
 
            // 创建 PdfCompositeField 对象,将页码字段和总页数字段组合为一个字段
            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
 
            // 获取页面尺寸
            SizeF pageSize = doc.Pages[0].Size;
 
            // 设置复合字段的位置
            compositeField.Location = new PointF(72, pageSize.Height - 45);
 
            // 遍历文档中的每一页
            for (int i = 0; i < doc.Pages.Count; i++)
            {
 
                // 获取指定页面
                PdfPageBase page = doc.Pages[i];
 
                // 在指定位置绘制一条直线
                page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
 
                // 在页面上绘制复合字段
                compositeField.Draw(page.Canvas);
            }
 
            // 保存为新的 PDF 文件
            doc.SaveToFile("AddPageNumbersToLeftCorner.pdf");
 
            // 释放资源
            doc.Dispose();
        }
    }
}

在 C# 中在页脚添加居中对齐的页码

为了将页脚中的页码居中对齐,关键在于动态计算文本 “第 X 页,共 Y 页” 的宽度。这一步计算非常重要,因为它决定了页码(PdfCompositeField)的 X 坐标。为了实现居中对齐,X 坐标的计算方法为:用页面宽度减去页码宽度,然后将结果除以 2,即 (PageWidth - PageNumberWidth) / 2。

具体示例代码如下:

using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
 
namespace AddPageNumbersToCenter
{
    class Program
    {
        static void Main(string[] args)
        {
            // 应用许可证密钥
            LicenseProvider.SetLicenseKey("License Key");
 
            // 创建 PdfDocument 对象
            PdfDocument doc = new PdfDocument();
 
            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
 
            // 创建字体、画刷和画笔,用于设置页码的显示样式
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
            PdfBrush brush = PdfBrushes.Black;
            PdfPen pen = new PdfPen(brush, 1.0f);
 
            // 创建 PdfPageNumberField 对象和 PdfPageCountField 对象
            PdfPageNumberField pageNumberField = new PdfPageNumberField();
            PdfPageCountField pageCountField = new PdfPageCountField();
 
            // 创建 PdfCompositeField 对象,将页码字段和总页数字段组合为一个字段
            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
 
 
            // 遍历文档中的每一页
            for (int i = 0; i < doc.Pages.Count; i++)
            {
 
                // 获取指定页面
                PdfPageBase page = doc.Pages[i];
 
                // 获取页面尺寸
                SizeF pageSize = doc.Pages[i].Size;
 
                // 在指定位置绘制一条直线
                page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
 
                // 测量 "Page X of Y" 文本的尺寸
                SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));
 
                // 设置复合字段的位置,实现居中对齐
                compositeField.Location = new PointF((pageSize.Width - pageNumberSize.Width) / 2, pageSize.Height - 45);
 
                // 在页面上绘制复合字段
                compositeField.Draw(page.Canvas);
 
            }
 
            // 保存为新的 PDF 文件
            doc.SaveToFile("AddPageNumbersToCenter.pdf");
 
            // 释放资源
            doc.Dispose();
        }
    }
}

在 C# 中在页脚添加右对齐的页码

为了将页脚中的页码放置在右侧角落,同样需要动态计算文本 “第 X 页,共 Y 页” 的宽度。因为页码(PdfCompositeField)的 X 坐标是通过用页面宽度减去页码宽度与右侧页边距的和来确定的,计算公式如下:

PageWidth - (PageNumberWidth + RightPageMargin)

具体示例代码如下:

using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
 
namespace AddPageNumbersToRigthCorner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 应用许可证密钥
            LicenseProvider.SetLicenseKey("License Key");
 
            // 创建 PdfDocument 对象
            PdfDocument doc = new PdfDocument();
 
            // 加载 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
 
            // 创建字体、画刷和画笔,用于设置页码的显示样式
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
            PdfBrush brush = PdfBrushes.Black;
            PdfPen pen = new PdfPen(brush, 1.0f);
 
            // 创建 PdfPageNumberField 对象和 PdfPageCountField 对象
            PdfPageNumberField pageNumberField = new PdfPageNumberField();
            PdfPageCountField pageCountField = new PdfPageCountField();
 
            // 创建 PdfCompositeField 对象,将页码字段和总页数字段组合为一个字段
            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
 
 
            // 遍历文档中的每一页
            for (int i = 0; i < doc.Pages.Count; i++)
            {
 
                // 获取指定页面
                PdfPageBase page = doc.Pages[i];
 
                // 获取页面尺寸
                SizeF pageSize = doc.Pages[i].Size;
 
                // 在指定位置绘制一条直线
                page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
 
                // 测量 "Page X of Y" 文本的尺寸
                SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));
 
                // 设置复合字段的位置,实现右对齐
                compositeField.Location = new PointF(pageSize.Width - pageNumberSize.Width - 72, pageSize.Height - 45);
 
                // 在页面上绘制复合字段
                compositeField.Draw(page.Canvas);
 
            }
 
            // 保存为新的 PDF 文件
            doc.SaveToFile("AddPageNumbersToRigthCorner.pdf");
 
            // 释放资源
            doc.Dispose();
        }
    }
}

到此这篇关于使用C#代码在PDF文档添加页码的操作指南的文章就介绍到这了,更多相关C# PDF文档添加页码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#遍历集合与移除元素的方法

    C#遍历集合与移除元素的方法

    这篇文章主要介绍了C#遍历集合与移除元素的方法,结合实例形式分析了C#使用for循环遍历集合以及add与Remove方法进行元素添加与移除的使用技巧,需要的朋友可以参考下
    2016-06-06
  • 如何用C#在PC上查找连接蓝牙设备并实现数据传输

    如何用C#在PC上查找连接蓝牙设备并实现数据传输

    这篇文章主要介绍了如何用C#在PC上查找连接蓝牙设备并实现数据传输,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#遍历文件夹获取指定后缀名文件

    C#遍历文件夹获取指定后缀名文件

    这篇文章主要为大家详细介绍了C#遍历文件夹获取指定后缀名文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C# 文件操作函数 创建文件 判断存在

    C# 文件操作函数 创建文件 判断存在

    本文列举了C#中文件操作中常用的函数,创建文件和判断文件存不存在的基本使用,简单实用,希望能帮到大家。
    2016-05-05
  • C#实现控制摄像头的类

    C#实现控制摄像头的类

    这篇文章主要介绍了C#实现控制摄像头的类,涉及C#操作摄像头的初始化、抓图、录像等功能,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • C#实现简易计算器功能(2)(窗体应用)

    C#实现简易计算器功能(2)(窗体应用)

    这篇文章主要为大家详细介绍了C#实现简易计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C# MemoryStream的具体使用

    C# MemoryStream的具体使用

    MemoryStream是.NET中用于内存数据读写的流类,速度快且灵活,适用于临时缓存、二进制处理等场景,下面就来详细的介绍一下MemoryStream的使用,感兴趣的可以了解一下
    2025-07-07
  • Unity实现轮盘方式的按钮滚动效果

    Unity实现轮盘方式的按钮滚动效果

    这篇文章主要为大家详细介绍了Unity实现轮盘方式的按钮滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#使用base64对字符串进行编码和解码的测试

    C#使用base64对字符串进行编码和解码的测试

    今天小编就为大家分享一篇关于C#使用base64对字符串进行编码和解码的测试,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Unity实现文本转贴图

    Unity实现文本转贴图

    这篇文章主要为大家详细介绍了Unity实现文本转贴图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05

最新评论