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

相关推荐
heimeiyingwang9 小时前
【架构实战】状态机架构:订单/工单状态流转设计
观察者模式·架构·wpf
Muyuan199815 小时前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
开开心心就好20 小时前
近200个工具的电脑故障修复合集
安全·智能手机·pdf·电脑·consul·memcache·1024程序员节
其实秋天的枫21 小时前
2026年初中英语大纲词汇表1600词
经验分享·pdf
开开心心_Every1 天前
轻量级PDF阅读器,仅几M大小打开秒开
linux·运维·服务器·安全·macos·pdf·phpstorm
福大大架构师每日一题1 天前
ragflow v0.25.1 最新版发布:API 统一、PDF 解析性能大幅优化、连接器删除同步全面增强,更新要点一次看懂
pdf·ragflow
KmSH8umpK2 天前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第三篇
redis·分布式·wpf
cosinmz2 天前
图片太多太乱怎么整理?分享一个我最近常用的图片转 PDF方法
经验分享·小程序·pdf
KmSH8umpK2 天前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案
redis·分布式·wpf
武藤一雄2 天前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf