简体   繁体   中英

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.

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.

So my question is....How can I use the current PrintDocument and generate a PDF in memory from it??

Thanks

The System.Drawing code for a PrintDocument can be reused to generate a PDF document with ABCpdf .NET. See the System.Drawing example...

You would have to use a 3rd party component in order to generate the PDF. The following article has some links to some such components: Generating PDF Files from .Net

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.

You might be in luck, however, as it appears that PDFsharp + MigraDoc might be able to do this for you.

I think you will find there is not any tools that will take a PrintDocument as input and render a PDF as output. The only way to do what you want is to "print" the PrintDocument to a "PDF printer driver" that will generate PDF. Basically a virtual printer that will generate PDF instead of printing the actual output. 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)

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/

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.

iTextSharp is a great tool for generating PDFs with .NET on the Internet. I highly recommend it; I've used iText with Java...and have been using iTextSharp for the past few years.

There are several ports of iText for .NET (A very popular open-source PDF library for Java).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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