如何使用C#代码在 PDF 文档添加页码

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

安装 Spire.PDF for .NET

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

cs 复制代码
PM> Install-Package Spire.PDF

PDF 坐标系统

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

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

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

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

具体示例代码如下:

cs 复制代码
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

具体示例代码如下:

cs 复制代码
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 坐标是通过用页面宽度减去页码宽度与右侧页边距的和来确定的,计算公式如下:

cs 复制代码
PageWidth - (PageNumberWidth + RightPageMargin)

具体示例代码如下:

cs 复制代码
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();
        }
    }
}
相关推荐
优选资源分享1 小时前
PDF Anti-Copy Pro v2.6.2.4:PDF 防拷贝工具
网络·安全·pdf
半熟的皮皮虾1 天前
又重新写了个PDF工具箱-转换office格式/合并/拆分/删除常见操作都有了
python·程序人生·pdf·flask·开源·json·学习方法
waterfeeling1 天前
AGI 论文复现日记:攻克 PDF 解析的“第一公里”
pdf·agi
qq_546937271 天前
PDF工具的天花板!PDF补丁丁:开源免费+无广告,支持Win7~Win11,批量OCR秒完成
pdf·ocr
小真zzz1 天前
ChatPPT免费功能之【导出PDF】:PPT内容安全+便捷分享
人工智能·ai·pdf·powerpoint·ppt·aippt
Rover Ramble2 天前
提取大型非扫描pdf文件的表格数据
pdf
2501_907136822 天前
电子礼簿系统-红白喜事记账工具,PDF/Execl导出
pdf·软件需求
王五周八2 天前
html转化为base64编码的pdf文件
前端·pdf·html
ComPDFKit3 天前
ComPDF 与 Aspose:转换 SDK 的全面比较
pdf
优选资源分享3 天前
PDF 电子签章工具 v5.0:全能处理PDF电子签章
pdf