Often you need to combine the images into a single file in C#. One of the best solutions is the conversion of images to a PDF file. In this article, you will learn how to convert images to a PDF in C# following a few easy steps. Also, the provided method will ensure the high-quality and high-speed C# image to PDF conversion. So let’s go through the step-by-step guide and see how to perform image to PDF conversion in C#.
- C# Image to PDF Conversion Library
- Convert an Image to PDF in C#
- Convert Multiple Images to a PDF
C# Library to Convert Image to PDF#
For image to PDF conversion, we’ll use Aspose.PDF for .NET. It is a powerful PDF library that lets you create and manipulate PDF files from within your .NET applications. Also, it provides high-fidelity conversion of PDF files.
You can either download the library or install it using NuGet.
PM> Install-Package Aspose.PDF
How to Convert an Image to PDF in C##
Let’s first have a look at how to convert a single image to a PDF in C#. The following are the steps to perform this operation.
- Create an instance of Document class.
- Add a new Page to the PDF document using Document.Pages.Add() method and set the page’s properties.
- Load the image file into a FileStream object.
- Add a new Image to the page using Page.Paragraphs.Add(Image) method.
- Set the image’s stream using Image.ImageStream property.
- Finally, save the PDF document using Document.Save(String) method.
The following code sample shows how to convert a PNG image to PDF in C#.
Convert Multiple Images to a PDF in C##
The following are the steps to convert multiple images to a single PDF file in C#.
- Create an instance of Document class.
- Get the list of the image files’ names in an array using Directory.GetFiles(string) method.
- For each image file in the list, do the following:
- Add a new Page to the PDF document using Document.Pages.Add() method and set page’s properties.
- Load the image file into a FileStream object.
- Add a new Image to the page using Page.Paragraphs.Add(Image) method.
- Set the image’s stream using Image.ImageStream property.
- Finally, save the PDF document using Document.Save(String) method.
The following code sample shows the conversion of multiple images to a single PDF in C#.
Free C# Image to PDF Conversion#
You can convert images to PDF without evaluation limitations by getting a free temporary license.
Explore C# Image to PDF Library#
You can explore more about the C# image to PDF converter library using documentation. In case you would have any queries, feel free to let us know via our forum.
Conclusion#
In this article, you have learned how to convert an image to PDF in C#. Also, you have seen how to convert multiple images to a single PDF programmatically. Thus, you can easily perform any kind of image to PDF conversion in your application. All you need to do is install the C# PDF library in your application.
See Also#
- Generate PDF Files using C# – .NET PDF API