Convert PNG to PDF in C# | PNG Image to PDF Converter

PNG is a popular image format known for its lossless compression and transparency support. PDF is a versatile document format widely used for sharing and printing. Converting PNG to PDF can be useful for creating documents, reports, or presentations. In this blog post, we will explore how to convert PNG to PDF using C#.

This article covers the following topics:

PNG to PDF Converter API

Aspose.PDF for .NET is a powerful library that allows developers to create, manipulate, and convert PDF files in .NET applications. It simplifies the task of converting PNG images to PDF by providing a robust API that handles the conversion with ease. With Aspose.PDF for .NET, you can quickly integrate PNG to PDF conversion into your C# applications without dealing with complex code.

Instructions for Setting Up and Installing Aspose.PDF for .NET:

  1. Download the Aspose.PDF for .NET package.
  2. Install the package using NuGet Package Manager in Visual Studio.
PM> Install-Package Aspose.PDF
  1. Add the necessary using directives in your C# project:
using Aspose.Pdf;   
using Aspose.Pdf.Drawing;   

Steps to Convert PNG to PDF

  1. Create a new PDF document.
  2. Add a page to the document.
  3. Add an image to the page.
  4. Save the PDF document.

Convert a Single PNG Image to a PDF in C#

To convert a single PNG image to a PDF, follow these steps:

  1. Initialize a new Document class object.
  2. Add a Page to the document.
  3. Create an Image object and set its file path.
  4. Add the image to the page’s Paragraphs collection.
  5. Save the document.

The following code sample shows how to convert a PNG image to a PDF document in C#.

// Create a new document
Document doc = new Document();
// Path of the image file
string imageFile = @"Sample_PNG.png";
// Add a page to pages collection of document
var page = doc.Pages.Add();
// Load image into stream
FileStream imageStream = new FileStream(imageFile, FileMode.Open);
// Create an image object
Image image1 = new Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = imageStream;
// Save resultant PDF file
doc.Save("png-to-pdf.pdf");

Convert Multiple PNG Images to a PDF in C#

To convert multiple PNG images to a single PDF, follow these steps:

  1. Initialize a new Document class object.
  2. Loop through the list of images in a directory.
  3. For each image, add a new Page to the document.
  4. Create an Image object and set its file path.
  5. Add the image to the page’s Paragraphs collection.
  6. Save the document.

The following code sample shows how to convert multiple PNG images into a PDF document using C#.

// Create a new document
Document doc = new Document();
// Read all JPG files
var fileList = Directory.GetFiles("D:\\Files\\images\\", "*.png")
.OrderBy(f => f)
.Select(f => File.OpenRead(f))
.Cast<Stream>()
.ToList();
foreach (var file in fileList)
{
// Add a page to the document
Page page = doc.Pages.Add();
// Create an Image object
Image image = new Image();
image.ImageStream = file;
// Add the image to the page's Paragraphs collection
page.Paragraphs.Add(image);
}
// Save resultant PDF file
doc.Save("D:\\Files\\multiple-images-to-pdf.pdf");

Get a Free License

Get a free temporary license and unlock the full potential of Aspose.PDF for .NET with no limitations.

Online PNG to PDF Converter

In addition, you can also convert your PNG image to a PDF document online using our free online converter web app.

PNG to PDF - Free Resources

Besides converting PNG images into a PDF document, learn how to create, manipulate, and convert PDF documents, along with exploring various other features of the library, using the resources below:

Conclusion

Converting PNG to PDF in C# is simple with Aspose.PDF for .NET library. It provides a user-friendly API to handle the conversion efficiently. Whether you need to convert a single image or multiple images, Aspose.PDF for .NET has you covered. Try it out in your next project and streamline your image-to-PDF conversion tasks.

If you have any questions or need further assistance, please feel free to reach out at our free support forum.

See Also