Spire.PDF for .NET【页面设置】演示:向 PDF 文档添加页码

news2025/3/1 8:11:39

在 PDF 文档中添加页码不仅实用,而且美观,因为它提供了类似于专业出版材料的精美外观。无论您处理的是小说、报告还是任何其他类型的长文档的数字副本,添加页码都可以显著提高其可读性和实用性。在本文中,您将学习如何使用Spire.PDF for .NET在 C# 中向 PDF 文档添加页码

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载 

安装 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#:向 PDF 文档添加页码

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

在 Spire.PDF for .NET 库中,有两个可用的类:PdfPageNumberFieldPdfPageCountField。这些类允许您在将当前页码和总页数添加到 PDF 文档的页面时检索和显示它们。如果您希望插入诸如“第 X 页”或“第 X 页,共 Y 页”之类的文本,则可以使用PdfCompositeField类,该类使您能够将所需文本与一个或多个字段组合成单个字段。以下是使用 C# 在 PDF 页脚中添加左对齐页码的步骤。

  • 创建一个Document对象。
  • 从指定页面加载 PDF 文件。
  • 创建一个PdfPageNumberField对象和一个PdfPageCountField对象。
  • 创建一个PdfCompositeField对象来创建“第 X 页,共 Y 页”格式。
  • 使用PdfCompositeField.Location属性指定 PdfCompositeField 对象的位置。
  • 遍历文档中的页面,并使用PdfCompositeField.Draw()方法将“第 X 页,共 Y 页”添加到页脚部分的左上角。
  • 将文档保存为不同的 PDF 文件。

【C#】

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)
{
// Apply your license key
LicenseProvider.SetLicenseKey("License Key");

// Create a PdfDocument object
PdfDocument doc = new PdfDocument();

// Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

// Create font, brush and pen, which determine the appearance of the page numbers to be added
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);

// Create a PdfPageNumberField object and a PdfPageCountField object
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();

// Create a PdfCompositeField object to combine page count field and page number field in a single field
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);

// Get the page size
SizeF pageSize = doc.Pages[0].Size;

// Set the location of the composite field
compositeField.Location = new PointF(72, pageSize.Height - 45);

// Iterate through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{

// Get a specific page
PdfPageBase page = doc.Pages[i];

// Draw a line at the specified position
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);

// Draw the composite field on the page
compositeField.Draw(page.Canvas);
}

// Save to a different PDF file
doc.SaveToFile("AddPageNumbersToLeftCorner.pdf");

// Dispose resources
doc.Dispose();
}
}
}

C#:向 PDF 文档添加页码

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

为了将页脚部分的页码与中心对齐,动态计算文本“第 X 页,共 Y 页”的宽度至关重要。此计算至关重要,因为它决定了页码 ( PdfCompositeField ) 的 X 坐标。要实现居中对齐,X 坐标的计算方法是从页面宽度中减去页码的宽度,然后将结果除以 2,如下所示:(PageWidth - PageNumberWidth)/2。

以下是使用 C# 在 PDF 页脚中添加居中对齐页码的步骤。

  • 创建一个Document对象。
  • 从指定页面加载 PDF 文件。
  • 创建一个PdfPageNumberField对象和一个PdfPageCountField对象。
  • 创建一个PdfCompositeField对象来创建“第 X 页,共 Y 页”格式。
  • 使用PdfCompositeField.Location属性指定 PdfCompositeField 对象的位置。
  • 遍历文档中的页面,并使用PdfCompositeField.Draw()方法将“第 X 页,共 Y 页”添加到页脚部分的中心。
  • 将文档保存为不同的 PDF 文件。

【C#】

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)
{
// Apply your license key
LicenseProvider.SetLicenseKey("License Key");

// Create a PdfDocument object
PdfDocument doc = new PdfDocument();

// Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

// Create font, brush and pen, which determine the appearance of the page numbers to be added
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);

// Create a PdfPageNumberField object and a PdfPageCountField object
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();

// Create a PdfCompositeField object to combine page count field and page number field in a single field
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);

// Iterate through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{

// Get a specific page
PdfPageBase page = doc.Pages[i];

// Get the page size
SizeF pageSize = doc.Pages[i].Size;

// Draw a line at the specified position
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);

// Measure the size the "Page X of Y"
SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));

// Set the location of the composite field
compositeField.Location = new PointF((pageSize.Width - pageNumberSize.Width) / 2, pageSize.Height - 45);

// Draw the composite field on the page
compositeField.Draw(page.Canvas);

}

// Save to a different PDF file
doc.SaveToFile("AddPageNumbersToCenter.pdf");

// Dispose resources
doc.Dispose();
}
}
}

C#:向 PDF 文档添加页码

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

