C# Convert JPG to PDF (Code Example Tutorial)

JPG or JPEG (Joint Photographic Experts Group) is a commonly used method of lossy compression for digital images, particularly images produced by digital photography. When you save a photo to your phone or computer, it is usually saved as a JPG image. If you need a PDF version of your photo, you can easily convert it using software already on your computer, phone, or tablet. In the case where you need to do this programmatically in C#, then the IronPDF library is the super-fast option to convert JPG images to PDF files. In this article, we will walk you through converting JPG formats to PDF format.

Topics covered in the Tutorial

In this tutorial, the following topics will be covered:

  1. Introduction to the IronPDF Library
  2. Create a C# Project
  3. Install the IronPDF Library

    • Method 1: NuGet Package Manager Solution
    • Method 2: NuGet Package Manager Console
    • Method 3: Using the DLL file
  4. Add the IronPDF Namespace
  5. Convert JPG Images to PDF Documents
  6. Summary

Requirements when using IronPDF:

  • You should have basic knowledge of C# languages.
  • You should have a basic knowledge of Windows applications.

1. Introduction to the IronPDF Library

The IronPDF .NET PDF Library solution is a dream for developers, particularly software engineers who use C#. Using this excellent IronSoftware tool, you can easily create a core PDF library for .NET. IronPDF will ensure any PDF conversion from different formats is an effortless and time-saving process.

It also enables you to construct a .NET PDF library using HTML5, Javascript, CSS, and images. You can seamlessly edit, stamp, and add headers and footers to a PDF. Furthermore, it makes it very easy to read PDF text, extract images or convert image to PDF programmatically.

Some of the important features include:

  • Create PDF documents from HTML4/5, CSS, JavaScript, and images.
  • Generate PDF documents from URLs.
  • Load URLs with custom-network login credentials, HTTP headers, proxies, cookies, user-agents, and form variables, allowing login behind HTML login forms.
  • Encrypting and decrypting PDFs.
  • Merging existing PDF files.
  • Creating and editing PDF forms.

Here, we are focused on the conversion of a JPG image to a PDF document. IronPDF supports almost every image format for conversion. Supported formats are JPG, PNG, TIFF, GIF, SVG, and BMP.

2. Create a C# Project

Before starting, you should have some knowledge regarding top-level statements, as the basic code is extracted from C# 10.0.

Let's begin by creating a C# project.

  • Open Visual Studio 2022.
  • Create a new C# Project.
  • Give a name to the project.
  • Select a .NET core version for your project. I'll use .NET 6.0 for this project.

3. Install the IronPDF Library

Method 1: NuGet Package Manager Solution

Visual Studio supplies the NuGet Package Manager to download NuGet packages in your projects. You can access it through the Tools Menu, or by right-clicking your project in Solution Explorer.

Open from Solution Explorer

Open from Solution Explorer

Once the NuGet Package Manager Solution panel is open, search for the IronPDF library. Select install.

Browse IronPDF

Browse IronPDF

Method 2: NuGet Package Manager Console

We can use the NuGet Package Manager Console to easily install our library. Administrative privilege is not required for installation. Use a NuGet command to install the IronPDF library in your project. Copy the code snippet in the NuGet Package Manager Console and press enter. IronPDF library will be installed and ready to use in your project.

Install-Package IronPDF

Method 3: Using DLL File

You can also download IronPDF .DLL file directly from the website from this link.

After unzipping the file, reference the library in your project by following these steps:

  • Right-click the Solution in the Solution Explorer
  • Select "References"
  • Browse for the IronPDF.dll library
  • Click OK

All done! IronPDF is downloaded, installed, and ready to use to convert JPG to PDF format.

4. Add the IronPDF Namespace

Now add the IronPDF namespace to your program. You have to add the provided line of code to the top of the file.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

This will allow you to access the functions of the IronPDF library. You are required to add this line of code to every file in order to use the IronPDF features.

5. Convert JPG Images to PDF Format

The Conversion of JPG images to PDF files can be achieved in a single line with IronPDF. The code is neat, clean, and understandable. This task can be achieved using the IronPDFs ImageToPDF method. Follow the steps below to convert any JPG image to a PDF document.

First, place all JPG file formats to be converted inside a folder named assets located in the directory of the project. The folder must be placed in the location: bin\Debug\net6.0.

Then using System.IO.Directory, enumerate the assets folder with all the JPG files and pass it to the ImageToPDF method for PDF conversion. The following code snippet helps convert JPG to PDF images and saves it to a new document.

using IronPdf;

Console.WriteLine("C# Convert Images (JPG to PDF) using IronPDF");

// Selects all JPG/JPEG images in the folder 'assets'.
var Image = System.IO.Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));

// Converting image to PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf");

// Print success message
Console.WriteLine("JPG successfully converted to PDF using C#");
using IronPdf;

Console.WriteLine("C# Convert Images (JPG to PDF) using IronPDF");

// Selects all JPG/JPEG images in the folder 'assets'.
var Image = System.IO.Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));

// Converting image to PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf");

// Print success message
Console.WriteLine("JPG successfully converted to PDF using C#");
Imports IronPdf

Console.WriteLine("C# Convert Images (JPG to PDF) using IronPDF")

' Selects all JPG/JPEG images in the folder 'assets'.
Dim Image = System.IO.Directory.EnumerateFiles("assets").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))

' Converting image to PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf")

' Print success message
Console.WriteLine("JPG successfully converted to PDF using C#")
VB   C#

In the above code snippet, the asset folder contains only one JPG image. The output file looks like this:

Single JPG file to PDF file

Single JPG file to PDF file

The same code example can be used to convert multiple JPG files. The output contains three JPG images to a PDF document.

Multiple JPG Images to PDF Document

Multiple JPG Images to PDF Document

6. Summary

This tutorial shows how we can convert JPG images to PDF documents using the IronPDF C# library. The manipulation and formatting of PDF files become significantly effortless using the IronPDF library function. All that is required is just a few lines of code to create a PDF document from JPG files. This assists in sending all images in a single PDF document, conserving time in uploading and downloading. You can also convert TIFF and other image formats using the same method just like the JPG tutorial.

You can try the free version of IronPDF to test it out and with a free 30-day trial key, you can test the functionality of IronPDF. Furthermore, the current special offer allows you to get five products from IronPDF for the price of just two! Information regarding licensing can be found at this link.

You can download the software product from this link.