PdfiumViewer是一款基于谷歌开源PDF渲染引擎PDFium的.NET库,主要用于在Windows应用程序中显示和处理PDF文档。PdfiumViewer提供了多种API和控件,使得开发者可以轻松地将PDF文档嵌入到其应用程序中。同时,PdfiumViewer还支持多种平台,包括Windows、Linux以及MacOS。
PdfiumViewer适用于各种需要在应用程序中显示和处理PDF文档的场景。比如,可以将PdfiumViewer用于制作PDF阅读器、PDF编辑器、报告生成器等。
PdfiumViewer库的架构设计和组件模块
PdfiumViewer的架构设计主要包含以下几个组件模块:
要在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
}
PdfiumViewer支持多种方式渲染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);
}
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
}
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);
}
}
PdfiumViewer提供了多种API,用于处理PDF文档。下面介绍一些常用的API。
要获取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页面的各种信息,需要使用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文本,需要使用
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库的完整实例。
示例包含一个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是一款基于PDFium引擎的.NET库,适用于各种需要在应用程序中显示和处理PDF文档的场景。它提供了多种API和控件,使得开发者可以轻松地将PDF文档嵌入到其应用程序中。同时,PdfiumViewer还支持多种平台,具有良好的跨平台支持。开发者可以根据自己的需求来选择合适的PDF处理库。