How can we (programmatically, in Java) get the size of each image file embedded in a PDF file without actually extracting the whole image to memory/disk? We want to check in case it's too large to handle.
Many thanks,
Joe
How can we (programmatically, in Java) get the size of each image file embedded in a PDF file without actually extracting the whole image to memory/disk? We want to check in case it's too large to handle.
Many thanks,
Joe
Hi Joe,
// Load the source PDF document<o:p></o:p>
com.aspose.pdf.Document doc = new com.aspose.pdf.Document("c:\\input.pdf");
com.aspose.pdf.ImagePlacementAbsorber abs = new com.aspose.pdf.ImagePlacementAbsorber();
// Load the contents of first page
doc.getPages().get_Item(1).accept(abs);
for(int counter=1; counter <= abs.getImagePlacements().size(); counter++)
{
com.aspose.pdf.ImagePlacement imagePlacement = abs.getImagePlacements().get_Item(counter);
System.out.println("Image # = " +counter );
// Get image properties
System.out.println("image width:" + imagePlacement.getRectangle().getWidth());
System.out.println("image height:" + imagePlacement.getRectangle().getHeight());
System.out.println("image LLX:" + imagePlacement.getRectangle().getLLX());
System.out.println("image LLY:" + imagePlacement.getRectangle().getLLY());
System.out.println("image horizontal resolution:" + imagePlacement.getResolution().getX());
System.out.println("image vertical resolution:" + imagePlacement.getResolution().getY());
System.out.println("==================================================");
}
Thanks for the help! Yet, I'm not sure how you can use these numbers to calculate the image size?
I called PdfExtractor.getNextImage to get the sizes of the first e.g. 3 images (using the default format .jpg) as below:
6,541 bytes
31,239 bytes
49,386 bytes
And I used your code to get the corresponding image metadata:
analysing page1 image1
image width:612.0
image height:131.0399932861328
image LLX:0.0
image LLY:660.9599609375
image horizontal resolution:150
image vertical resolution:150
analysing page1 image2
image width:612.0
image height:131.0399932861328
image LLX:0.0
image LLY:529.9199829101562
image horizontal resolution:150
image vertical resolution:150
analysing page1 image3
image width:612.0
image height:131.0399932861328
image LLX:0.0
image LLY:398.8800048828125
image horizontal resolution:150
image vertical resolution:150
The numbers don't appear to add up. How could I use these numbers to calculate the image size?
Thanks,
Joe
Hi Joe,
Hi Nayyer,
Hi Joe,
Thanks very much for the confirmation! Do you know roughly when this feature would be available? Or is there a workaround for now to work out the image file size based upon the dimensions?
Hi Joe,