如何在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

相关推荐
AI视觉网奇3 小时前
麒麟系统 doc转pdf
linux·运维·pdf
CodeCraft Studio4 小时前
国产化PDF处理控件Spire.PDF教程:如何在 Java 中通过模板生成 PDF
java·python·pdf·spire.pdf·java创建pdf·从html创建pdf
self_myth7 小时前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
c#上位机8 小时前
wpf之TextBlock
c#·wpf
janthinasnail1 天前
使用Docker安装Stirling-PDF(PDF工具)
docker·pdf
海斗星河万里长1 天前
ConvertAPI:PDF转Word的便捷之选
pdf
玉面小君2 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
weixin_584121432 天前
vue3+ts导出PDF
javascript·vue.js·pdf
Access开发易登软件2 天前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发