LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#使用开源免费库 PdfiumViewer 实现PDF打印与查看

admin
2024年7月1日 16:14 本文热度 601

前言

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、使用和附录

github地址:
https://github.com/pvginkel/PdfiumViewer
https://github.com/TimChen44/PdfiumViewer.Core
通过包管理器安装下面几个包:
PdfiumViewer
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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved