4

I have a whole bunch of 2D graphics that is being used for both rendering controls on screen and used to print, it's pretty custom graphics that couldn't be done by any 3rd party reporting tools or off the shelf controls.

The program can generate PDF files of the printed content but when I wrote it I cheated and just print the Graphics object to an in memory image and then embed that into the PDF page.

Since the users are emailing the documents they're finding they are too large.

I've started writing the PDF from scratch using iText, however is there a way to get System.Drawing.Graphics content directly into PDF? The way iText works and Graphics works is completely different.

Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
  • I know that it's not a free library but [Windows Forms Infragistics.Documents Control](http://www.infragistics.com/dotnet/netadvantage/winforms/infragisticsdocuments.aspx#Overview) provides a way to easily compose PDF document. It's a dev licence, and you can test it for free. – JPBlanc Sep 16 '11 at 03:22

4 Answers4

4

try this library http://www.pdfsharp.com/PDFsharp/

Edit: Above library has been moved to http://www.pdfsharp.net/

jswolf19
  • 2,303
  • 15
  • 16
Eugen
  • 2,934
  • 2
  • 26
  • 47
  • With a fair bit of work this works perfectly. I actually had to duplicate everything and make subtle changes but it keeps my math consistent which is much easier than with iText. The library hasn't been updated in a couple of years so may contribute if needed :) – Brett Ryan Sep 19 '11 at 01:10
  • Yep, it is a fair bit of work - in the middle of it right now. It was soooo easy to use XPS, but my client insists on PDF. Ah well! ... it did help knowing you've pushed through and got a working solution :) – noelicus Dec 11 '12 at 17:34
1

You can direct what your drawing with Graphics to a WMF (EMF) and then hand that to iText.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • I didn't try this approach though did read up on it and this does look like a viable alternative to using PDF sharp. I may look into it further later, thanks for the answer. – Brett Ryan Sep 19 '11 at 01:15
1

Do you mean that you have a System.Drawing.Image? If so you can pass that directly to iTextSharp.text.Image.GetInstance() which will give you an iTextSharp Image object. Otherwise you can draw directly to the PDF canvas, see this post for more.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • I was using the image method previously though as mentioned my files sizes are too big and need to do direct drawing. Yes, I can use direct drawing as you mentioned though it's completely different to the GDI+ system and I need to start everything from scratch, I was looking for a more GDI+ like way of writing, Eugen's answer was just the ticket. – Brett Ryan Sep 19 '11 at 01:12
0

One (hacky) way would be to simply use a PDF printer driver like this one. You can reuse the same GDI+ code through the printing API.

Igor Brejc
  • 18,714
  • 13
  • 76
  • 95
  • I did investigate this approach though there's some caveats to this approach, as you mention for one it's hacky, the other is we don't save the files but instead write documents that get emailed without user intervention. I would also need to install drivers on machines which we don't want as we're using click-once, thanks though it is a viable alternative. – Brett Ryan Sep 19 '11 at 01:14