Images are an effective way to visualize data and make documents look more informative. PDF files are one of the widely used document file formats for sharing information and content. As a .NET application developer, you would like to add the functionality of converting different types of images such as PNG, JPG, BMP, and GIF to PDF file format using C# in your application.
In this article, we’ll show how to convert images to PDF using iTextPDF for .NET API. It assumes that you have a basic knowledge of working with .NET applications and working with Nuget.
About iTextPdf for .NET
iTextPdf for .NET is a free open-source API that provides the capability of working with PDF files from within your .NET applications. It has a wide range of features including creating, manipulating, and extracting content from PDF documents. Among its wide range of features, iTextPdf enables us to convert images to PDF with just a few lines of code as we will show in this article.
How to Convert Images to PDF in C#?
Before we jump into writing the code for converting images to PDF in C#, make sure that you have installed iTextPdf on your system. If you haven’t installed iTextPdf yet, you can check our guide, Open-Source API for PDF Documents, to know more about how to install the API for working with it.
ImageData imageData = ImageDataFactory.Create(ORIG); PdfDocument pdfDocument = new PdfDocument(new PdfWriter("ImageToPdf.pdf")); Document document = new Document(pdfDocument); Image image = new Image(imageData); image.SetWidth(pdfDocument.GetDefaultPageSize().GetWidth() - 50); image.SetAutoScaleHeight(true); document.Add(image); pdfDocument.Close();
Explanation of Code for Converting Image to PDF in C#
Let’s have a detailed look at how iTextPdf API converts an image to PDF using C#.
- ImageData Creation: The ImageData object is created by calling the Create method of the ImageDataFactory class. The ORIG variable presumably contains the path to the image file. This method is used to read the image data and create an ImageData object from it, which is later used to create an Image object.
- PDF Document Creation: Here, a new PDF document is created using the PdfDocument class constructor. It takes a PdfWriter object as an argument, which specifies the file where the PDF will be saved. In this case, the file is named “ImageToPdf.pdf”.
- Document Creation: A Document object is created, representing the content of the PDF document. It’s constructed using the PdfDocument object created in the previous step.
- Image Creation and Configuration: An Image object is created using the previously created ImageData object. This Image object represents the image that will be added to the PDF document. The SetWidth method is used to set the width of the image to match the width of the PDF document’s default page size, with a 50-point margin on each side. This will make the image fit the width of the page while maintaining its aspect ratio. The SetAutoScaleHeight(true) method is called to automatically scale the height of the image to maintain its aspect ratio based on the width set in the previous step.
- Adding Image to the Document: The Image object is added to the Document. This step inserts the image into the PDF document.
- Closing the PDF Document: After adding all the necessary content to the PDF document, it is closed. This ensures that all the changes made to the PDF are properly saved and the file is ready for use.
In summary, this code reads an image file, creates a PDF document, adds the image to the document, and saves the result as “ImageToPdf.pdf”. The image is automatically scaled to fit the width of the PDF page while maintaining its aspect ratio.
iTextPdf API lets you convert BMP, JPEG, PNG, GIF, and many more image file formats to PDF file format. It is simple to use, can be installed easily and conversion is performed very quickly. Here are some more examples of working with iTextPdf API.
- Create PDF File using iTextPdf in C#
- Split PDF into multi parts with iTextPdf
- Merge PDF files into a single large PDF
- PDF Page Deletion in C#
- PDF Page Rotation in C#
For more examples of working with iTextPdf in C#, stay tuned.