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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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.

Method 4

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:

Method 5

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.

Method 6

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x