An Image can be converted to PDF to assure that the image is displayed correctly across devices without being altered. PDF images are ideal for printing and for storing images online when intended to be downloaded. PDF can hold as many images in one document so can be printed easily or saved as a catalog. This article will guide you to programmatically convert images like JPG, GIF, WebP, PNG to PDF in C# using .NET API for document and image conversion.
The following topics are covered briefly below:
- Image Conversion .NET API
- Convert JPG images to PDF
- Convert PNG, GIF, BMP images to PDF
- Image to PDF conversion with advance options
.NET API for Image Conversion
I will use GroupDocs.Conversion for .NET library to convert images to PDF format. The library lets us convert a long list of image formats to PDF. Some of the supported ones are mentioned here. For the complete list, visit the documentation.
- AI
- BMP
- CDR
- DJVU
- GIF
- ICO
- JPEG, JPG, JP2
- PNG
- SVGZ
- TGA
- TIF, TIFF
- WEBP
Along with the images, the API allows the developers to convert Word documents, spreadsheets, presentations, eBooks, Visio documents, Microsoft Project files, PSD files, PDL, Email messages, and much more. Many examples are available at GitHub for the mentioned support.
You can download the DLLs or MSI installer from the downloads section or get it from NuGet.
Install-Package GroupDocs.Conversion
Convert JPG to PDF in C#
To simply convert your JPG images to PDF format, you can follow the below steps:
- Load the JPG file using Converter class.
- Instantiate PdfConvertOptions class.
- Call the Convert method to convert the JPG image into PDF and get it saved on the provided path.
The following source code shows how to convert a JPG image to PDF in C#.
Convert PNG Images to PDF in C#
If you want to convert a PNG image there will be no difference in the code. The following steps allow us to convert a PNG image to PDF using C#.
- Load the PNG image file using Converter class.
- Instantiate PdfConvertOptions class.
- Call the Convert method to convert the provided image into PDF and get it saved on the provided path.
The following code shows how to convert a PNG image to PDF using C#.
Convert any Image to PDF
Similarly, you just have to provide your JPG, PNG, GIF, WebP, or any other image to the Converter class while loading. Also, there are many conversion options while converting to PDF format.
Convert Images to PDF in C# with Advanced Options
GroupDocs.Conversion provides PdfConvertOptions to give us control over conversion results when converting Image to PDF. Some of the additional options are:
- Width – Image width after conversion.
- Height – Image height after conversion.
- MarginTop – Page top margin after conversion.
- MarginBottom – Page bottom margin after conversion.
- MarginLeft – Page left margin after conversion.
- MarginRight – Page right margin after conversion.
- Rotate – Page rotation. Available options are: None, On90, On180, On270
The following C# code sample uses these additional options and converts an image to PDF. It sets the height and width of the resultant image, sets the page margins, and also rotates the image at 180 degrees.
Get a Free API License
You can use the API without evaluation limitations by requesting a free temporary license.
Conclusion
To conclude, we learned to convert images to PDF format using image conversion API for .NET. Specifically, we discussed how to programmatically convert JPG, PNG, WebP, and other images to PDF in C#. You can explore more about the image conversion API using the documentation. For queries, reach us via the forum.