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

开发 前端
PdfiumViewer是一款基于PDFium引擎的.NET库,适用于各种需要在应用程序中显示和处理PDF文档的场景。它提供了多种API和控件,使得开发者可以轻松地将PDF文档嵌入到其应用程序中。

PdfiumViewer库简介

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

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文档内容并提供交互功能。

PdfiumViewer库的优点和缺点

PdfiumViewer的优点包括:

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

PdfiumViewer的缺点包括:

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

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

加载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
}

渲染PDF文档

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

渲染到屏幕

要将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 PdfViewerControl
    viewer.Document = document;

    // Add the viewer control to a parent container
    this.MyGrid.Children.Add(viewer);
}

渲染到内存

要将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 object
    var renderer = new PdfRenderer(document);

    // Render the first page of the PDF to a Bitmap object
    Bitmap bitmap = renderer.Render(0, 300, 300, true);

    // Use the bitmap object
}

渲染到文件

要将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 object
    var renderer = new PdfRenderer(document);

    // Render the first page of the PDF to a file stream
    using (var fileStream = new FileStream("page1.png", FileMode.Create))
    {
        renderer.RenderToStream(0, fileStream, ImageFormat.Png);
    }
}

处理PDF文档

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

获取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 document
    string title = document.Title;

    // Get the author of the PDF document
    string author = document.Author;

    // Get the number of pages in the PDF document
    int pageCount = document.PageCount;
}

获取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 document
    PdfPage page = document.Pages[0];

    // Get the size of the PDF page
    SizeF size = page.Size;

    // Get the rotation angle of the PDF page
    int rotation = page.Rotation;
}

搜索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 object
    var finder = new PdfTextFinder(document);

    // Search for a string in the PDF document
    PdfTextMatches matches = finder.Search("search string");

    // Use the matches object
}

使用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 file
        using (var document = PdfDocument.Load("sample.pdf"))
        {
            // Create a PDFViewerControl object and set its document property
            var viewer = new PdfViewerControl { Document = document };

            // Add the viewer control to a parent container
            this.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库。

总结PdfiumViewer库

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

责任编辑:姜华 来源: 今日头条
相关推荐

2023-07-06 14:51:30

开发高质量软件

2017-07-14 09:54:47

代码函数程序

2015-08-19 08:54:23

Android开发框架

2011-08-17 09:22:00

TD-LTE组网

2021-08-08 14:26:24

SQL数据库开发

2023-01-27 23:46:36

嵌入式软件技巧

2011-05-31 13:43:46

外链

2013-12-03 09:34:26

iOS应用开发实践高质量Objectiv

2021-12-08 07:49:46

Ahooks 3.0React Hooks

2010-03-01 14:31:04

Java

2012-09-13 10:44:18

Python代码

2011-03-04 10:11:09

JavascriptAPI

2009-11-23 20:39:21

ibmdw敏捷开发

2020-03-12 14:03:59

工具代码开发

2023-03-09 15:05:46

HTMLWeb 开发SEO

2015-08-25 08:29:11

编写高质量命名

2011-12-29 15:02:27

JavaScript

2015-08-03 10:40:59

程序员代码质量Quora
点赞
收藏

51CTO技术栈公众号