I have to use a 3rd party SDK that generates reports in a System.Drawing.Printing.PrintDocument
format. Since my project is not a Windows Form project(it is a Web project),I need to convert this System.Drawing.Printing.PrintDocument
to a PDF. I have downloaded iTextSharp and SharpPDF but they seem to create a new PDF. I have also gone through the tutorial(s) here: http://sharppdf.sourceforge.net/Tutorials.html. My requirement is to convert the document that I already have into a PDF. Is there something that I'm missing in using these libraries? Can someone please provide a sample code to achieve this?

- 369
- 2
- 9
- 26
2 Answers
How about installing a virtual PDF printer and "print" the document? For example - http://www.dopdf.com/, or http://www.cutepdf.com/products/cutepdf/writer.asp?
A code sample of how to print a PrintDocument, can be found here http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx
Another possible course would be take the Graphics from the PrintDocument (http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage(v=vs.80).aspx and look at what is proposed here - http://www.websupergoo.com/helppdfnet/source/4-examples/20-systemdrawing.htm

- 2,847
- 15
- 18
-
I dont want to "Print" the document.I have to store it as a BLOB in database.Since "System.Drawing.Printing.PrintDocument" is not Serializable,I cant use FileStream to stream it. I would have to convert the System.Drawing.Printing.PrintDocument object into a .doc/pdf or any other serializable object before I can store it in the database as BLOB. Do you have a sample code that would do this using cutepdf/dopdf? – user1550951 Aug 01 '13 at 07:44
-
When you print to a virtual printer, it'll create a pdf file. You could then read the file and store its contents in a BLOB in a DB. This is obviously not a good solution, but it's a way to perform what you need. – Vadim Aug 02 '13 at 07:39
-
Thank your for the "print to a virtual printer" hint. :) But how can I do this? Can you provide some sample code please? – user1550951 Aug 02 '13 at 07:45
-
I also updated my answer with another possible course of action. It look more promising. Check if it suits your needs and if not I'll try to provide some code sample. P.S - though I did provide the print solution, and implemented something like this myself once (and it did work), I don't think the solution is good. It's slow, not extensible and has other issues. It is only meant to be used as a last resort. – Vadim Aug 02 '13 at 07:49
you can create document by print to file in .net
exapmle : convert printDocument to XPS file , try code:
private void button8_Click(object sender, EventArgs e)
{
Addpage = 0;
printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
printDocument1.DefaultPageSettings.PrinterSettings.PrintToFile = true;
printDocument1.DefaultPageSettings.PrinterSettings.PrintFileName = "d:\\ddd11.xps";
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
}
in future time we will get convert printDucument to pdf file .
thank you

- 19
- 2