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();
}
}
}
相关推荐
忘却的纪念4 分钟前
基于SpringBoot的考研资讯平台设计与实现
java·spring boot·spring
.生产的驴6 分钟前
SpringBoot 消息队列RabbitMQ死信交换机
java·spring boot·后端·rabbitmq·java-rabbitmq
振华OPPO7 分钟前
我的5周年创作纪念日,不忘初心,方得始终。
android·java·android studio·安卓·安卓app
抚月code10 分钟前
Java线程池
java
IT枫斗者15 分钟前
集合工具类
java·linux·数据库·windows·算法·microsoft
会敲代码的小张28 分钟前
设计模式-观察者模式
java·开发语言·后端·观察者模式·设计模式·代理模式
程序猿!=程序员4 小时前
JAVA学习路线
java
Rivieres4 小时前
算法入门-贪心1
java·算法·leetcode·推荐算法
清风霁玥缘4 小时前
Maven入门学习
java·maven
中式代码美式咖啡5 小时前
记录开发一个英语听力训练网站
java·spring boot·bootstrap·音视频·语音识别