首页 > 其他分享 >PdfiumViewer库详细解析

PdfiumViewer库详细解析

时间:2024-03-04 17:25:05浏览次数:24  
标签:Load document PdfiumViewer 文档 详细 using PDF 解析

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处理库。

       

      标签:Load,document,PdfiumViewer,文档,详细,using,PDF,解析
      From: https://www.cnblogs.com/soliang/p/18052189

      相关文章

      • 免费ssl证书,使用acme.sh,泛解析,阿里dns自动续期
        -阿里云注册用户,添加dns可编辑权限- curlhttps://get.acme.sh|[email protected] -注意大小写exportAli_Key="<key>"exportAli_Secret="<secret>" - 执行阿里自动dns cd~/.acme.sh/./acme.sh--issue--dnsdns_ali-dexample.com-......
      • Application.Caller详细解释
        官网给的解释基本看不懂反复测试下能搞懂的部分,我知道就三点1)单元格调用自定义函数的时候,表示单元格自己单元格调用老版本又分2种同样下面的代码Functiontest()DimrngAsRangeSetrng=Application.Callertest=rng.AddressEndFunction如果我在C14单元格......
      • JSON.parse解析字符串报错-SyntaxError: Unexpected token ‘ in JSON at position 报
        “SyntaxError:Unexpectedtoken’inJSONatposition”报错原因是因为解析的字符串对象中,JSON.parse无法识别;JSON.parse可以将标准的json类型数据转换为JavaScript对象,如果数据不是正确的json类型的数据则会控制台报错,可能会阻断代码的正常运行我们可以写一个函数来......
      • 数据解析常用命令1——strstr
        strstr()函数的使用说明(C语言)头文件<string.h>函数作用:1、strstr()函数搜索一个字符串在另一个字符串中的第一次出现。2、找到所搜索的字符串,则该函数返回第一次匹配的字符串的地址;3、如果未找到所搜索的字符串,则返回NULL。函数原型:char*strstr(char*str1,constchar......
      • 【C++ STL容器set 】std::set 的全方位解析
        装载自知乎(虽然有AI辅助操作,但是确实写得好好):【C++STL容器set】std::set的全方位解析-知乎(zhihu.com)<imgsrc="https://pic3.zhimg.com/v2-cc8068b8931c7f65e9a89717e2ab404e_b.jpg"data-size="normal"data-rawwidth="1024"data-rawheight="1024......
      • CAS解析
        目录什么是CAS应用场景以AtomicInteger为例,分析在Java中如何利用CAS实现原子操作的Unsafe类Unsafe类中的compareAndSwapInt缺点什么是CAS应用场景:并发我认为V的值应该是A,如果是的话,那我就把它修改成B,如果不是A(说明被别人修改过了),那我就不修改了,避免多人同时操作导致出错......
      • pod常见的非故障及故障状态解析
        在Kubernetes中,Pod的状态可以反映其当前的生命周期状态、是否正常运行或遇到了某些状况。以下是一些Pod常见的非故障状态:Running:这是Pod最常见的非故障状态,表示Pod已经成功调度到了一个节点上,并且其中所有的容器都已经被成功创建,至少有一个容器正在运行。Succeeded:这个状态通常......
      • [详解-vector] C++必知必会 vector常用各种操作解析
        原文转载自知乎:https://zhuanlan.zhihu.com/p/648650828vector是C++标准库中的一个动态数组容器,它可以自动管理内存大小,可以在运行时根据需要动态增长或缩小。它是一个非常常用且强大的容器,用于存储一系列元素。下面详细介绍vector的使用方法,并提供相应的代码案例。1.包含......
      • python接口自动化系列(06):解析初始化sql
         本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/18033074实现目标将initSql中常用变量占位符(下图划线的)替换为实际值。 添加替换变量工具模块substitution_tool.py#!/usr/bin/envpython#-*-coding:utf-8-*-#@Author:韧#@wx:ren168632201#@Bl......
      • 问题:图片解析后url路径解析发生改变
        背景:解析站长素材首页的图片看程序执行在我执行完程序之后显示爬取成功但是并没有爬取到图片找错过程把settings中的LOG_LEVEL="ERROE"改成WARNING 就发现了警告信息于是我又进行输出url发现了本身爬取的url没有问题但是用Request解析的时候就发现了解析错了就......