In different cases, you may need to create a PDF based on a bunch of images, such as generating PDF from scanned document pages or invoices. For such scenarios, this article covers how to create PDF from the images programmatically using C#.
- 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
For creating the PDF files from images, we’ll use Aspose.PDF for .NET. It is a powerful PDF API that lets you create, modify, and convert PDF files from within your .NET applications. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.Pdf
Steps to Create a PDF File from Images
The following are the steps to create PDF from the images using Aspose.PDF for .NET.
- First, create a new PDF document.
- Get the list of the image files’ names in an array.
- For each image in the list, do the following:
- Add a new page to the PDF document and set its properties.
- Load each image file from the list.
- Add image to the paragraph collection of the page.
- Finally, save the PDF document.
Create PDF from Images using C#
The following are the steps along with API references to create PDF from images.
- Create an instance of Document class.
- Get the list of the image files’ names in an array using Directory.GetFiles(string) method.
- For each image file in the list, do the following:
- Add a new Page to the PDF document using Document.Pages.Add() method and set page’s properties.
- Load the image file into a FileStream object.
- Add a new Image to the page using Page.Paragraphs.Add(Image) method.
- Set image’s stream using Image.ImageStream property.
- Finally, save the PDF document using Document.Save(String) method.
The following code sample shows how to create a PDF from images.
Get a Free API License
You can try the API without evaluation limitations by getting a free temporary license.
Conclusion
In this article, you have learned how to create PDF files from images using C#. The step-by-step guide and code sample have shown how to insert each image in a folder to a page in a PDF file. You can explore more about Aspose.PDF for .NET using documentation. In case you would have any queries, feel free to let us know via our forum.