views:

678

answers:

6

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?

A: 

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).

casperOne
A few years ago, I read something about embedding a special control codes in the printed document that Adobe Acrobat (Distiller) recognized to set the filename of the PDF file to generate.
Philippe Leybaert
A: 

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.

taserian
AFAIK, iTextSharp uses a custom API to generate PDF documents, instead of the standard GDI drawing methods used in standard .NET printing. As stated in my question, this is not what I want
Philippe Leybaert
A: 

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.

John Fisher
A: 

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

Ryan ONeill
I checked their documentation, and it seems to me that they use a custom API to generate PDF documents, instead of the standard GDI drawing methods used in standard .NET printing. As stated in my question, this is not what I want.
Philippe Leybaert
My mistake, I missed the GDI part of your question.
Ryan ONeill
A: 

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:

Philippe Leybaert
+2  A: 

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.

AffineMesh94464