If you need a fast, high‑fidelity conversion of images to PDF, this article shows you how to convert images to PDF in C#. We cover JPG, PNG, TIFF, BMP, and EMF conversions.
- 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 perform the image‑to‑PDF conversion using the Aspose.PDF for .NET API. Download it from the Releases page or install it via NuGet:
Install-Package Aspose.Pdf -Version 20.9.0
The API is now installed. Let’s explore the different use cases.
Convert JPG to PDF in C#
Follow these steps to convert a JPG image to PDF:
- Initialize a
Documentobject. - Add a new page to the PDF.
- Load the JPG image and add it to a paragraph.
- Save the output PDF.
The snippet below demonstrates JPG‑to‑PDF conversion in C#:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “Image-to-PDF.cs” >}}
Screenshot of the JPG conversion:
{{< figure align=center src=“images/Image-To-PDF.png” alt=“Image to PDF”>}}
By default, the PDF uses standard page dimensions and margins. The Aspose.PDF for .NET API lets you set custom page size and margins to match the image dimensions.
C# Image to PDF - Same Page Height and Width
Continuing from the previous example, you can create a PDF page that matches the exact height and width of the source image. Steps:
- Load the input image file.
- Retrieve the image’s height and width.
- Set the PDF page’s height, width, and margins accordingly.
- Save the PDF.
The code below shows how to achieve this:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “JPG-to-PDF.cs” >}}
Resulting PDF with matching dimensions:
{{< figure align=center src=“images/JPG-to-PDF-1024x648.png” alt="">}}
The page size now mirrors the original image, and default margins are removed.
C# PNG to PDF Conversion
PNG images support transparency, making them useful for many scenarios. Convert PNG to PDF with these steps:
- Load the PNG image.
- Read its height and width.
- Create a new document and add a page.
- Set the page dimensions.
- Save the PDF.
Code example:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “PNG-to-PDF.cs” >}}
Convert TIFF to PDF using C#
A TIFF file can contain multiple frames. Aspose.PDF handles both single‑frame and multi‑frame TIFF images. To convert a multi‑page TIFF to a multi‑page PDF:
- Create a
Documentinstance. - Load the TIFF image.
- Get the
FrameDimensionof each frame. - Add a new PDF page for each frame.
- Save the document.
Code example:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “TIFF-to-PDF.cs” >}}
Save EMF Image as PDF in C#
EMF (Enhanced Metafile Format) images are device‑independent. Convert EMF to PDF with these steps:
- Initialize a
Documentobject. - Load the EMF image.
- Add the image to a PDF page.
- Save the PDF.
Code example:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “EMF-to-PDF.cs” >}}
Export BMP to PDF in C#
BMP bitmap images can also be converted to PDF. Follow these steps:
- Create a new
Document. - Load the BMP image.
- Save the PDF.
Code example:
{{< gist aspose-cloud 7b6fd21096e2fbbda3548cdd8c565b96 “BMPtoPDF.cs” >}}
Convert Image to PDF with a Free License
You can get a free temporary license and convert images to PDF without evaluation limitations.
Conclusion
We have covered image‑to‑PDF conversion in C# for JPG, PNG, TIFF, EMF, and BMP formats. You now know how to control page size and margins for optimal rendering. For further assistance, visit our Free Support Forum.