Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#5.0

Converting Multiple Image Files to PDF

4.43/5 (3 votes)
30 Aug 2015CPOL 25.7K   2.7K  
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)