USING IRONPDF

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

Regan Pun
Regan Pun
September 5, 2022
Updated February 11, 2024
Share:

Numerous libraries allow C# developers to convert images to PDFs. Finding a free, user-friendly library with good performance can be challenging, as some are paid, complex, or limited in functionality. Among these libraries, IronPDF stands out as a free, efficient, and easy-to-implement C# library. It comes with comprehensive documentation and a professional, responsive support team.

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.

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. It is recommended to use the latest version of Visual Studio. The steps for creating a new project may differ between versions, but the remainder should be the same for every version.

  1. Click on Create New Project.
  2. Select Project Template, then select 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 .NET Framework version.
  5. Click the Create button.

The new project will be created as shown below.

How to Convert Image to PDF in C# [Code Example Tutorial], Figure 1: Create a new Console Application in Visual Studio Create a new Console Application in Visual Studio

Next, install the IronPDF NuGet Package in this 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 rendering example 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:

How to Convert Image to PDF in C# [Code Example Tutorial], Figure 2: Package Manager Console UI Package Manager Console UI

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

Install-Package IronPdf

Press Enter.

How to Convert Image to PDF in C# [Code Example Tutorial], Figure 3: Install the IronPdf package in the Package Manager Console Install the IronPdf package in the Package Manager Console

Convert Image File to PDF Document

The next step will show how to convert the following image to PDF.

Example Image

How to Convert Image to PDF in C# [Code Example Tutorial], Figure 4: The sample image The sample image

To use the library, reference the IronPDF library to the program.cs file. Write the following code snippet at the top of the file.

using IronPdf;
using IronPdf;

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");

In the above code example, the ImageToPdfConverter class provided by IronPDF is used for image conversion. The ImageToPdf method can be used to create PDF documents from images. It accepts both image files and a 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;

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

Convert Multiple Images to a PDF File

The following example 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.
    PdfDocument doc = ImageToPdfConverter.ImageToPdf(imageFiles);
    doc.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.
    PdfDocument doc = ImageToPdfConverter.ImageToPdf(imageFiles);
    doc.SaveAs(@"D:\Iron Software\ImageToPDF\JpgToPDF.pdf");
}

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();

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 visit this PDF printing example.

Summary

This tutorial showed a very easy way to convert images into a PDF file with code examples, either convert a single image into a PDF or combine multiple images into a single PDF file. Moreover, it also explained how to print documents with a single line of code.

Additionally, some of the important features of IronPDF include:

There are multiple useful and interesting features provided by IronPDF, please visit this IronPDF homepage for more details.

IronPDF is a part of the Iron Software suite. The Iron Suite includes additional interesting products such as IronXL, IronBarcode, IronOCR, and IronWebscraper, and all of these products are extremely useful. You can save up to 250% by purchasing the complete Iron Suite, as you can currently get all five products for the price of just two. Please visit the licensing details page for more details.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
Creating a PDF file in .NET MAUI Using IronPDF
NEXT >
C# Text to PDF (Code Example Tutorial)