You may often need to generate thumbnails while embedding the PDF documents into a web application. Also, thumbnails of pages are required when creating a PDF viewer. For such scenarios, this article covers how to generate thumbnails of a PDF file programmatically in C# .NET. We will explicitly cover how to create a thumbnail of a particular page or all the pages in a PDF file.
- .NET API to Generate Thumbnails of PDF
- Generate a Thumbnail of a PDF File
- Generate Thumbnails of all Pages in PDF
C# .NET API to Generate Thumbnails of PDF
Aspose.PDF for .NET is a C# class library that lets you create and manipulate PDF documents within the .NET applications. Using the API, you can perform basic as well as advanced PDF automation operations quite easily. We will use this API to generate thumbnails of a PDF file. The API can be downloaded as DLL or installed via NuGet.
PM> Install-Package Aspose.Pdf
Generate a Thumbnail of a PDF File in C# .NET
Usually, you have to generate a single thumbnail image for a PDF document. The page for thumbnail could be the cover page or any other page of your choice. The following are the steps to generate a thumbnail of the desired page of a PDF in C#.
- Load the PDF file using the Document class.
- Get a reference of the desired page from the Document.Pages collection.
- Create a new FileStream for the image file.
- Create an instance of the Resolution class to set the resolution of the thumbnail image.
- Instantiate JpegDevice and set the height, width, resolution, and quality of the image.
- Generate thumbnail using JpegDevice.Process(Page, FileStream) method.
- Close the file stream.
The following code sample shows how to generate a thumbnail of a PDF file in C#.
Generate Thumbnails of all Pages in PDF in C#
The following are the steps to generate thumbnails for all the pages in PDF in C#.
- Load the PDF file using the Document class.
- Loop through all the pages in PDF using Document.Pages collection.
- In each iteration, perform the following operations:
- Create a new FileStream for the image file.
- Create an instance of the Resolution class to set the resolution of the thumbnail image.
- Instantiate JpegDevice and set the height, width, resolution, and quality of the image.
- Generate thumbnail using JpegDevice.Process(Page, FileStream) method.
- Close the file stream.
The following code sample shows how to generate thumbnails for all the pages in PDF.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to generate thumbnails for PDF files in C#. You have seen how to generate a thumbnail of a particular page or all the pages in a PDF file. Besides, you can explore more about the C# PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.