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();
}
}
}
相关推荐
非 白几秒前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼6 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒9 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
不想有bug的小菜鸟16 分钟前
vue3使用iframe全屏展示pdf效果
前端·pdf
Forget the Dream38 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek41 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式