Free Support Forum - aspose.com

Facing issue in Extract images from PDF

Hi,

We tried to extract image from PDF. If some Images integrated multiple images(Input.pdf) in this case we expected whole image as a single image but we are getting individual images.

PFA Images

How can we solve this problem?Input.pdf (1.3 MB)
output.jpg (6.3 KB)

@prasanth1249

Thanks for contacting support.

Please note that you cannot extract multiple images as an unified image because they are stored separately in resources collection of PDF document. However, as a workaround, you can capture particular region of PDF Page by specifying rectangle of largest/parent image and save it as an image. Please check following code snippet where a workaround is applied for your particular document. An output image is also attached for your reference.

// Open document
Document document = new Document(dataDir + "InputMultipleImages.pdf");
ImagePlacementAbsorber imagePlacementAbsorber = new ImagePlacementAbsorber();
document.Pages[1].Accept(imagePlacementAbsorber);
Rectangle imageRectangle = imagePlacementAbsorber.ImagePlacements[1].Rectangle;
// Get rectangle of particular page region
Aspose.Pdf.Rectangle pageRect = new Aspose.Pdf.Rectangle(imageRectangle.LLX, imageRectangle.LLY, imageRectangle.URX + 10, imageRectangle.URY + 10);
// Set CropBox value as per rectangle of desired page region
document.Pages[1].CropBox = pageRect;
// Save cropped document into stream
MemoryStream ms = new MemoryStream();
document.Save(ms);
// Open cropped PDF document and convert to image
document = new Document(ms);
// Create Resolution object
Resolution resolution = new Resolution(300);
// Create PNG device with specified attributes
PngDevice pngDevice = new PngDevice(resolution);
dataDir = dataDir + "ConvertPageRegionToDOM_out.png";
// Convert a particular page and save the image to stream
pngDevice.Process(document.Pages[1], dataDir);
ms.Close();

ConvertPageRegionToDOM_out.jpg (101.2 KB)

In case of further assistance, please feel free to let us know.