如何在WPF中打印PDF文件

最近遇到有客户需要打印PDF的需求,这里分享一下两种解决方案:

1、使用"谓词(verb)"

当用户右键单击 Shell 对象(如文件)时,Shell 会显示 (上下文) 菜单的快捷方式。 此菜单包含一个命令列表,用户可以选择这些命令对项执行各种操作。 这些命令也称为快捷菜单项或谓词。 可以自定义快捷菜单。

当我们在创建进程时,Verb 指定为print 时,系统会到注册表寻找当前类型文件的注册程序节点下的shell/print/command,并执行其中的命令。

例如我本地默认的PDF打开程序是Foxit Reader,当使用下面的代码执行时:

复制代码
1 Process p = new Process( );
2 p.StartInfo = new ProcessStartInfo( )
3 {
4     CreateNoWindow = true,
5     Verb = "print",
6     FileName = path //PDF文件路径
7 };
8 p.Start( );

系统会找到如下路径:

计算机\HKEY_CLASSES_ROOT\FoxitReader.Document\shell\print\command

并调用里面的命令:

%1代表的是文件路径。

2、使用PdfiumViewer库

PdfiumViewer 库是一个基于PDFium 项目的PDF查看器。项目地址:GitHub - pvginkel/PdfiumViewer: PDF viewer based on Google's PDFium.

PDFiumChrome 所使用的的PDF渲染引擎。项目地址:GitHub - chromium/pdfium: The PDF library used by the Chromium project

使用示例代码如下:

复制代码
 1   public bool PrintPDF(string printer,string paperName,int copies, Stream stream, Duplex duplex)
 2   {
 3       try
 4       {
 5           var printerSettings = new PrinterSettings
 6           {
 7               PrinterName = printer,
 8               Copies = (short)copies,
 9               Duplex = duplex
10           };
11 
12           var pageSettings = new PageSettings(printerSettings)
13           {
14               Margins = new Margins(0, 0, 0, 0),
15           };
16 
17           foreach (PaperSize paperSize in printerSettings.PaperSizes)
18           {
19               if (paperSize.PaperName == paperName)
20               {
21                   pageSettings.PaperSize = paperSize;
22                   break;
23               }
24           }
25 
26           using (var document = PdfiumViewer.PdfDocument.Load(stream))
27           {
28               using (var printDocument = document.CreatePrintDocument())
29               {
30                   printDocument.PrinterSettings = printerSettings;
31                   printDocument.DefaultPageSettings = pageSettings;
32                   printDocument.PrintController = new StandardPrintController();
33                   printDocument.Print();
34               }
35           }
36           return true;
37       }
38       catch (Exception ex)
39       {
40           return false;
41       }
42   }

注意:需要配合PdfiumViewer.Native.x86_64.v8-xfa包使用,这是PDFium的运行时文件。

方法1简单方便,但是不支持设置打印机参数。

参考资料:

https://stackoverflow.com/questions/5566186/print-pdf-in-c-sharp

https://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print

相关推荐
HAPPY酷1 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
✎ ﹏梦醒͜ღ҉繁华落℘15 小时前
开发WPF项目时遇到的问题总结
wpf
代码AI弗森1 天前
PDF OCR + 大模型:让文档理解不止停留在识字
pdf·ocr
hqwest2 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars2 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest2 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
小周同学:2 天前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu2 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
hqwest2 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0072 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度