Click here to Skip to main content
11,501,561 members (71,557 online)
Click here to Skip to main content

Converting Image Files to PDF

, 15 Aug 2008 CPOL 190.5K 24K 97
Rate this:
Please Sign up or sign in to vote.
An article on converting image files (BMP, PNG, GIF, JPEG, TIFF) to PDF
ImageToPdf.JPG

Introduction

Hello everybody! This is my first article here. It's nothing fancy, but it can save a lot of trouble if you need to include a simple image -> PDF convertor in your .NET application. Please forgive any spelling or grammar mistakes which may follow.

Background

Some time ago I needed a TIFF to PDF converter. I found this article called Convert Tiff To PDF, which is written in Visual C++ 6 and uses the CXImage library, as well as LibTIFF.

As that article says, it only suppots TIFF Image files in Fax formats, whatever that means. I haven't looked in depth, but probably that's the reason why, during my testing, out of these 18 TIFF sample files, only 11 were converted to something that Adobe Reader could understand. They contain scanned text (from books and handwritten), technical drawings and pictures, so I suppose they cover a wide range of sub-formats.

So after the few minutes of joy during which I thought I had found the perfect solution, I was back to Google to look for something else. Luckily I found the wonderful freeware Open Source library called PDFSharp, which saved the day.

Using the Code

The purpose of the PDFSharp libray is to create PDF files from scratch as easy as possible. The methods it exposes are symilar to the GDI+ ones in .NET. So you can imagine that writing a PDF file is like drawing 'stuff' (text, shapes, images) in the Paint event of a Windows Forms Panel. With that knowledge, converting a BMP/PNG/GIF/JPEG/TIFF file to a PDF one is done like this:

  • create a new empty PDF document
  • add a blank page
  • get the XGraphics object
  • create the XImage from the source file
  • draw the image
  • save the PDF file

The core of the program is listed below. I used a BackgroundWorker, so that the UI will not hang during the conversion.

                
     PdfDocument doc = new PdfDocument();
     doc.Pages.Add(new PdfPage());
     XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
     XImage img = XImage.FromFile(source);

     xgr.DrawImage(img, 0, 0);
     doc.Save(destinaton);
     doc.Close();

Conclusion

Creating PDF files from images is very easy with PDFSharp.

For TIFF images, the downside is that the speed is significantly lower compared with the method described in the article Convert Tiff To PDF. I don't have rigorous data, but as an estimation, converting a large image (2.5 MB) takes around 2 seconds. The other method is about 10 times faster, but again it only supports some TIFF files. The performance is not that bad, but if you want to use this code to process multiple files in an application, be sure to do it asyncronously with th UI.

Note: In the download archives I included the PDFSharp library. If you want the latest version, be sure to visit their download page.

History

14 Aug 2008 - first submission of the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

blackjack2150
Software Developer
Romania Romania
No Biography provided

