简体   繁体   English

使用System.Drawing.Printing.PrintDocument在内存中生成PDF

[英]Use a System.Drawing.Printing.PrintDocument to generate a PDF in memory

Does anyone know if the following is possible and if so what the best way of doing it is for free? 有谁知道以下是否可能,如果是这样,最好的方法是免费的?

I am generating a PrintDocument in a project I am currently working on and displaying a print dialog box so a user can choose which printer they want to use etc. The is currently a windows form application and if a user wants to print to a PDF they can select to print to CutePDF or something similar. 我正在我正在处理的项目中生成PrintDocument并显示一个打印对话框,以便用户可以选择他们想要使用的打印机等。目前是Windows窗体应用程序,如果用户想要打印到PDF,可以选择打印到CutePDF或类似的东西。

However I am now putting a ASP.Net web frontend on the application and want to use the same code to generate the PrintDocument but want to print it to a PDF on the fly and serve it up via the Response stream in the format of a PDF download. 但是我现在在应用程序上放置一个ASP.Net Web前端,并希望使用相同的代码生成PrintDocument但是想要动态地将其打印到PDF并通过Response流以PDF格式提供它下载。

So my question is....How can I use the current PrintDocument and generate a PDF in memory from it?? 所以我的问题是......我怎样才能使用当前的PrintDocument并在内存中生成PDF?

Thanks 谢谢

The System.Drawing code for a PrintDocument can be reused to generate a PDF document with ABCpdf .NET. 可以重用PrintDocument的System.Drawing代码来生成带有ABCpdf .NET的PDF文档。 See the System.Drawing example... 请参阅System.Drawing示例...

You would have to use a 3rd party component in order to generate the PDF. 您必须使用第三方组件才能生成PDF。 The following article has some links to some such components: Generating PDF Files from .Net 以下文章提供了一些此类组件的链接: 从.Net生成PDF文件

You're in a world of hurt if you think you're going to run the "same code" that deals with printers in both a forms app and an ASP.NET app. 如果您认为自己要运行与表单应用程序和ASP.NET应用程序中的打印机相同的“相同代码”,那么您将处于一个充满伤害的世界。

You might be in luck, however, as it appears that PDFsharp + MigraDoc might be able to do this for you. 然而,你可能很幸运,因为看起来PDFsharp + MigraDoc可能能够为你做到这一点。

I think you will find there is not any tools that will take a PrintDocument as input and render a PDF as output. 我想你会发现没有任何工具可以将PrintDocument作为输入并将PDF渲染为输出。 The only way to do what you want is to "print" the PrintDocument to a "PDF printer driver" that will generate PDF. 执行所需操作的唯一方法是将PrintDocument“打印”到将生成PDF的“PDF打印机驱动程序”。 Basically a virtual printer that will generate PDF instead of printing the actual output. 基本上是一个虚拟打印机,它将生成PDF而不是打印实际输出。 There are a plethora of products on the market for that. 市场上有很多产品。 A couple that are cheap and widely used are as follows: 便宜且广泛使用的一对夫妇如下:

You really should be looking at iTextSharp (it is mentioned on the iText.NET page recommended earlier) 你真的应该看看iTextSharp(它在前面推荐的iText.NET页面上提到过)

http://itextsharp.sourceforge.net/ http://itextsharp.sourceforge.net/

PrintDocument is meant for Windows Forms applications but is up and coming in SilverLight, see this video... http://silverlight.net/learn/videos/all/printing-api-basics/ PrintDocument适用于Windows窗体应用程序,但即将推出SilverLight,请参阅此视频... http://silverlight.net/learn/videos/all/printing-api-basics/

If you wish to continue with the PrintDocument and a web application, I think SilverLight 4 (which is beta right now) is the only way to go, or your going to have to have a lite weight windows form application installed locally for the end user that maybe uses web services. 如果您希望继续使用PrintDocument和Web应用程序,我认为SilverLight 4(现在是测试版)是唯一的方法,或者您必须在本地为最终用户安装轻量级Windows窗体应用程序也许使用网络服务。

iTextSharp is a great tool for generating PDFs with .NET on the Internet. iTextSharp是一个很棒的工具,用于在Internet上使用.NET生成PDF。 I highly recommend it; 我强烈推荐它; I've used iText with Java...and have been using iTextSharp for the past few years. 我已经将iText与Java一起使用了...过去几年一直在使用iTextSharp。

There are several ports of iText for .NET (A very popular open-source PDF library for Java). iText for .NET有几个端口(一个非常流行的Java开源PDF库)。

http://www.ujihara.jp/iTextdotNET/en/ http://www.ujihara.jp/iTextdotNET/en/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM