If you need a high-speed and high-fidelity conversion of images to PDF format, this article is going to help you a lot. Because in this article, you will learn how to easily convert an image to PDF in C#. We will explicitly cover how to convert JPG to PDF, PNG to PDF, TIFF to PDF, BMP to PDF, and EMF to PDF.
- Image to PDF Converter API - Installation
- Convert JPG to PDF with C#
- Convert Image to PDF with Same Page Height and Width in C#
- Convert PNG to PDF using C#
- Convert TIFF to PDF using C#
- Convert EMF Image to PDF using C#
- Convert BMP to PDF with C#
C# Image to PDF Converter API - Installation
We will be performing these image formats to PDF conversion using Aspose.PDF for .NET API. You can either download it from Releases section, or via the NuGet Gallery with the following command:
Install-Package Aspose.Pdf -Version 20.9.0
So the API has been installed successfully by now. Let us move on to explore different use cases:
Convert JPG to PDF in C#
You can easily convert a JPG image to PDF by following steps:
- Initialize object of Document class
- Add a new Page to PDF document
- Load JPG image and add to the paragraph
- Save output PDF
The code snippet below shows how to convert JPG Image to PDF using C#:
Below is the screenshot of JPG conversion to PDF:
However, in this screenshot, you can notice that the image is converted to a PDF page, but, default page dimensions and margins are applied in the output PDF document. Aspose.PDF for .NET API allows you to control page dimensions and margins that help you create the same size PDF as the image dimensions. Let us proceed to explore this feature:
C# Image to PDF - Same Page Height and Width
Continuing this example from the above use case, you can notice a bigger page size in the above screenshot. Let us learn how to convert an image to a PDF with the same height and width as the page. We will be getting the image dimensions and accordingly set the page dimensions of PDF document with the below steps:
- Load input image file
- Get the height and width of the image
- Set height, width, and margins of a page
- Save the output PDF file
Following code snippet shows how to convert an Image to PDF with the same page height and width using C#:
So you can notice the difference in page dimensions that have been created with the updated code snippet:
Finally, you can notice the page size is exactly similar in accordance with input image dimensions, and no more default page margins are being set now.
C# PNG to PDF Conversion
PNG image format is popular because of its features as compared to JPG images. For example, PNG images support transparency. Firstly, let us convert PNG to PDF with the below steps:
- Load input PNG image
- Read height and width values
- Create new document and add Page
- Set page dimensions
- Save output file
Moreover, the code snippet below shows how to convert PNG to PDF with C# in your .NET applications:
Convert TIFF to PDF using C#
A TIFF image is different from conventional JPEG, PNG, or other formats in a way that a TIFF image can contain several frames with different images. Aspose.PDF file format is also supported, be it a single-frame or multi-frame TIFF image. So you can convert the TIFF image to PDF in your .NET applications. Therefore, we will consider an example of converting a multi-page TIFF image to a multi-page PDF document with below steps:
- Instantiate an instance of Document class
- Load input TIFF image
- Get FrameDimension of the frames
- Add new page for each frame
- Finally, save images to PDF pages
Moreover, the following code snippet shows how to convert multi-page or multi-frame TIFF image to PDF with C#:
Save EMF Image as PDF in C#
EMF images, also known as Enhanced Metafile Format, are device-independent images. Furthermore, you can convert EMF to PDF image using the below steps:
- Firstly, initialize Document class object
- Load EMF image file
- Add the loaded EMF image to a Page
- Save PDF document
Moreover, the following code snippet shows how to convert an EMF to PDF with C# in your .NET code snippet:
Export BMP to PDF in C#
BMP images are Bitmap images that you can convert to PDF with Aspose.PDF for .NET API. Therefore, you can follow the following steps to convert BMP images:
- Initialize a new Document
- Load input BMP image
- Finally, save the output PDF file
So the following code snippet follows these steps and shows how to convert BMP to PDF using C#:
Convert Image to PDF with a Free License
You can [get a free temporary license][20] and convert images to PDF without evaluation limitations.
Conclusion
After all, we have explored images to PDF conversion from different aspects in C#. Different conversions including JPG to PDF, PNG to PDF, TIFF to PDF, EMF to PDF, and BMP to PDF have been discussed in detail. Moreover, you have learned how to improve the image to PDF rendering while setting page size and margins. Furthermore, if you still have any inquiries or questions then please reach out to us at Free Support Forum.