如何使用 C# 打印 PDF [英] How to print a PDF with C#
问题描述
我已经尝试解决这个问题将近 2 天了.网络上或多或少有很多好的解决方案,但没有一个完全适合我的任务.
I´ve trying to solve this problem for nearly 2 days. There are a lot of more or fewer good solutions on the net, but not a single one fits my task perfectly.
- 以编程方式打印 PDF
- 使用固定打印机进行操作
- 不要让用户执行多个 Button_Click
- 保持沉默 - 越多越好
- 做客户端
使用 Forms.WebBrowser
如果我们安装了 Adobe Reader,则有一个插件可以在网络浏览器中显示 PDF.使用这个解决方案,我们有一个很好的预览,使用 webbrowserControlName.Print() 我们可以触发控件打印其内容.
If we have Adobe Reader installed, there is a plugin to show PDF´s in the webbrowser. With this solution we have a nice preview and with webbrowserControlName.Print() we can trigger the control to print its content.
问题 - 我们还有一个 PrintDialog.
Problem - we still have a PrintDialog.
使用启动参数启动 AcroRd32.exe
下面的 CMD 命令让我们使用 Adobe Reader 打印我们的 PDF.
The following CMD command let us use Adobe Reader to print our PDF.
InsertPathTo..AcroRd32.exe/t "C:sample.pdf" "printerNetworkprinterName"
问题 - 我们需要 AcroRd32.exe 的绝对路径 |有一个 Adobe Reader Window 打开,它必须打开,直到打印任务准备就绪.
Problems - we need the absolute path to AcroRd32.exe | there is an Adobe Reader Window opening and it has to be opened until the print task is ready.
使用 Windows 预设
Process process = new Process();
process.StartInfo.FileName = pathToPdf;
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = """ + printerName + """;
process.Start();
process.WaitForInputIdle();
process.Kill();
问题 - 仍然弹出一个 Adobe Reader 窗口,但在打印完成后它通常会自行关闭.
Problem - there is still an Adobe Reader window popping up, but after the printing is done it closes itself usually.
解决方案 - 说服客户使用福昕阅读器(不要使用最后两行代码).
Solution - convince the client to use Foxit Reader (don´t use last two lines of code).
将 PDF 页面转换为 Drawing.Image
我不知道如何用代码来做,但是当我让它工作时,剩下的就是小菜一碟了.Printing.PrintDocument 可以满足所有需求.
I´ve no idea how to do it with code, but when I get this to work the rest is just a piece of cake. Printing.PrintDocument can fulfill all demands.
最好的问候,最大
推荐答案
我能找到的最灵活、最简单、性能最好的方法是使用 GhostScript.可以直接按打印机名称打印到windows打印机.
The most flexible, easiest and best performing method I could find was using GhostScript. It can print to windows printers directly by printer name.
"C:Program Filesgsgs9.07ingswin64c.exe" -dPrinted -dBATCH -dNOPAUSE -sDEVICE=mswinpr2 -dNoCancel -sOutputFile="%printer%打印机名称" "pdfdocument.pdf"
"C:Program Filesgsgs9.07ingswin64c.exe" -dPrinted -dBATCH -dNOPAUSE -sDEVICE=mswinpr2 -dNoCancel -sOutputFile="%printer%printer name" "pdfdocument.pdf"
添加这些开关以将文档缩小到 A4 页面.
Add these switches to shrink the document to an A4 page.
-sPAPERSIZE=a4 -dPDFFitPage
-sPAPERSIZE=a4 -dPDFFitPage
这篇关于如何使用 C# 打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!