要将页码定位在页脚部分的右上角,还必须动态计算文本“第 X 页,共 Y 页”的宽度。因为页码 ( PdfCompositeField ) 的 X 坐标是通过从页面宽度中减去页码和右页边距的组合宽度来确定的,如下所示:PageWidth - (PageNumberWidth + RightPageMargin)。

以下是使用 C# 在 PDF 页脚中添加右对齐页码的步骤。

  • 创建一个Document对象。
  • 从指定页面加载 PDF 文件。
  • 创建一个PdfPageNumberField对象和一个PdfPageCountField对象。
  • 创建一个PdfCompositeField对象来创建“第 X 页,共 Y 页”格式。
  • 使用PdfCompositeField.Location属性指定 PdfCompositeField 对象的位置。
  • 遍历文档中的页面,并使用PdfCompositeField.Draw()方法将“第 X 页,共 Y 页”添加到页脚部分的右上角。
  • 将文档保存为不同的 PDF 文件。

【C#】

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)
{
// Apply your license key
LicenseProvider.SetLicenseKey("License Key");

// Create a PdfDocument object
PdfDocument doc = new PdfDocument();

// Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");

// Create font, brush and pen, which determine the appearance of the page numbers to be added
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);

// Create a PdfPageNumberField object and a PdfPageCountField object
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();

// Create a PdfCompositeField object to combine page count field and page number field in a single field
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);

// Iterate through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{

// Get a specific page
PdfPageBase page = doc.Pages[i];

// Get the page size
SizeF pageSize = doc.Pages[i].Size;

// Draw a line at the specified position
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);

// Measure the size the "Page X of Y"
SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));

// Set the location of the composite field
compositeField.Location = new PointF(pageSize.Width - pageNumberSize.Width - 72, pageSize.Height - 45);

// Draw the composite field on the page
compositeField.Draw(page.Canvas);

}

// Save to a different PDF file
doc.SaveToFile("AddPageNumbersToRigthCorner.pdf");

// Dispose resources
doc.Dispose();
}
}
}

C#:向 PDF 文档添加页码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2259109.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【iOS】OC高级编程 iOS多线程与内存管理阅读笔记——自动引用计数(三)

目录 ARC规则 概要 所有权修饰符 __strong修饰符 __weak修饰符 __unsafe_unretained修饰符 __autoreleasing修饰符 ARC规则 概要 “引用计数式内存管理”的本质部分在ARC中并没有改变&#xff0c;ARC只是自动地帮助我们处理“引用计数”的相关部分。 在编译单位上可以…

An error happened while trying to locate the file on the Hub and we cannot f

An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on. 关于上述comfy ui使用control net预处理器的报错问…

angular19-官方教程学习

周日了解到angular已经更新到19了&#xff0c;想按官方教程学习一遍&#xff0c;工欲善其事必先利其器&#xff0c;先更新工具&#xff1a; 安装新版版本 卸载老的nodejs 20.10.0&#xff0c;安装最新的LTS版本 https://nodejs.org 最新LTS版本已经是22.12.0 C:\Program File…

计算机毕业设计Python+Vue.js游戏推荐系统 Steam游戏推荐系统 Django Flask 游 戏可视化 游戏数据分析 游戏大数据 爬虫 机

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

上海亚商投顾:创业板指震荡调整 机器人概念股再度爆发

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 沪指昨日冲高回落&#xff0c;深成指、创业板指盘中跌超1%&#xff0c;尾盘跌幅有所收窄。机器人概念股逆势爆…

粘贴可运行:Java调用大模型(LLM) 流式Flux stream 输出;基于spring ai alibaba

在Java中&#xff0c;使用Spring AI Alibaba框架调用国产大模型通义千问&#xff0c;实现流式输出&#xff0c;是一种高效的方式。通过Spring AI Alibaba&#xff0c;开发者可以轻松地集成通义千问模型&#xff0c;并利用其流式处理能力&#xff0c;实时获取模型生成的文本。这…

【CSS in Depth 2 精译_070】11.3 利用 OKLCH 颜色值来处理 CSS 中的颜色问题(下):从页面其他颜色衍生出新颜色

当前内容所在位置&#xff08;可进入专栏查看其他译好的章节内容&#xff09; 第四部分 视觉增强技术 ✔️【第 11 章 颜色与对比】 ✔️ 11.1 通过对比进行交流 11.1.1 模式的建立11.1.2 还原设计稿 11.2 颜色的定义 11.2.1 色域与色彩空间11.2.2 CSS 颜色表示法 11.2.2.1 RGB…

Ajax--实现检测用户名是否存在功能

目录 &#xff08;一&#xff09;什么是Ajax &#xff08;二&#xff09;同步交互与异步交互 &#xff08;三&#xff09;AJAX常见应用情景 &#xff08;四&#xff09;AJAX的优缺点 &#xff08;五&#xff09;使用jQuery实现AJAX 1.使用JQuery中的ajax方法实现步骤&#xf…

