
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.
- API to Create PDF from Images
- Steps to Generate a PDF File from Images
- Create PDF from Images using C#
- Get a Free API License
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:
- Initialize a new
Documentobject – this represents the PDF you will generate. - Retrieve image file names – use
Directory.GetFilesto collect all supported image files (e.g., *.jpg, *.png) into an array. - Iterate through the image list and for each file:
- Add a new
Pageto theDocumentand configure its size or margins if needed. - Open the image with a
FileStream. - Create an
Imageobject, assign the stream toImage.ImageStream, and add the image to the page’sParagraphscollection.
- Add a new
- Save the document – call
Document.Savewith 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 viaDocument.Pages.Add().FileStream– reads the image file into a stream.Image– represents the visual element; itsImageStreamproperty 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.
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.