欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 开发高质量PDF应用的不二选择:PdfiumViewer库详细解析

开发高质量PDF应用的不二选择:PdfiumViewer库详细解析

2024/10/24 14:16:09 来源:https://blog.csdn.net/houbincarson/article/details/141307202  浏览:    关键词:开发高质量PDF应用的不二选择:PdfiumViewer库详细解析

在这里插入图片描述

1. PdfiumViewer库简介

PdfiumViewer是一款基于谷歌开源PDF渲染引擎PDFium的.NET库,主要用于在Windows应用程序中显示和处理PDF文档。PdfiumViewer提供了多种API和控件,使得开发者可以轻松地将PDF文档嵌入到其应用程序中。同时,PdfiumViewer还支持多种平台,包括Windows、Linux以及MacOS。
在这里插入图片描述

2. PdfiumViewer库使用场景

PdfiumViewer适用于各种需要在应用程序中显示和处理PDF文档的场景。比如,可以将PdfiumViewer用于制作PDF阅读器、PDF编辑器、报告生成器等。

PdfiumViewer库的架构设计和组件模块
PdfiumViewer的架构设计主要包含以下几个组件模块:

  • PDF Rendering:PDF Rendering是PdfiumViewer的核心模块,主要用于解析和渲染PDF文档。该模块包含多个类,如PDFDocument、PDFPage、PDFRenderer等,用于描述PDF文档的各个部分,并将它们渲染到屏幕上。
  • Input Handling:Input Handling是PdfiumViewer的输入处理模块,负责处理用户与PDF文档的交互。该模块包含多个类,如PDFMouseHandler、PDFKeyboardHandler等,用于响应鼠标、键盘事件,实现PDF文档的滚动、缩放等功能。
  • User Interface:User Interface是PdfiumViewer的用户界面模块,负责实现PDF文档的显示和控制。该模块包含多种UI控件,如PDFViewControl、PDFSearchControl、PDFBookmarkControl等,用于显示PDF文档内容并提供交互功能。

3. PdfiumViewer库的优点和缺点

3.1 PdfiumViewer的优点包括:

  • 高质量渲染:PdfiumViewer基于PDFium引擎实现,能够对PDF文档进行高质量的渲染和展示,保证了PDF文档在应用程序中的可视化效果。
  • 轻量级:PdfiumViewer是一款轻量级的库,易于学习和使用,同时也占用较少的计算机资源。
  • 跨平台支持:PdfiumViewer支持多种平台,包括Windows、Linux以及MacOS,方便开发者进行跨平台开发。

3.2 PdfiumViewer的缺点包括:

  • 输入处理相对简单:PdfiumViewer对用户输入的处理相对简单,无法满足高级用户交互的需求。
  • 文档较少:由于PdfiumViewer是一个相对较新的库,目前的文档比较少,需要自行阅读源码或查找社区资源。

4. PdfiumViewer的基本功能和代码示例。

4.1 加载PDF文档

要在PdfiumViewer中加载PDF文档,需要使用PdfiumViewer.PdfDocument类。使用Load方法加载指定路径的PDF文档。Load方法返回PdfDocument对象,用于描述PDF文档的各个部分。

using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Use the document object
}

4.2 渲染PDF文档

PdfiumViewer支持多种方式渲染PDF文档,包括渲染到屏幕、渲染到内存和渲染到文件。下面分别介绍这三种方式。

  1. 渲染到屏幕
    要将PDF文档渲染到屏幕上,需要使用PdfViewerControl控件。将PdfDocument对象赋值给PdfViewerControl的Document属性,即可在控件中显示PDF文档。
using PdfiumViewer;// Create a PdfViewerControl
var viewer = new PdfViewerControl();// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Set the document object to the PdfViewerControlviewer.Document = document;// Add the viewer control to a parent containerthis.MyGrid.Children.Add(viewer);
}
  1. 渲染到内存
    要将PDF文档渲染到内存中,需要使用PdfiumViewer.PdfRenderer类。将PdfDocument对象和指定的页面参数传递给PdfRenderer的Render方法,即可获得一个Bitmap对象,该对象包含渲染后的图像数据。
