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();
}
}
}
相关推荐
都叫我大帅哥25 分钟前
🌊 Redis Stream深度探险:从秒杀系统到面试通关
java·redis
都叫我大帅哥25 分钟前
Redis持久化全解析:从健忘症患者到记忆大师的逆袭
java·redis
程序猿阿越44 分钟前
Kafka源码(一)Controller选举与创建Topic
java·后端·源码
程序无bug1 小时前
Spring6 当中 Bean 的生命周期的详细解析:有五步,有七步,有十步
java
二川bro1 小时前
飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
java·人工智能·重构
Q_970956391 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724091 小时前
Maven
java·数据库·maven
军训猫猫头1 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站