Click here to Skip to main content
15,887,477 members
Articles / Programming Languages / C# 5.0
Tip/Trick

Converting Multiple Image Files to PDF

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
30 Aug 2015CPOL 24.8K   2.7K   9   3
A tip on converting multiple image files (BMP, PNG, GIF, JPEG, TIFF) to single or multiple PDF

Introduction

This tip is an extension of the article Converting Image File to PDF.

Image 1

Background

Often, there is a need to convert many files into single PDF or many PDFs. This article describes a simple program allowing this. The program is based on the Open Source library called PDFSharp.

Instead of single TextBox for input files, there is a ListBox enabling to select more files. With Up/Down button, you can change the source files order. The order in the ListBox represents the order of the pages in the created PDF document. The source image files can be added as well via Drag&Drop operation from file explorer. Alternatively can be checked automatic delete of source files after succesful convertion.

Using the Code

Details about the conversion technique is described in the base article Converting Image File to PDF. The processing can run in two modus: single PDF with multiple pages, or multiple PDF with single page. The core worker thread is calling the PDFSharp library in the following way:

for (int i = 1; i < fNames.Length; i++)
{
    // each source file saeparate
    PdfDocument doc = new PdfDocument();
    toolStripStatusLabel1.Text = "Processing " + fNames[i];
    doc.Pages.Add(new PdfPage());
    XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
    XImage img = XImage.FromFile(fNames[i]);
    xgr.DrawImage(img, 0, 0);
    img.Dispose();
    xgr.Dispose();
    //  save to destination file
    FileInfo fi = new FileInfo(fNames[i]);

    doc.Save(fi.FullName.Replace(fi.Extension,".PDF"));
    doc.Close();
}

Points of Interest

This is a very simple WinForm application helping to produce PDF file in an effective way.

License

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


Written By
Software Developer (Senior)
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionNeed escale function Pin
Member 1450422913-May-20 0:31
Member 1450422913-May-20 0:31 
QuestionNo source code provided :( Pin
Tridip Bhattacharjee31-Aug-15 21:31
professionalTridip Bhattacharjee31-Aug-15 21:31 
QuestionSample project would have be nice Pin
fredatcodeproject31-Aug-15 1:34
professionalfredatcodeproject31-Aug-15 1:34 

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

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