Spire.PDF for .NET【文档操作】演示:创比较 PDF 文档

PDF 已成为跨不同平台共享和保存文档的标准格式,在专业和个人环境中都发挥着无处不在的作用。但是,创建高质量的 PDF 文档需要多次检查和修订。在这种情况下,了解如何有效地比较 PDF 文件并找出它们的差异变得至关重要,这使文档编辑器能够快速识别文档不同版本之间的差异,从而在文档创建和审阅过程中节省大量时间。本文旨在演示如何在 C# 程序中使用Spire.PDF for .NET轻松比较 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 项目中。 以通过NuGet安装。

复制代码
PM> Install-Package Spire.PDF
使用 C# 比较两个 PDF 文档

使用 Spire.PDF for .NET,开发人员可以创建PdfComparer 类的实例,传递两个PdfDocument 对象作为参数,然后利用**PdfComparer.Compare(String fileName)**方法比较两个文档。比较结果将保存为新的 PDF 文档,以便进一步分析或审查两个 PDF 之间的差异。

生成的 PDF 文档在左侧和右侧显示两个原始文档,其中删除的项目以红色显示,添加的项目以黄色显示。

以下是比较两个PDF文档的详细步骤:

  • 创建两个PdfDocument 类的对象并使用**PdfDocument.LoadFromFile()**方法加载两个 PDF 文档。
  • 创建PdfComparer 类的实例并将两个PdfDocument对象作为参数传递。
  • **使用PdfComparer.Compare()**方法比较两个文档并将结果保存为另一个 PDF 文档。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Comparison;

namespace ExtractTablesToExcel
{
class Program
{
static void Main(string[] args)
{
//Create an object of PdfDocument class and load a PDF document
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile("Sample1.pdf");

//Create another object of PdfDocument class and load another PDF document
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile("Sample2.pdf");

//Create an object of PdfComparer class with the two document
PdfComparer comparer = new PdfComparer(pdf1, pdf2);

//Compare the two document and save the comparing result to another PDF document
comparer.Compare("output/ComparingResult.pdf");
pdf1.Close();
pdf2.Close();
}
}
}
比较两个 PDF 文档的特定页面范围

创建PdfComparer 类实例后,开发人员还可以使用**PdfComparer.Options.SetPageRange()**方法设置要比较的页面范围。这样可以仅比较两个 PDF 文档中的指定页面范围。详细步骤如下:

  • 创建两个PdfDocument 类的对象并使用**PdfDocument.LoadFromFile()**方法加载两个 PDF 文档。
  • 创建PdfComparer 类的实例并将两个PdfDocument对象作为参数传递。
  • **使用PdfComparer.Options.SetPageRange()**方法设置要比较的页面范围。
  • **使用PdfComparer.Compare()**方法比较两个 PDF 文档中指定的页面范围,并将结果保存为另一个 PDF 文档。

【C#】

复制代码
using Spire.Pdf;
using Spire.Pdf.Comparison;

namespace ExtractTablesToExcel
{
class Program
{
static void Main(string[] args)
{
//Create an object of PdfDocument class and load a PDF document
PdfDocument pdf1 = new PdfDocument();
pdf1.LoadFromFile("Sample1.pdf");

//Create another object of PdfDocument class and load another PDF document
PdfDocument pdf2 = new PdfDocument();
pdf2.LoadFromFile("Sample2.pdf");

//Create an object of PdfComparer class with the two document
PdfComparer comparer = new PdfComparer(pdf1, pdf2);

//Set the page range to be compared
comparer.Options.SetPageRanges(1, 1, 1, 1);

//Compare the specified page range and save the comparing result to another PDF document
comparer.Compare("output/PageRangeComparingResult.pdf");
pdf1.Close();
pdf2.Close();
}
}
}
相关推荐
guslegend7 小时前
SpringBoot源码剖析
java
皮卡龙7 小时前
Spring MVC 接收请求参数的核心
java·spring·mvc
爱笑的眼睛117 小时前
FastAPI 路由系统深度探索:超越基础 CRUD 的高级模式与架构实践
java·人工智能·python·ai
武子康7 小时前
Java-193 Spymemcached 深入解析:线程模型、Sharding 与序列化实践全拆解
java·开发语言·redis·缓存·系统架构·memcached·guava
韩凡8 小时前
HashMap的理解与结构
java·开发语言·哈希算法
hhzz8 小时前
Spring Boot整合Activiti的项目中实现抄送功能
java·spring boot·后端
初心灬8 小时前
Java 对接coze工作流
java
代衡_Monster8 小时前
通过位运算实现Java逻辑的包含关系
java·java-ee
毕设源码-朱学姐8 小时前
【开题答辩全过程】以 基于Java的失物招领系统设计与实现为例,包含答辩的问题和答案
java·开发语言