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:

  1. Create a GdPicturePDF object and a GdPictureImaging object.

  2. Load the source document by passing its path to the LoadFromFile method of the GdPicturePDF object.

  3. Select the page from which to create an image with the SelectPage method of the GdPicturePDF object.

  4. Render the selected page to an image with the RenderPageToGdPictureImageEx method of the GdPictureImaging 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.

  5. Save the image to a file with the SaveAsPNG method of the GdPictureImaging object.

  6. 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
Used Methods

Related Topics