C#使用开源免费库 PdfiumViewer 实现PDF打印与查看
|
admin
2024年7月1日 16:14
本文热度 3980
|
前言
PDF是一种常用的文件格式,实现其打印、查看操作是较为常见的需求。例如打印PDF格式的快递面单、发票等。如何通过编写C#代码实现打印、查看需求,可以使用一些三方C#组件库。如Spire.PDF for .NET、PdfiumViewer等,本文介绍使用PdfiumViewer实现方式。
PdfiumViewer
PdfiumViewer 是基于 Pdfium 库的.NET PDF查看器组件。可以使我们轻松地在应用程序中嵌入PDF文档,对文档的查看与打印。(或其他基于 Pdfium 库.NET库)。.NET Framework》PdfiumViewer;.NET 6》PdfiumViewer.Core
1、使用和附录
https://github.com/pvginkel/PdfiumViewer
https://github.com/TimChen44/PdfiumViewer.Core
PdfiumViewer.Native.x86_64.v8-xfa:64位的Pdfium。PdfiumViewer.Native.x86.v8-xfa:32位的Pdfium。
2、使用的类或控件
| 类名 | 描述 |
|---|
| PdfDocument | 用于呈现 PDF 文档的类。 |
| PdfRenderer | 用于呈现 PdfDocument 的控件类。 |
| PdfViewer | 托管 PdfRenderer 的控件。 |
示例代码
1、打印示例
/// <summary>/// 静默打印 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void buttonPrint_Click(object sender, EventArgs e){ try { string printerName = "Microsoft Print to PDF"; string documentName = Guid.NewGuid().ToString("N"); short printCopies = 1; // string fileName = string.Format(@"D:\Temp\{0}", "20240512193805.pdf"); // 判断文件是否存在 if (!File.Exists(fileName)) { MessageBox.Show(string.Format("【{0}】文件不存在!",fileName)); return; } // 读取文件内容 byte[] fileBuffer = File.ReadAllBytes(fileName); // 将字节内容转为流 MemoryStream memoryStream = new MemoryStream(fileBuffer); // fileBuffer = null; // 开始组件装载文件 using (PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(memoryStream)) { // 创建文档输出发送到打印机对像 PrintDocument printDocument = pdfDocument.CreatePrintDocument(); // 将文档打印到打印机 printDocument.PrintController = new StandardPrintController(); if (!string.IsNullOrEmpty(printerName)) { // 输出的目标打印机 printDocument.PrinterSettings.PrinterName = printerName; } // 打印文档名称 printDocument.DocumentName = documentName; // 打印份数 printDocument.PrinterSettings.Copies = printCopies; // 开始发送打印 printDocument.Print(); } } catch(Exception exception) { MessageBox.Show(exception.Message); }}
2、查看示例
/// <summary>/// 打开阅读/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){ try { // PDF 文件 string fileName = string.Format(@"D:\Temp\{0}", "20240512193805.pdf"); // 判断文件是否存在 if (!File.Exists(fileName)) { MessageBox.Show(string.Format("【{0}】文件不存在!", fileName)); return; } // 开始组件装载文件 PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(fileName); this.pdfViewer.Document = pdfDocument; this.pdfViewer.Show(); } catch (Exception exception) { MessageBox.Show(exception.Message); }}
3、其它示例
/// <summary>/// 其它/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void buttonOther_Click(object sender, EventArgs e){ try { // PDF文件 string fileName = string.Format(@"D:\Temp\{0}", "20240512193805.pdf"); // 判断文件是否存在 if (!File.Exists(fileName)) { MessageBox.Show(string.Format("【{0}】文件不存在!", fileName)); return; } // 开始组件装载文件 using (PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(fileName)) { // 获取文档总页数 int pageCount = pdfDocument.PageCount; // 获取文件信息 PdfInformation pdfInformation = pdfDocument.GetInformation(); // 在文件中搜索指定内容 var finder = pdfDocument.Search("文本",false,false); } } catch (Exception exception) { MessageBox.Show(exception.Message); }}
小结
以上是PdfiumViewer组件简单介绍,通过实现打印与查看示例了解其使用方式。对.NET 6及以上版本使用PdfiumViewer.Core,有兴趣的可以试试。
该文章在 2024/7/2 8:35:40 编辑过