Click here to Skip to main content
6,629,885 members and growing! (21,124 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » File Formats     Intermediate License: The Code Project Open License (CPOL)

Converting Image Files to PDF

By blackjack2150

An article on converting image files (BMP, PNG, GIF, JPEG, TIFF) to PDF
C# (C# 2.0), Windows, .NET (.NET 2.0), Dev
Posted:15 Aug 2008
Views:24,095
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 4.34 Rating: 4.17 out of 5
1 vote, 9.1%
1

2
2 votes, 18.2%
3
3 votes, 27.3%
4
5 votes, 45.5%
5
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)

About the Author

blackjack2150


Member

Occupation: Software Developer
Location: Romania Romania

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralPDFsharp samples PinmemberDagdason13:01 26 Oct '09  
GeneralRe: PDFsharp samples Pinmemberblackjack21505:34 27 Oct '09  
Generalmore than one image file to one PDF file Pinmembereng walaa anwar5:01 7 Oct '09  
GeneralRe: more than one image file to one PDF file PinmemberDagdason12:54 26 Oct '09  
Generalhide the text in specified position? Pinmemberraj2313620:31 15 Sep '09  
Generalmerge Pdf problem... Pinmemberraj23136222:07 17 Aug '09  
GeneralRe: merge Pdf problem... Pinmemberblackjack215023:19 18 Aug '09  
GeneralProblem on converting multipage tiff image file PinmemberR a j en4:36 6 Aug '09  
GeneralRe: Problem on converting multipage tiff image file PinmemberDagdason12:55 26 Oct '09  
GeneralExport Images From PDF? Pinmemberthund3rstruck10:46 18 Jun '09  
GeneralRe: Export Images From PDF? Pinmemberblackjack21501:57 19 Jun '09  
GeneralRe: Export Images From PDF? Pinmemberthund3rstruck3:50 19 Jun '09  
QuestionImage in center of the PDF file? PinmemberR a j en5:32 16 May '09  
AnswerRe: Image in center of the PDF file? Pinmemberblackjack215023:57 19 May '09  
GeneralException in when converting BMP to PDF Pinmemberkingmax5421200816:44 30 Mar '09  
GeneralRe: Exception in when converting BMP to PDF Pinmemberblackjack21503:27 1 Apr '09  
GeneralRe: Exception in when converting BMP to PDF Pinmemberkingmax5421200821:17 2 Apr '09  
GeneralRe: Exception in when converting BMP to PDF Pinmemberblackjack215023:54 2 Apr '09  
GeneralMy vote of 1 PinmemberDr. Russell7:11 21 Jan '09  
GeneralRe: My vote of 1 Pinmemberblackjack21506:58 24 Jan '09  
GeneralConcatinating multiple pdfs.. Pinmembermkrishna1$12:35 24 Dec '08  
QuestionConverting PDF File to images PinmemberMember 441709418:24 16 Dec '08  
AnswerRe: Converting PDF File to images Pinmemberblackjack215022:11 17 Dec '08  
QuestionIt's possible to use this lib (pdfsharp) on vb.net? [modified] Pinmemberla_morte7:29 16 Dec '08  
AnswerRe: It's possible to use this lib (pdfsharp) on vb.net? Pinmemberblackjack21503:47 17 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Aug 2008
Editor: Sean Ewington
Copyright 2008 by blackjack2150
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project