Extract Images from PDF C#
Contents
[
Hide
]
Images are held in each page’s Resources collection’s Images collection. To extract a particular page, then get the image from the Images collection using the particular index of the image.
The image’s index returns an XImage object. This object provides a Save method which can be used to save the extracted image. The following code snippet shows how to extract images from a PDF file.
The following code snippet also work with Aspose.PDF.Drawing library.
private static void ExtractImagesFromPDF()
{
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open document using 'using' block to ensure proper disposal
using (var document = new Aspose.Pdf.Document(dataDir + "ExtractImages.pdf"))
{
// Extract a particular image
var xImage = document.Pages[1].Resources.Images[1];
using (var outputImage = new FileStream(dataDir + "outputImage.jpg", FileMode.Create))
{
// Save output image
xImage.Save(outputImage, ImageFormat.Jpeg);
}
// Save updated PDF file
document.Save(dataDir + "ExtractImages_out.pdf");
}
}