0

I am trying to print Form using PrintForm in VB.Net

This Form has few labels and a chart control. I have used this very simple code

 Dim pf As New PrintForm
    pf.Form = Me
    pf.PrintAction = PrintAction.PrintToFile
    pf.PrintFileName = "../../generated_pdf.pdf"
    pf.Print()

It generated "generated_pdf.pdf" file. When I am trying to open this file it gives me an error Couldn’t open PDF Something’s keeping this PDF from opening.

Any ideas on how to resolve this error and successfully generate a working pdf that can be opened and viewed with it's right contents?

The form that should be converted to pdf looks like this

Chart

Jimi
  • 29,621
  • 8
  • 43
  • 61
sam
  • 85
  • 9
  • 1
    You're probably not actually generating a valid .pdf file. – ouflak Sep 19 '18 at 16:24
  • 1
    Just giving something an extension doesn't magically make it a file of that type. A PDF file (just like every other file) has a defined type of content. You can't just give anything a PDF extension and expect it to somehow morph into the proper content type, any more than you can stick a sign on the side of your Prius that says *Ferrari* and expect it to suddenly be worth a hundred thousand dollars more. PrintForm generates an image, not a PDF. – Ken White Sep 19 '18 at 16:36
  • Can you open the PDF in another application, say, Adobe Reader? – Visual Vincent Sep 19 '18 at 16:47
  • Possible duplicate of [How do I generate a pdf document from vb.net ready for printing](https://stackoverflow.com/questions/24700088/how-do-i-generate-a-pdf-document-from-vb-net-ready-for-printing) – Simo Sep 19 '18 at 16:48
  • 2
    @VisualVincent The solution is to change some PrintForm property in order to change the printer to a PDFCreator or instead of print to open the preview window where you can choose a pdf printer as a printer. – Simo Sep 19 '18 at 16:50
  • I have edited the question and included that image of the form that has to be converted to pdf. – sam Sep 19 '18 at 17:32
  • @Simo : It appears so, but your suggested duplicate doesn't demonstrate how to do that (the OP already does everything that the duplicate does). If you know how, you could answer this question. – Visual Vincent Sep 19 '18 at 17:37
  • You need to add `pf.Dispose()` Odds that this makes a difference are however not great, do assume it is the printer driver that is causing this problem. Use Control Panel > Devices and Printers and tell us the name of the default printer. – Hans Passant Sep 19 '18 at 17:57
  • See this [CodeProject PDF Printer](https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version) class (stand alone, no need for external components, it just uses some WPF assemblies and classes). It also offers direct support for printing Charts. Updated 19 Jul 2018. If you can't rely on external printer drivers. – Jimi Sep 19 '18 at 18:12
  • @VisualVincent I never used the PrintForm class, so I'm not confident to give any answer, just some suggestions in the comment. – Simo Sep 19 '18 at 20:38

1 Answers1

0

PrintForm does not know how to handle PDF files, which is why its not working.

It only knows how to do .eps, .ps & .ai.

If you want PDF, you'll need something that knows how to turn one of the above formats into a PDF.

Ghostscript & Ghostscript.Net do this nicely and are free & open source. There are others, but I've actually used these and know they work.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32