C# Convert PNG to PDF (Code Example Tutorial)

PNG image - Portable Network Graphics, is a raster graphics file format that supports lossless data compression. When sending images to friends and family via email, we oftentimes struggle with whether we should send PNG files directly to them. Due to its high quality, the PNG file is usually too large and takes a lot more time to upload or download numerous PNGs- especially when sharing. A PDF document is a useful file format for you to transfer images. Converting PNG to PDF format can make your photos display perfectly in smaller file sizes and make it easier for others to print the images. In this article, we will see how to convert PNG to PDF using IronPDF image conversion API in C#.

Topics covered in the Tutorial

In this tutorial, the following topics will be covered:

  1. Introduction to the IronPDF Library
  2. Create a C# Project
  3. Install the IronPDF Library

    • Method 1: NuGet Package Manager Solution
    • Method 2: NuGet Package Manager Console
    • Method 3: Using the DLL file
  4. Add the IronPDF Namespace
  5. Convert JPG Images to PDF Documents
  6. Summary

Requirements when using IronPDF:

  • You should have basic knowledge of C# languages.
  • You should have a basic knowledge of Windows applications.

1. Introduction to the IronPDF Library

The IronPDF .NET PDF Library solution is a dream for developers- particularly software engineers who use C#. Using this excellent IronSoftware tool, you can easily create a core PDF library for .NET. IronPDF will ensure any PDF conversion from different formats is an effortless and time-saving process.

It also enables you to construct a .NET PDF library using HTML5, Javascript, CSS, and images. You can seamlessly add amd modify headers and footers in a PDF. Furthermore, it makes it very easy to read PDF text, extract images and convert images to PDFs programmatically.

Some of the important features include:

  • Create PDF documents from HTML 4/5, CSS, JavaScript, and images.
  • Generate PDF documents from URLs
  • Load URLs with custom-network login credentials, HTTP headers, proxies, cookies, user-agents, and form variables, allowing login behind HTML login forms.
  • Encrypt and decrypt PDFs.
  • Merge existing PDF files.
  • Create and edit PDF forms.

Here, we are focused on the conversion of PNG files to PDF documents. IronPDF supports almost every image format for conversion. Supported image formats are JPG images, PNG, GIF, TIFF, SVG, and BMP.

2. Create a C# Project

In this tutorial, we will use the Visual Studio 2022 version (the latest version is recommended) and C# programming language to build our project. As the example code is derived from C# 10.0, you should have some knowledge concerning top-level statements.

Let's begin by creating a C# project.

  • Open Visual Studio 2022.
  • Either create a new C# project or open an existing one.
  • Give a name to the project.
  • Select .NET Core >= 3.1 as 3.1 is supported and works on every device. The latest and most stable version of the .NET framework is 6.0.
Console Program after Creating Project

Console Program after Creating Project

3. Install the IronPDF Library

Method 1: NuGet Package Manager Solution

Open from Solution Explorer

Open from Tools

Once the NuGet Package Manager Solution is open, browse for the IronPDF library to convert a PNG image to a PDF file. Then click on install.

Browse IronPDF

Browse IronPDF

Method 2: NuGet Package Manager Console

Using the NuGet Package Manager Console will allow us to install our library effortlessly. Administrative privilege is not required to install the library. A NuGet command will be used to install the IronPDF library in your project. Just utilize the following command to proceed.

Install-Package IronPDF

Method 3: Using DLL File

You can directly download the IronPDF .DLL file from the website. It can be instantly downloaded from the IronPDF DLL download.

To reference the library in your project follow these directions:

  • Right-click the Solution in the Solution Explorer
  • Select "References"
  • Browse for the IronPDF.dll library
  • Click OK

All done! IronPDF is downloaded, installed, and ready to use to convert PNG to PDF format.

4. Add the IronPDF Namespace

Now add the IronPDF namespace to your program. You have to add a given line of code at the top of the file.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

This will allow you to access all of the functions provided by IronPDF. This line of code must be added to every file where you wish to use the IronPDF features.

5. Convert PNG to PDF format

Converting PNG files to PDF documents is very easy with IronPDF. Only one line of code can achieve this task using the IronPDFs ImageToPDF method. Inside the directory of the project, place the PNG images to be converted inside a folder named assets. The folder must be placed in the location: bin\Debug\net6.0. Then, using System.IO.Directory, enumerate the assets folder with all the PNG files and pass it to ImageToPDF method for conversion operation stream. The following code example helps you convert a PNG image to a PDF document and save steam object to the disk.

using IronPdf;

Console.WriteLine("C# Convert PNG to PDF using IronPDF");

// One or more images as IEnumerable. This example selects all PNG images in a specific 'assets' folder.
var Image = System.IO.Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".png") || f.EndsWith(".PNG"));

// Converts the images to a PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf");

// Print success message
Console.WriteLine("PNG successfully converted to PDF");
using IronPdf;

Console.WriteLine("C# Convert PNG to PDF using IronPDF");

// One or more images as IEnumerable. This example selects all PNG images in a specific 'assets' folder.
var Image = System.IO.Directory.EnumerateFiles("assets").Where(f => f.EndsWith(".png") || f.EndsWith(".PNG"));

// Converts the images to a PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf");

// Print success message
Console.WriteLine("PNG successfully converted to PDF");
Imports IronPdf

Console.WriteLine("C# Convert PNG to PDF using IronPDF")

' One or more images as IEnumerable. This example selects all PNG images in a specific 'assets' folder.
Dim Image = System.IO.Directory.EnumerateFiles("assets").Where(Function(f) f.EndsWith(".png") OrElse f.EndsWith(".PNG"))

' Converts the images to a PDF and saves it.
ImageToPdfConverter.ImageToPdf(Image).SaveAs("composite.pdf")

' Print success message
Console.WriteLine("PNG successfully converted to PDF")
VB   C#

In the above code snippet, the asset folder contains only one PNG file. The output file looks like this:

Single JPG file to PDF file

Single JPG file to PDF file

The same code example can be used to convert multiple PNG images. The output file formats contain three PNG-to-PDF documents.

Multiple PNG Images to PDF Document

Multiple PNG Images to PDF Document

6. Summary

This tutorial shows how we can convert a PNG image to a PDF document using the IronPDF C# library. The manipulation and configuration of PDF files becomes remarkably effortless with the IronPDF library function. All that is needed is just a few lines of code to construct a PDF document from PNG files. Whether it's to convert JPG image, PNG image, or numerous other formats, IronPDF is ideal for developers and companies.

Utilize the free version to test it out! Additionally, with a free 30-day trial key, you can test the functionality of IronPDF. Furthermore, the current special offer allows you to get five products from IronPDF for the price of just two! Use this link for more information about licensing.

You can download the software product from this link.