MarkB29
MarkB29

Reputation: 4892

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

Upvotes: 6

Views: 20329

Answers (6)

AffineMesh
AffineMesh

Reputation: 1045

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

Upvotes: 2

petegordon
petegordon

Reputation: 339

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.

Upvotes: 0

Scott Willeke
Scott Willeke

Reputation: 9335

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:

Upvotes: 0

user1228
user1228

Reputation:

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.

Upvotes: 0

Lilith River
Lilith River

Reputation: 16468

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

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

Upvotes: -1

Adam Gritt
Adam Gritt

Reputation: 2674

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

Upvotes: 0

Related Questions