Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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.
ImageToPdf
method to convert image to PDFTo 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.
The new project will be created as shown below.
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.
To install the NuGet Package, go to Tools > NuGet Package Manager > Package Manager Console. The following window will appear:
Package Manager Console UI
Next, write the following command in the Package Manager Console:
Install-Package IronPdf
Press Enter.
Install the
IronPdf
package in the Package Manager Console
The next step will show how to convert the following image to PDF.
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.
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.
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.
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.