【PSINS】以速度和位置作为观测量(即6维观测量)的组合导航滤波,EKF实现,提供可直接运行的MATLAB代码

原有的代码是以位置作为观测量的,这里提供位置+速度,共6维的观测量,状态量还是15维不变 文章目录 源代码运行结果PS源代码 源代码如下: % 【PSINS】位置与速度为观测的153,EKF。从速度观测的EKF153改进而来 % 2024-12-11/Ver1:位置与速度为观测量% 清空工作空间,清除命…

探索云原生安全解决方案的未来

我们是否充分意识到云端所面临的网络安全威胁&#xff1f; 在当今互联互通的世界中&#xff0c;维护安全的环境至关重要。云的出现扩大了潜在威胁的范围&#xff0c;因为它催生了机器身份&#xff08;称为非人类身份 (NHI)&#xff09;及其秘密。随着组织越来越多地转向云原生…

无法正常启动此程序,因为计算机丢失wlanapi.dll

wlanapi.dll丢失怎么办&#xff1f;有没有什么靠谱的修复wlanapi.dll方法_无法启动此程序,因为计算机中丢失wlanapi.dll-CSDN博客 wlanapi.dll是 Windows 操作系统中的一个动态链接库文件&#xff0c;主要与 Windows 无线 LAN (WLAN) API 相关。该DLL提供了许多必要的函数&…

ADC -DMA

文章目录 前言一、ADC配置修改二、配置DMA三、DMA中断四、完整参考代码总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 项目需要&#xff1a; 在原有基础上改进&#xff0c;采用DMA中断采集数据 提示&#xff1a;以下是本篇文章正文内容&#xff0…

Referer头部在网站反爬虫技术中的运用

网站数据的安全性和完整性至关重要。爬虫技术&#xff0c;虽然在数据收集和分析中发挥着重要作用&#xff0c;但也给网站管理员带来了挑战。为了保护网站数据不被恶意爬取&#xff0c;反爬虫技术应运而生。本文将探讨HTTP头部中的Referer字段在反爬虫技术中的应用&#xff0c;并…

docker搭建haproxy实现负载均衡

华子目录 获取haproxy镜像建立haproxy容器的数据卷获取haproxy的配置文件编写yaml文件运行测试 获取haproxy镜像 [rootdocker-node1 ~]# docker pull haproxy:2.3建立haproxy容器的数据卷 conf目录为数据卷 [rootdocker-node1 ~]# mkdir /var/lib/docker/volumes/conf/获取h…

阿里云数据库MongoDB版助力极致游戏高效开发

客户简介 成立于2010年的厦门极致互动网络技术股份有限公司&#xff08;以下简称“公司”或“极致游戏”&#xff09;&#xff0c;是一家集网络游戏产品研发与运营为一体的重点软件企业&#xff0c;公司专注于面向全球用户的网络游戏研发与运营。在整个产业链中&#xff0c;公…

深入探索前端调试神器vConsole

深入探索前端调试神器vConsole 在前端开发过程中&#xff0c;调试工具的重要性不言而喻。而vConsole作为一款轻量级的、可嵌入的JavaScript调试面板&#xff0c;为前端开发者提供了一个便捷的调试解决方案。本文将带你深入了解vConsole的基本概念、作用&#xff0c;并通过丰富…

子网划分实例

看到有人问这个问题&#xff1a; 想了一下&#xff0c;这是一个子网划分的问题&#xff1a; 处理方法如图&#xff1a; 这是一个子网划分的问题 设备1用三层交换机&#xff0c;端口设置为路由模式&#xff0c;设备2和设备3为傻瓜交换机模式 设备2和设备3下挂设备都是26为掩码&…

【机器人】振动分析和控制工具之Bode图

Bode 图完整介绍 Bode 图由两个部分组成&#xff1a; 幅值图 (Magnitude Plot)&#xff1a;描述系统对不同频率输入信号的增益大小&#xff08;幅值响应&#xff09;。相位图 (Phase Plot)&#xff1a;描述系统输出信号相对于输入信号的相位差。 Bode 图的横轴是频率&#x…

Rerender A Video 技术浅析(五):对象移除与自动配色

Rerender A Video 是一种基于深度学习和计算机视觉技术的视频处理工具&#xff0c;旨在通过智能算法对视频进行重新渲染和优化。 一、对象移除模块 1. 目标检测 1.1 概述 目标检测是对象移除的第一步&#xff0c;旨在识别视频中需要移除的对象并生成相应的掩码&#xff08;m…

经典的网络安全技术

以我的理解&#xff0c;“黑客”大体上应该分为“正”、“邪”两类&#xff0c;正派黑客依靠自己掌握的知识帮助系统管理员找出系统中的漏洞并加以完善&#xff0c;而邪派黑客则是通过各种黑客技能对系统进行攻击、入侵或者做其他一些有害于网络的事情&#xff0c;因为邪派黑客…