Create PDF from Images in C#

In many scenarios you must generate a PDF from a set of images, such as scanned documents, receipts, or invoice pages. This article explains how to create a PDF from images programmatically using C# and the powerful Aspose.PDF for .NET library.

C# API to Create PDF from Images

To convert images to PDF in a .NET environment we use Aspose.PDF for .NET. This API supports .NET Framework, .NET Core, and .NET 5/6+, allowing you to create, edit, and convert PDF documents directly from code. You can obtain the library by downloading it from the official site or by adding the NuGet package to your project.

PM> Install-Package Aspose.Pdf

The package provides classes such as Document, Page, and Image that simplify the process of adding each image to a separate PDF page, handling different image formats, and customizing page size or orientation.

Steps to Create a PDF File from Images

Follow these concise steps to build a PDF from a collection of image files:

  1. Initialize a new Document object – this represents the PDF you will generate.
  2. Retrieve image file names – use Directory.GetFiles to collect all supported image files (e.g., *.jpg, *.png) into an array.
  3. Iterate through the image list and for each file:
    • Add a new Page to the Document and configure its size or margins if needed.
    • Open the image with a FileStream.
    • Create an Image object, assign the stream to Image.ImageStream, and add the image to the page’s Paragraphs collection.
  4. Save the document – call Document.Save with the desired output path and format.

These steps ensure each image appears on its own page, preserving original dimensions and quality.

Create PDF from Images using C#

Below is a complete C# example that implements the workflow described above. The code demonstrates how to load images from a folder, add them to a PDF, and save the final document.

Key points in the sample:

  • Document – the main PDF container.
  • Directory.GetFiles(string) – fetches all image file paths.
  • Page – a new page is added for each image via Document.Pages.Add().
  • FileStream – reads the image file into a stream.
  • Image – represents the visual element; its ImageStream property receives the file stream.
  • Page.Paragraphs.Add(Image) – inserts the image into the page content.
  • Document.Save(string) – writes the assembled PDF to disk.

Get a Free API License

You can evaluate Aspose.PDF without restrictions by obtaining a free temporary license. This removes evaluation watermarks and enables full API functionality during development.

Get a free temporary license

Conclusion

This guide walked you through converting a set of images into a single PDF using C# and Aspose.PDF for .NET. By following the step‑by‑step instructions and sample code, you can automate PDF creation for scanned documents, invoices, or any image collection. Explore additional features such as custom page sizes, watermarks, and encryption in the Aspose.PDF documentation. If you have questions, share them on our forum.

See Also