Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 12, 2024 13:07
Show Gist options
  • Save aspose-com-gists/3c70ee281bd9461139425323267f864b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/3c70ee281bd9461139425323267f864b to your computer and use it in GitHub Desktop.
Generate Thumbnails for PDF Files in C#
// Open document
Document pdfDocument = new Document("file.pdf");
// Loop through pages
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
// Create stream for each image
using (FileStream imageStream = new FileStream("Thumbanils_" + pageCount + ".jpg", FileMode.Create))
{
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create an instance of JpegDevice and set height, width, resolution, and quality of image
JpegDevice jpegDevice = new JpegDevice(45, 59, resolution, 100);
// Convert a particular page and save the image to stream
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
}
// Open document
Document pdfDocument = new Document("file.pdf");
int pageIndex = 0;
// Get page of desired index from collection
var page = pdfDocument.Pages[pageIndex];
// Create stream for image file
using (FileStream imageStream = new FileStream("Thumbanils_" + page.Number + ".jpg", FileMode.Create))
{
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create an instance of JpegDevice and set height, width, resolution, and quality of image
JpegDevice jpegDevice = new JpegDevice(45, 59, resolution, 100);
// Convert a particular page and save the image to stream
jpegDevice.Process(page, imageStream);
// Close stream
imageStream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment