User Interface:User Interface是PdfiumViewer的用户界面模块,负责实现PDF文档的显示和控制。该模块包含多种UI控件,如PDFViewControl、PDFSearchControl、PDFBookmarkControl等,用于显示PDF文档内容并提供交互功能。
跨平台支持:PdfiumViewer支持多种平台,包括Windows、Linux以及MacOS,方便开发者进行跨平台开发。
文档较少:由于PdfiumViewer是一个相对较新的库,目前的文档比较少,需要自行阅读源码或查找社区资源。
using PdfiumViewer; // Load a PDF document from a file using (var document = PdfDocument.Load("sample.pdf")) { // Use the document object }渲染PDF文档
using PdfiumViewer; // 堆代码 duidaima.com // 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); } }处理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页面信息
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文本
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库用法
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库。