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
How can I convert an image to a PDF in C#?
You can convert an image to a PDF in C# using IronPDF's ImageToPdf
method. This method allows you to specify the image path and desired PDF output settings.
Can multiple images be converted into a single PDF file?
Yes, IronPDF enables you to convert multiple images into a single PDF file using the ImageToPdf
method, where you provide a collection of image files.
What image formats are compatible with conversion to PDF?
IronPDF supports conversion of various image formats such as JPG, PNG, GIF, and SVG into PDF documents.
How do I set the page size when converting an image to a PDF?
To set the page size during conversion, use the PaperSize
property within the ImageToPdfConverter
class to select a standard size like Letter or A4.
Is it possible to print a PDF document created with IronPDF?
Yes, IronPDF includes a Print
method within the PdfDocument
class that allows you to print PDF documents using either default or specified printer settings.
What additional features does IronPDF offer?
IronPDF provides additional features such as generating PDFs from URLs, encrypting and decrypting PDFs, merging PDF files, and creating and editing PDF forms.
How do I install IronPDF in a Visual Studio project?
To install IronPDF in a Visual Studio project, open the Package Manager Console and run the command Install-Package IronPdf
.
What are the advantages of using IronPDF for PDF generation?
IronPDF offers a simple, efficient, and well-documented API for PDF generation without relying on proprietary APIs. It also provides professional support and handles various PDF tasks effectively.