Comments and Discussions

 
QuestionCode for PdfSharp.dll Pin
nayana_a8526-May-15 21:07
membernayana_a8526-May-15 21:07 
QuestionGood Job! Pin
winhtay17-Nov-14 23:23
memberwinhtay17-Nov-14 23:23 
QuestionMore images for listbox Pin
Gabee82-Nov-13 22:40
memberGabee82-Nov-13 22:40 
Questionthank you Pin
jacaboo8-Sep-13 16:28
memberjacaboo8-Sep-13 16:28 
Questioncode for convert image,excel or word documents to pdf on dreamweaver Pin
Member 101477509-Jul-13 8:49
memberMember 101477509-Jul-13 8:49 
GeneralIt worked perfectly. five stars Pin
pianocomposer20-May-13 4:58
memberpianocomposer20-May-13 4:58 
GeneralMy vote of 5 Pin
Animo199410-May-13 10:34
memberAnimo199410-May-13 10:34 
QuestionCan i convert GIF image to PDF file Pin
Tridip Bhattacharjee9-Apr-13 4:32
memberTridip Bhattacharjee9-Apr-13 4:32 
Questioni can set the DPI ? Pin
waveiro21-Feb-13 9:33
memberwaveiro21-Feb-13 9:33 
QuestionThanks For This Great Project Pin
fireofwin17-Jul-12 22:50
memberfireofwin17-Jul-12 22:50 
GeneralMy vote of 5 Pin
GANESH289029-Mar-12 10:05
memberGANESH289029-Mar-12 10:05 
GeneralUseful with a nice touch Pin
joshd0626-Mar-12 3:17
memberjoshd0626-Mar-12 3:17 
QuestionI really appreciate for this work. This code helped me a lot. Thanx very much Pin
Member 43617134-Feb-12 6:05
memberMember 43617134-Feb-12 6:05 
GeneralMy vote of 5 Pin
Airelement07423-Jun-11 23:39
memberAirelement07423-Jun-11 23:39 
QuestionProcessing large TIFFs into PDFs: out of memory issue Pin
Member 31967208-Jun-11 3:33
memberMember 31967208-Jun-11 3:33 
AnswerRe: Processing large TIFFs into PDFs: out of memory issue Pin
nguyenvanlam16-May-12 6:46
membernguyenvanlam16-May-12 6:46 
I got the same problem. First time I converted 5 images to PDF successfully, the second time I converted only 2 others one but it got "Out of memory" exception
GeneralCompiling and Using this library Pin
ashkanreza3-May-11 3:17
memberashkanreza3-May-11 3:17 
GeneralMy vote of 4 Pin
Abhijit Vaidya2-Nov-10 0:28
memberAbhijit Vaidya2-Nov-10 0:28 
GeneralGraphics Pin
silkkeng10-Jun-10 21:13
membersilkkeng10-Jun-10 21:13 
GeneralRe: Graphics Pin
blackjack215011-Jun-10 10:04
memberblackjack215011-Jun-10 10:04 
GeneralFacing problem when tried to convert 3000*3000 dimensional image Pin
NarVish14-Mar-10 23:13
memberNarVish14-Mar-10 23:13 
GeneralRe: Facing problem when tried to convert 3000*3000 dimensional image Pin
blackjack215015-Mar-10 2:22
memberblackjack215015-Mar-10 2:22 
GeneralRe: Facing problem when tried to convert 3000*3000 dimensional image Pin
NarVish15-Mar-10 7:13
memberNarVish15-Mar-10 7:13 
GeneralRe: Facing problem when tried to convert 3000*3000 dimensional image Pin
NarVish16-Mar-10 18:48
memberNarVish16-Mar-10 18:48 
Generalpdf page my photo auto fit Pin
Fatih Sahinbas28-Feb-10 11:02
memberFatih Sahinbas28-Feb-10 11:02 
GeneralCompressed/Stretched image when opening PDF file Pin
eftexem19-Jan-10 8:38
membereftexem19-Jan-10 8:38 
GeneralPDFsharp samples Pin
Dagdason26-Oct-09 12:01
memberDagdason26-Oct-09 12:01 
GeneralRe: PDFsharp samples Pin
blackjack215027-Oct-09 4:34
memberblackjack215027-Oct-09 4:34 
Generalmore than one image file to one PDF file Pin
eng walaa anwar7-Oct-09 4:01
membereng walaa anwar7-Oct-09 4:01 
GeneralRe: more than one image file to one PDF file Pin
Dagdason26-Oct-09 11:54
memberDagdason26-Oct-09 11:54 
GeneralRe: more than one image file to one PDF file [modified] Pin
Maher Abu Zer6-Jun-11 4:50
memberMaher Abu Zer6-Jun-11 4:50 
GeneralRe: more than one image file to one PDF file Pin
ishwer18-Mar-13 0:22
memberishwer18-Mar-13 0:22 
GeneralRe: more than one image file to one PDF file Pin
mattsnuts7-Dec-10 7:26
membermattsnuts7-Dec-10 7:26 
Questionhide the text in specified position? Pin
raj23136214-Sep-09 23:31
memberraj23136214-Sep-09 23:31 
Generalmerge Pdf problem... Pin
raj23136217-Aug-09 21:07
memberraj23136217-Aug-09 21:07 
GeneralRe: merge Pdf problem... Pin
blackjack215018-Aug-09 22:19
memberblackjack215018-Aug-09 22:19 
GeneralProblem on converting multipage tiff image file Pin
R a j en6-Aug-09 3:36
memberR a j en6-Aug-09 3:36 
GeneralRe: Problem on converting multipage tiff image file Pin
Dagdason26-Oct-09 11:55
memberDagdason26-Oct-09 11:55 
QuestionExport Images From PDF? Pin
thund3rstruck18-Jun-09 9:46
memberthund3rstruck18-Jun-09 9:46 
AnswerRe: Export Images From PDF? Pin
blackjack215019-Jun-09 0:57
memberblackjack215019-Jun-09 0:57 
GeneralRe: Export Images From PDF? Pin
thund3rstruck19-Jun-09 2:50
memberthund3rstruck19-Jun-09 2:50 
QuestionImage in center of the PDF file? Pin
R a j en16-May-09 4:32
memberR a j en16-May-09 4:32 
AnswerRe: Image in center of the PDF file? Pin
blackjack215019-May-09 22:57
memberblackjack215019-May-09 22:57 
AnswerRe: Image in center of the PDF file? Pin
mattsnuts7-Dec-10 7:23
membermattsnuts7-Dec-10 7:23 
GeneralException in when converting BMP to PDF Pin
kingmax5421200830-Mar-09 15:44
memberkingmax5421200830-Mar-09 15:44 
GeneralRe: Exception in when converting BMP to PDF Pin
blackjack21501-Apr-09 2:27
memberblackjack21501-Apr-09 2:27 
GeneralRe: Exception in when converting BMP to PDF Pin
kingmax542120082-Apr-09 20:17
memberkingmax542120082-Apr-09 20:17 
GeneralRe: Exception in when converting BMP to PDF Pin
blackjack21502-Apr-09 22:54
memberblackjack21502-Apr-09 22:54 
GeneralRe: Exception in when converting BMP to PDF Pin
thuyhuynh30-Sep-10 22:40
memberthuyhuynh30-Sep-10 22:40 
GeneralMy vote of 1 Pin
Dr. Russell21-Jan-09 6:11
memberDr. Russell21-Jan-09 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Aug 2008
Article Copyright 2008 by blackjack2150
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid