使用标准 .NET GDI 打印类从 .NET 生成 PDF 文件
我正在寻找一种使用 .NET 中的标准 PrintDocument 和 Graphics (GDI) 类生成 PDF 文件的方法。 据我所知,唯一的方法是使用 PDF 打印机进行打印。
问题是 PDF 打印机驱动程序总是要求提供文件名,但我需要从代码中控制文件名。 使用 PDFSharp 或 DynamicPDF 等 PDF 库不是一种选择,因为它们都提供自己的 API 来生成 PDF 文件。
我需要这个用于内部应用程序,所以依赖性不是问题。
我的问题很简单:有没有一种方法可以控制打印机驱动程序(Adobe Acrobat,PDFCreator,...),以便可以指定文件名并且不会提示用户输入任何内容?
I'm looking for a way to generate PDF files using the standard PrintDocument and Graphics (GDI) classes in .NET. As far as I know, the only way to do that is by printing to a PDF printer.
The problem is that a PDF printer driver always asks for a filename, but I need to control the filename from my code. Using a PDF library like PDFSharp or DynamicPDF is not an option, because they all provide their own API for generating PDF files.
I need this for an internal application, so dependencies are not a problem.
My question is simple: is there a way to control a printer driver (Adobe Acrobat, PDFCreator, ...) in such a way that a filename can be specified and the user is not prompted for anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
PrintDocument 的 System.Drawing 代码可以重复使用,通过 webSupergoo 的 ABCpdf.NET 生成 PDF。 有关更多详细信息,请参阅 System.Drawing 示例。 该组件不使用打印机驱动程序 - 它直接创建 PDF - 因此不需要指定文件名。
The System.Drawing code for a PrintDocument can be reused to generate a PDF with ABCpdf.NET from webSupergoo. See the System.Drawing example for more details. The component doesn't use a printer driver - it creates PDF directly - so doesn't require a filename to be specified.
一个名为 Amyuni 的产品可以做到这一点(我在一个项目中使用它),但我不喜欢许可,你可能会相处得很好。
请参阅 Amyuni.com
瑞安
A product called Amyuni does this (I use it in a project) but I don't like the licencing, you may get on fine with it.
See Amyuni.com
Ryan
Amyuni PDF Converter 可以满足您的需求,它提供了 Microsoft 认证的打印机驱动程序,您可以使用 COM 接口或 .net 程序集进行完全操作,并且可以使用标准 GDI(或 Windows.Forms 类)来生成 PDF 文件(您可能检查了 Amyuni PDF Creator,这是一个自定义 API,如您在对此答案的评论)。
Amyuni PDF Converter does what you need, it provides a Microsoft certified printer driver that you can fully manipulate using a COM interface or .net assembly, and you can use standard GDI (or Windows.Forms classes) to generate your PDF files (you probably checked on Amyuni PDF Creator, which is a custom API as you stated in your comments to this answer ).
除非您使用的打印机驱动程序手动支持此操作(据我所知,不存在这种驱动程序),否则您必须编写自己的打印机驱动程序,以便在打印操作期间能够向其传递信息(例如文件名) 。
这样做的缺点是您必须实现打印机驱动程序,并且它必须是非托管代码(您不能在 .NET 中编写驱动程序)。
Unless the printer drivers that you are using support this manually (of which none exist that I know of), you would have to write your own printer driver which you would be able to pass information to (such as the filename) during the print operation.
The drawbacks to this are the fact you would have to implement a printer driver, as well as the fact that it would have to be unmanaged code (you can't write drivers in .NET).
有几家公司开发了文档格式转换工具。 其中一些使用打印机驱动程序将文档“打印”成所需的格式。 寻找可以将 Word 文档转换为 PDF 的工具。 其中之一应该具有您需要的驱动程序,有些将提供允许您控制文件名等的 API。
There are several companies who create document format conversion tools. Several of them use printer drivers to "print" the document into the format desired. Look around for tools that can convert from Word documents to PDF. One of these should have the kind of driver you need, and some will offer an API that allows you to control filenames and such.
做了更多研究,虽然我仍然没有找到完美的解决方案,但有一些产品安装了 PDF 打印机驱动程序,并允许您以某种方式控制要生成的文件的名称:
Bullzip 和 PDFCreator 是免费的。
更新:发现另一个看起来非常有前途的:
Did some more research, and although I still didn't find a perfect solution, there are a few products that install a PDF printer driver and which allow you in some way to control the name of the file to be generated:
Bullzip and PDFCreator are free.
UPDATE: Found another one that looks very promising:
Dane-Prairie Systems http://www.win2pdf.com/ 似乎捕获了“打印到文件”字符串来自 GDI 调用,并将在您指定的位置创建 PDF。
这是手册中的引用:
彼得
Dane-Prairie Systems http://www.win2pdf.com/ seems to capture the "print to file" string from the GDI calls and will create your PDF whereever you spec.
Here's a quote from the manual:
Peter
C1Pdf 组件执行此操作。 它具有模仿 GDI plus(DrawString、DrawImage 等)并生成 PDF 的方法。 它不安装或不需要任何打印机驱动程序。 如果您需要更多详细信息,请参阅以下文档链接:
http:// www.componentone.com/newimages/Products/Documentation/WinForms.PDF.pdf
The C1Pdf component does this. It has methods that mimic GDI plus (DrawString, DrawImage, etc) and generates PDFs. It does not install or require any printer drivers. Here's a link to the documentation in case you want more detail:
http://www.componentone.com/newimages/Products/Documentation/WinForms.PDF.pdf
不确定这是否是您考虑的解决方案,但我使用过 iTextSharp 填写 PDF 表单并使用代码生成的文件名保存它们,而不是由用户生成。 它还可以从头开始生成完整的 PDF,但我还没有深入研究它。
Not sure if this is a solution you'd consider, but I've used iTextSharp to fill in PDF Forms and save them with a filename that's generated in code, not by the user. It also can generate complete PDFs from scratch, but I haven't delved that far into it.