Create thumbnails from PDFs in C#
This guide explains how to create an image from a PDF document’s page. For example, you can use the created image as a thumbnail preview for the PDF document.
To create an image from a PDF document’s page, follow these steps:
- Create a
GdPicturePDF
object and aGdPictureImaging
object. - Load the source document by passing its path to the
LoadFromFile
method of theGdPicturePDF
object. - Select the page from which to create an image with the
SelectPage
method of theGdPicturePDF
object. - Render the selected page to an image with the
RenderPageToGdPictureImageEx
method of theGdPictureImaging
object. This method takes the following parameters:- The dots-per-inch (DPI) resolution of the image.
- A Boolean value that defines whether to include form fields and annotations in the image.
- Save the image to a file with the
SaveAsPNG
method of theGdPictureImaging
object. - Release unnecessary resources.
The example below creates a PNG image from a PDF document’s first page:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Load the source document.gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Select the first page.gdpicturePDF.SelectPage(1);// Render the selected page to an image.int imageId = gdpicturePDF.RenderPageToGdPictureImageEx(200, true);// Save the image to a file.gdpictureImaging.SaveAsPNG(imageId, @"C:\temp\output.png");// Release unnecessary resources.gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the source document. gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Select the first page. gdpicturePDF.SelectPage(1) ' Render the selected page to an image. Dim imageId As Integer = gdpicturePDF.RenderPageToGdPictureImageEx(200, True) ' Save the image to a file. gdpictureImaging.SaveAsPNG(imageId, "C:\temp\output.png") ' Release unnecessary resources. gdpictureImaging.ReleaseGdPictureImage(imageId)End Using
Related topics