using System.Drawing;
using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Create a PdfRenderer objectvar renderer = new PdfRenderer(document);// Render the first page of the PDF to a Bitmap objectBitmap bitmap = renderer.Render(0, 300, 300, true);// Use the bitmap object
}
  1. 渲染到文件
    要将PDF文档渲染到文件中,需要使用PdfiumViewer.PdfRenderer类。将PdfDocument对象和指定的页面参数传递给PdfRenderer的RenderToStream方法,即可将渲染后的图像数据写入到指定的文件流中。
using System.IO;
using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Create a PdfRenderer objectvar renderer = new PdfRenderer(document);// Render the first page of the PDF to a file streamusing (var fileStream = new FileStream("page1.png", FileMode.Create)){renderer.RenderToStream(0, fileStream, ImageFormat.Png);}
}

5. 处理PDF文档

PdfiumViewer提供了多种API,用于处理PDF文档。下面介绍一些常用的API。

5.1 获取PDF文档信息

要获取PDF文档的各种信息,需要使用PdfiumViewer.PdfDocument类的相应属性。比如,可以获取PDF文档的标题、作者、页数等信息。

using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Get the title of the PDF documentstring title = document.Title;// Get the author of the PDF documentstring author = document.Author;// Get the number of pages in the PDF documentint pageCount = document.PageCount;
}

5.2 获取PDF页面信息

要获取PDF页面的各种信息,需要使用PdfiumViewer.PDFPage类的相应属性。比如,可以获取PDF页面的大小、旋转角度等信息。

using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Get the first page of the PDF documentPdfPage page = document.Pages[0];// Get the size of the PDF pageSizeF size = page.Size;// Get the rotation angle of the PDF pageint rotation = page.Rotation;
}

5.3 搜索PDF文本

要搜索PDF文本,需要使用
PdfiumViewer.PdfTextFinder类。将PdfDocument对象和指定的搜索字符串传递给PdfTextFinder的Search方法,即可获得一个PdfTextMatches对象,该对象包含匹配结果的详细信息。

using PdfiumViewer;// Load a PDF document from a file
using (var document = PdfDocument.Load("sample.pdf"))
{// Create a PdfTextFinder objectvar finder = new PdfTextFinder(document);// Search for a string in the PDF documentPdfTextMatches matches = finder.Search("search string");// Use the matches object
}

在这里插入图片描述

6. 使用WPF代码案例介绍PdfiumViewer库用法

下面是一个WPF程序中使用PdfiumViewer库的完整实例。

示例包含一个MainWindow窗口,其中包含一个Button控件和一个Grid控件。点击Button控件会调用ShowPDF方法,在Grid控件中显示PDF文档。

同时,需要将PdfiumViewer库的dll文件添加到项目中,并在代码中引用。

using System.Windows;
using System.Windows.Controls;
using PdfiumViewer;public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){ShowPDF();}private void ShowPDF(){// Create a PDFDocument object from a fileusing (var document = PdfDocument.Load("sample.pdf")){// Create a PDFViewerControl object and set its document propertyvar viewer = new PdfViewerControl { Document = document };// Add the viewer control to a parent containerthis.MyGrid.Children.Add(viewer);}}
}

在XAML中,添加以下控件:

<Window x:Class="WpfApp1.MainWindow"...xmlns:pdf="clr-namespace:PdfiumViewer.Wpf;assembly=PdfiumViewer.Wpf"><Grid><Button Content="Show PDF" Click="Button_Click"/><Grid x:Name="MyGrid"/></Grid>
</Window>

以上代码会在MainWindow窗口中添加一个Button和一个Grid控件。当按钮被点击时,调用ShowPDF方法,在Grid控件中显示PDF文档。需要确保将PdfiumViewer库的dll文件添加到项目中,并在代码中引用。也可以通过NuGet包管理器安装PdfiumViewer库。

7. 总结PdfiumViewer库

PdfiumViewer是一款基于PDFium引擎的.NET库,适用于各种需要在应用程序中显示和处理PDF文档的场景。它提供了多种API和控件,使得开发者可以轻松地将PDF文档嵌入到其应用程序中。同时,PdfiumViewer还支持多种平台,具有良好的跨平台支持。开发者可以根据自己的需求来选择合适的PDF处理库。

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com