How to Convert Image to PDF in C# [Code Example Tutorial]
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.
How to Convert Image to PDF in C#
- Install image to PDF C# library
- Utilize
ImageToPdf
method to convert image to PDF - Specify optional image's behavior on page
- Input optional PDF page's settings
- Save the PDF to desired location
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.
- Click on Create New Project.
- Select Project Template, then select the Console Application template for this demonstration. You can use any as per your requirement.
- Click on Next. Name the Project.
- Click Next and select the .NET Framework version.
- Click the Create button.
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.
Install the IronPDF NuGet Package
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
Convert Image File to PDF Document
The next step will show how to convert the following image to PDF.
Example Image
The sample image
To use the library, reference the IronPDF library in the program.cs
file. Write the following code snippet at the top of the file.
using IronPdf;
using IronPdf;
Imports IronPdf
Next, write the following code inside the main function. This will convert a JPG file to a PDF file.
public static void Main(string[] args)
{
// Convert a single image to the PDF document
PdfDocument doc = ImageToPdfConverter.ImageToPdf(@"D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage);
// Save the resulting PDF to the specified path
doc.SaveAs(@"D:\Iron Software\ImageToPDF\bird.pdf");
}
public static void Main(string[] args)
{
// Convert a single image to the PDF document
PdfDocument doc = ImageToPdfConverter.ImageToPdf(@"D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage);
// Save the resulting PDF to the specified path
doc.SaveAs(@"D:\Iron Software\ImageToPDF\bird.pdf");
}
Public Shared Sub Main(ByVal args() As String)
' Convert a single image to the PDF document
Dim doc As PdfDocument = ImageToPdfConverter.ImageToPdf("D:\Iron Software\ImageToPDF\bird.jpg", IronPdf.Imaging.ImageBehavior.CropPage)
' Save the resulting PDF to the specified path
doc.SaveAs("D:\Iron Software\ImageToPDF\bird.pdf")
End Sub
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;
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.
public static void Main(string[] args)
{
// Enumerate and filter JPG files from the specified directory
var imageFiles = System.IO.Directory.EnumerateFiles(@"D:\Iron Software\ImageToPDF\")
.Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));
// Convert the images to a PDF document and save it
PdfDocument doc = ImageToPdfConverter.ImageToPdf(imageFiles);
doc.SaveAs(@"D:\Iron Software\ImageToPDF\JpgToPDF.pdf");
}
public static void Main(string[] args)
{
// Enumerate and filter JPG files from the specified directory
var imageFiles = System.IO.Directory.EnumerateFiles(@"D:\Iron Software\ImageToPDF\")
.Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));
// Convert the images to a PDF document and save it
PdfDocument doc = ImageToPdfConverter.ImageToPdf(imageFiles);
doc.SaveAs(@"D:\Iron Software\ImageToPDF\JpgToPDF.pdf");
}
Public Shared Sub Main(ByVal args() As String)
' Enumerate and filter JPG files from the specified directory
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 document and save it
Dim doc As PdfDocument = ImageToPdfConverter.ImageToPdf(imageFiles)
doc.SaveAs("D:\Iron Software\ImageToPDF\JpgToPDF.pdf")
End Sub
In the above code, System.IO.Directory.EnumerateFiles
retrieves all files available in the specified folder. The Where
clause filters all the JPG images from that folder and stores them in the imageFiles
collection. If you have PNG or any other image format, you can just add that to the Where
query.
The next line will take all the images and combine them into a single PDF document.
Print PDF File
The following code snippet will print the document:
doc.Print();
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 converting a single image into a PDF or combining 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:
- Generate PDF documents from URLs.
- Encrypting and decrypting PDFs.
- Merging existing PDF files.
- Creating and editing PDF forms.
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.
Frequently Asked Questions
What is the purpose of the library mentioned in this document?
IronPDF is a .NET library for generating, reading, editing, and saving PDF files in .NET projects. It supports HTML-to-PDF conversion and various document types like JPG, PNG, GIF, and SVG.
How do I install the library in a Visual Studio project?
To install IronPDF in a Visual Studio project, go to Tools > NuGet Package Manager > Package Manager Console and enter the command: Install-Package IronPdf.
How can I convert a single image to a PDF using the library?
You can convert a single image to PDF using the ImageToPdf method from the ImageToPdfConverter class. Provide the image path and specify the image behavior, then save the PDF to the desired location.
Can the library convert multiple images into a single PDF file?
Yes, IronPDF can convert multiple images into a single PDF file by using the ImageToPdf method with a collection of image files.
What image formats are supported by the library?
IronPDF supports various image formats including JPG, PNG, GIF, and SVG for conversion to PDF.
How do I specify the paper size when converting images to PDF?
You can specify the paper size by setting the PaperSize property of the ImageToPdfConverter class to a desired PdfPaperSize, such as Letter or A4.
Is it possible to print a PDF document using the library?
Yes, IronPDF provides a Print method in the PdfDocument class, allowing you to print the document using the default printer or by specifying printer settings.
What other functionalities does the library offer?
IronPDF offers functionalities such as generating PDFs from URLs, encrypting and decrypting PDFs, merging PDF files, and creating and editing PDF forms.
What are the benefits of using this library?
IronPDF is user-friendly, efficient, and comes with comprehensive documentation and professional support. It simplifies PDF generation by not relying on proprietary APIs.
What is included in the software suite mentioned?
The Iron Software suite includes products such as IronXL, IronBarcode, IronOCR, IronWebscraper, and IronPDF. Purchasing the entire suite offers significant cost savings.