How to Convert Image to PDF in C# [Code Example Tutorial]

There are multiple online tools and software available for converting images to PDF. Their number is increasing by the day. The main reason for the growing number of tools is their widespread application. People often need to transform their images to PDF for a variety of reasons. People often include documents in images, but some are required to be uploaded in PDF as it is a universally acceptable, compatible, and widely used document format. Why not create your own image-to-PDF converter or add functionality to your system for when documents need to be uploaded in PDF, such that the user is able to upload images and the app automatically converts them into PDF? Users will love this feature as it results in a much better user experience.

There are many other reasons why C# software engineers should know how to convert images into PDFs. There are often multiple image formats, so any engineer must be able to convert them all into a new PDF document.

There are multiple libraries available on the market which allow C# developers to convert images into PDFs. Some libraries are paid, some are difficult to use, and some have very limited functionality. It is very difficult to find a library that is free, developer-friendly, and provides good performance and efficiency.

After a lot of research, I found "IronPDF". This library is free for development, easy to implement, and provides better performance. Additionally, there is comprehensive documentation available for this library, enabling you to make full use of its functionalities. Their support team is also very responsive and assists you in a very professional manner.

IronPDF is a .NET library for generating, reading, editing, and saving PDF files in .NET projects. IronPDF features HTML-to-PDF for .NET 5, Core, Standard & Framework with full HTML-to-PDF support, including CSS3 and JS.

I think this suffices for an introduction. Let's take a look at how to create a sample project to learn about converting images to PDF.

Create a Visual Studio Project

To create a new project, open Microsoft Visual Studio (any version). I am using Visual Studio 2019, so the steps for creating a new project may vary from version to version. The steps for creating a new project may therefore differ, but the remainder should be the same for every version.

  1. Click on Create New Project.
  2. Select Project Template, I am selecting the Console Application template for this demonstration. You can use any as per your requirement.
  3. Click on Next. Name the Project
  4. Click Next and select the Dot Net Version.
  5. Click the Create button.

The new project will be created as shown below.

Next, we need to install the IronPDF NuGet Package in our project to use its features. The interesting thing about IronPDF is that it takes the frustration out of generating PDF documents by not relying on proprietary APIs. "Html-To-Pdf" renders pixel-perfect PDFs from open standard document types: HTML, JS, CSS, JPG, PNG, GIF, and SVG. In short, it uses the skills that developers already possess.

Install the IronPDF NuGet Package

To install the NuGet Package, go to Tools> NuGet Package Manager> Package Manager Console. The following window will appear:

Next, write the following command in the Package Manager Console:

install-package IronPDF

Press Enter.

Convert Image File to PDF Document

We will convert the following image to PDF.

Example Image

Next, let's get to our real task: converting images to PDF files.

We first need to reference the IronPDF library to our program.cs file. Write the following code snippet at the top of the file.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Next, write the following code inside the main function. This will convert a JPG file to a PDF file.

PdfDocument doc = ImageToPdfConverter.ImageToPdf(@"D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage);
doc.SaveAs(@"D:\Iron Software\ImageToPDF\bird.pdf");
PdfDocument doc = ImageToPdfConverter.ImageToPdf(@"D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage);
doc.SaveAs(@"D:\Iron Software\ImageToPDF\bird.pdf");
Dim doc As PdfDocument = ImageToPdfConverter.ImageToPdf("D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage)
doc.SaveAs("D:\Iron Software\ImageToPDF\bird.pdf")
VB   C#

In the above code example, we have used the ImageToPDFConverter Class provided by IronPDF for image conversion. The ImageToPDF Conversion class can be used to create PDF documents from images. It accepts both image files and System.Drawing.object as input.

The static method ImageToPDF() converts a single image file to an identical PDF document of matching dimensions. It takes two arguments: Image Path and Image Behavior (how the image will display on paper). Imaging.ImageBehavior.CropPage will set the paper size equal to the image size. The default page size is A4. You can set it via the following line of code:

 ImageToPdfConverter.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter;
 ImageToPdfConverter.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter;
ImageToPdfConverter.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter
VB   C#

There are multiple page size options provided, and you can set them as per your requirements.

Convert Multiple Images to a PDF File

Consider the following example, in which we will convert JPG images into a new document.

static void Main(string[] args)
{
    var ImageFiles = System.IO.Directory.EnumerateFiles(@"D:\Iron Software\ImageToPDF\").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));
    // Convert the images to a PDF and save it.
    ImageToPdfConverter.ImageToPdf(ImageFiles).SaveAs(@"D:\Iron Software\ImageToPDF\JpgToPDF.pdf");
}
static void Main(string[] args)
{
    var ImageFiles = System.IO.Directory.EnumerateFiles(@"D:\Iron Software\ImageToPDF\").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));
    // Convert the images to a PDF and save it.
    ImageToPdfConverter.ImageToPdf(ImageFiles).SaveAs(@"D:\Iron Software\ImageToPDF\JpgToPDF.pdf");
}
Shared Sub Main(ByVal args() As String)
	Dim ImageFiles = System.IO.Directory.EnumerateFiles("D:\Iron Software\ImageToPDF\").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))
	' Convert the images to a PDF and save it.
	ImageToPdfConverter.ImageToPdf(ImageFiles).SaveAs("D:\Iron Software\ImageToPDF\JpgToPDF.pdf")
End Sub
VB   C#

In the above code, firstly System.IO.Directory.EnumerateFiles will get all the files available in the given folder. After that, it will filter all the JPG images from that folder and store them in the ImageFiles variable. If you have png or any other image format, you can just add that in the where() query.

The next line will take all the images and combine them into a single PDF document.

Print PDF File

The following line of code snippet will print the document:

 doc.Print();
 doc.Print();
doc.Print()
VB   C#

The Print() method provided by the PDFDocument class will print the document using the default printer. It also provides an option to change the printer name and other settings. For more details about printing documents, please click here.

Summary

In this tutorial, we have learned a very easy way to convert images into a PDF file. We have demonstrated examples with code, and we have converted a single image into a PDF. We have also combined multiple images into a single PDF file. Moreover, we have also learned to print documents.

There are multiple useful and interesting features provided by IronPDF, and I cannot possibly describe them all here. For more details, please click here.

IronPDF is a part of the IronSoftware suite. IronSuite has additional interesting products such as IronXL, IronBarode, IronOCR, and IronWebScraper. I can assure you that you will find all these products extremely useful. You can save up to 250% by purchasing the complete IronSuite, as you can currently get all five products for the price of just two. For more details, please click here.

I hope this article was understandable and easy to follow. For any queries, please feel free to contact us.