Click here to Skip to main content
12,999,653 members (42,373 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 24 May 2015

Create PDF from Images using VB.NET

, 24 May 2015
Rate this:
Please Sign up or sign in to vote.
Create PDF from Image files using VB.NET and PDFSharp library.


Hello! This is my first article on Codeproject. I hope you will enjoy this tip. In this tip, I will show you how to convert images to PDF document. Of course, because we're converting from images, the PDF document will not be editable.


I'm a Naruto fan, and I want to download all of the Naruto manga to my PC. Because this manga started from 1997 and I just like Naruto since 2014, I realized that the number of Naruto manga is already a lot. So I started downloading from the first chapter. It was 20 pages and an image file.

I tried many online services to convert images to PDF, but it was limited to 40 pages only. So I started to write my own PDF creator. I have found many apps to do this, but I was interested in creating my own.

Using the Code

Code walkthrough:

  • Create a new blank PDF document.
  • Find all files on a specified directory.
  • Move supported image file paths to new array.
  • On the For...Next loop, add new PDF page with image as the content.
  • Save the PDF document to file.

First, you must create new Windows Forms Application. I'm using .NET Framework 2.0 for maximum compatibility.

Add controls like this:

Also, I added SaveFileDialog, FolderBrowseDialog, and BackgroundWorker.

Second, add reference to PDFSharp library. You can add it from Nuget Package or download it from official website.

Third, this is the core of code. I put this code in bwMakePDF_DoWork event handler.

Using pdf As New PdfDocument
    'Get parameter to process PDF.
    Dim info As WorkerParameters = DirectCast(e.Argument, WorkerParameters)

    'Find all files on source directory (sub-directories not included).
    Dim allFiles = Directory.GetFiles(info.InputDirectory, "*.*", SearchOption.TopDirectoryOnly)

    'Function to find compatible file types.
    'In .NET 4.0, you can use LINQ to do this.
    Dim findMatch = Function(filePath As String)
                        'PDFSharp only supports PNG, BMP, JPG, and GIF.
                        Return filePath.ToLower.EndsWith(".png") Or _
                               filePath.ToLower.EndsWith(".bmp") Or _
                               filePath.ToLower.EndsWith(".jpg") Or _
                    End Function

    'Find entire array.
    Dim filesToProcess = Array.FindAll(Of String)(allFiles, findMatch)

    'Get file count.
    Dim fileCount As Integer = filesToProcess.Length - 1

    'Add PDF pages.
    For i As Integer = 0 To fileCount
        'Check if operation is cancelled.
        If bwMakePdf.CancellationPending Then
            Exit For
        End If

        'Create new PDF page.
        Dim page = pdf.AddPage()

        'Create XImage object from file.
        Using xImg = PdfSharp.Drawing.XImage.FromFile(filesToProcess(i))
            'Resize page Width and Height to fit image size.
            page.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
            page.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution

            'Draw current image file to page.
            Dim GR = PdfSharp.Drawing.XGraphics.FromPdfPage(page)
            GR.DrawImage(xImg, 0, 0, page.Width, page.Height)
        End Using

        'Report progress.
        bwMakePdf.ReportProgress((i / fileCount) * 100)

    'Erase all items in array. Memory leaking free...
    Erase filesToProcess

    'Save to PDF file.
End Using

Don't forget to rename all controls on your form to fit the above code.

Points of Interest

No more using online services to convert images to PDF. No more using paid apps to create PDF. Just a little application to do this, without any limitation.


  • May 23rd, 2015: Initial post


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


About the Author

Fahmi Noor Fiqri
Student Blog Fahmi
Indonesia Indonesia
Fahmi Noor Fiqri is a student od SMP Negeri 1 Leuwiliang.
When he has free time, he makes program and helps in the programming group.

You may also be interested in...


Comments and Discussions

SuggestionVery Good... Pin
MayurDighe27-May-15 3:52
professionalMayurDighe27-May-15 3:52 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 24 May 2015
Article Copyright 2015 by Fahmi Noor Fiqri
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid