Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/bbe3c8e7913bf18be1676123ce194bdc to your computer and use it in GitHub Desktop.
Save conholdate-gists/bbe3c8e7913bf18be1676123ce194bdc to your computer and use it in GitHub Desktop.
Convert PDF Pages to Images using C#
// Open document
Document pdfDocument = new Document("C:\\Files\\sample.pdf");
// Loop through pages
foreach (var page in pdfDocument.Pages)
{
int imageCounter = 1;
// Loop through all images
foreach (XImage image in page.Resources.Images)
{
// Create file stream for image
FileStream outputImage = new FileStream(String.Format("C:\\Files\\Page{0}_Image{1}.jpg", page.Number, imageCounter), FileMode.Create);
// Save output image
image.Save(outputImage);
// Close stream
outputImage.Close();
imageCounter++;
}
}
// Open document
Document pdfDocument = new Document("C:\\Files\\sample.pdf");
foreach (var page in pdfDocument.Pages)
{
// Define Resolution
Resolution resolution = new Resolution(300);
// Create PNG device with specified attributes
// Width, Height, Resolution
BmpDevice BmpDevice = new BmpDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
BmpDevice.Process(pdfDocument.Pages[page.Number], "C:\\Files\\image" + page.Number + "_out" + ".bmp");
}
// Open document
Document pdfDocument = new Document("C:\\Files\\sample.pdf");
foreach (var page in pdfDocument.Pages)
{
// Define Resolution
Resolution resolution = new Resolution(300);
// Create Jpeg device with specified attributes
// Width, Height, Resolution
JpegDevice JpegDevice = new JpegDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
JpegDevice.Process(pdfDocument.Pages[page.Number], "C:\\Files\\image" + page.Number + "_out" + ".Jpg");
}
// Open document
Document pdfDocument = new Document("C:\\Files\\sample.pdf");
foreach (var page in pdfDocument.Pages)
{
// Define Resolution
Resolution resolution = new Resolution(300);
// Create Png device with specified attributes
// Width, Height, Resolution
PngDevice PngDevice = new PngDevice(500, 700, resolution);
// Convert a particular page and save the image to stream
PngDevice.Process(pdfDocument.Pages[page.Number], "C:\\Files\\image" + page.Number + "_out" + ".Png");
}
// Open document
Document pdfDocument = new Document("C:\\Files\\sample.pdf");
// Define Resolution
Resolution resolution = new Resolution(300);
// Create TiffSettings object
TiffSettings tiffSettings = new TiffSettings
{
Compression = CompressionType.None,
Depth = ColorDepth.Default,
Shape = ShapeType.Portrait,
SkipBlankPages = false
};
// Create TIFF device
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
// Convert a particular page and save the image to stream
tiffDevice.Process(pdfDocument, "C:\\Files\\AllPagesToTIFF_out.tif");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment