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

相关推荐
企鹅侠客2 小时前
开源免费文档翻译工具 可支持pdf、word、excel、ppt
人工智能·pdf·word·excel·自动翻译
界面开发小八哥3 小时前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计4 小时前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss6 小时前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
蒋劲豪6 小时前
WPF项目暴露WebApi接口;WinForm项目暴露WebApi接口;C#项目暴露WebApi接口;
开发语言·c#·wpf
近冬的阳光7 小时前
PDF文档管理系统V2.0
pdf
Driver_tu7 小时前
在windows10上基于Python部署marker,实现PDF转markdown文件(保姆级)
pdf
黄铎彦7 小时前
使用GDI+、文件和目录和打印API,批量将图片按文件名分组打包成PDF
c++·windows·pdf
梅如你7 小时前
IEEE官方期刊缩写查询pdf分享
pdf
jxf_jxfcsdn11 小时前
python读取pdf文档
开发语言·python·pdf