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();
}
}
}
相关推荐
华仔啊1 小时前
主线程存了用户信息,子线程居然拿不到?ThreadLocal 背锅
java·后端
间彧2 小时前
Spring Boot项目中,Redis 如何同时执行多条命令
java·redis
召摇2 小时前
如何避免写垃圾代码:Java篇
java·后端·代码规范
vker2 小时前
第 1 天:单例模式(Singleton Pattern)—— 创建型模式
java·设计模式
我不是混子2 小时前
什么是内存泄漏?
java
程序员小假2 小时前
我们来说说当一个线程两次调用 start() 方法会出现什么情况?
java·后端
SimonKing3 小时前
Archery:开源、一站式的数据库 SQL 审核与运维平台
java·后端·程序员
唐青枫4 小时前
C#.NET 定时任务必备:Quartz 全面解析与实战指南
c#·.net
皮皮林55114 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
该用户已不存在18 小时前
6个值得收藏的.NET ORM 框架
前端·后端·.net