Solved

Printing PDF document using pd_PrintPage in C#

Posted on 2007-11-30
6
6,496 Views
Last Modified: 2010-04-21
I am trying to use the following code to print a PDF document from a C# application. It works for a JPEG file but not for a PDF file. I get a message saying, "Not a valid Parameter" while trying to use PDF. I am using Bitmap in ev.Graphics.DrawImage. Can any one help me please? Thank you.


private void pd_PrintPage(object sender, PrintPageEventArgs ev)

        {

            Rectangle rect = new Rectangle(10,10, 500, 500);

           // Rectangle rectText = new Rectangle(40, 40, 300, 100);

           // StringFormat sf = new StringFormat();

          //  Bitmap b = new Bitmap("C:\\Program Files\\FSH Communications\\RoutePrt\\tmp\\rp2007112910331855.pdf");

            Bitmap b = new Bitmap("C:\\Program Files\\FSH Communications\\RoutePrt\\tmp\\test3.jpg");

            ev.Graphics.DrawImage(b, rect);

          //  ev.Graphics.DrawString("TEXT", new Font("Impact", 20), new SolidBrush(Color.Red), rectText, sf);

        }

Open in new window

0
Comment
Question by:sstukwila
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
The Bitmap class does not support the PDF format; so you cannot load it the way you've attempted to.

Two possible options for you, are:

  - Convert your PDF's into TIFF format (as Bitmap supports TIFF); or

  - Download a PDF library
    (here's a list: http://csharp-source.net/open-source/pdf-libraries).
     These libraries allow you to draw to the PDF's (if you wish to), and
     can send the PDF's straight to the printer.
0
 

Author Comment

by:sstukwila
Comment Utility
Hi Mr. InteractiveMind,

Thanks a lot for your guidance. I am not seeing any direct way of converting PDF's to TIFF's inside C#. These PDF files are created inside C#. Can you please let me know if there is a way of doing this inside C#?

Reg Open Source PDF library, I have never used an open source before. So, I am not sure how it works. PDFSharp says that it allows sending output to a printer. In my case, I have the PDF's created already. I just need to find a way to print them out at runtime. I will give a try by downloading the PDFSharp files.

Thanks a lot.
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
Comment Utility
You would need to use an external library to convert the PDF's to TIFF's.
Unfortunately, most of them cost money.

Your best (free) option would really be to go with one of the open source libraries (PDFSharp seems like a good choice), and print directly from that.

Let me know how you get on with PDFSharp.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sstukwila
Comment Utility
Hi Mr. Interactive Mind,

PDFSharp worked. I had to work on it for may be 1 hour to get it work. It is a big relief for me. Thanks a lot for your help. I wanted to check with you to see if it is safe and secure to use open source code like this? Can you please let me know your thoughts on this? Thank you.
0
 

Author Closing Comment

by:sstukwila
Comment Utility
Thank you
0
 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
Glad to hear it's working.

In many cases, especially for these popular open source projects, the code is often "safer" and more "secure" than typical industry-standard code - because the software has been tested and used by hundreds, or even thousands, of developers. So, especially for the later versions, practically all bugs have been ironed out.

Best of luck.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now