GroupDocs.Parser provides the functionality to extract images from documents by the getImages() method:
parser.getImages()// returns Iterable<PageImageArea> or null
This method returns a Java Iterable collection of PageImageArea objects:
Member
Description
getPage()
The page that contains the image.
getRectangle()
The rectangular area on the page that contains the image.
getFileType()
The format of the image.
getRotation()
The rotation angle of the image.
getImageStream()
Returns the image stream (java.io.InputStream).
getImageStream(ImageOptions)
Returns the image stream in a different format.
save(String)
Saves the image to the file.
save(String, ImageOptions)
Saves the image to the file in a different format.
The ImageOptions class is used to define the image format into which the image is converted. The following image formats (ImageFormat) are supported:
Bmp
Gif
Jpeg
Png
WebP
Here are the steps to extract images from the whole document:
Instantiate the Parser object for the initial document;
Call the getImages() method and obtain the collection of PageImageArea objects;
Check if the collection isn’t null (images extraction is supported for the document);
Iterate through the collection and get sizes, image types and image contents.
The following example shows how to extract all images from the whole document:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Create an instance of Parser class
constparser=newgroupdocs.Parser('images.pdf');try{// Extract images
constimages=parser.getImages();// Check if images extraction is supported
if(images==null){console.log("Images extraction isn't supported");}else{// Iterate over images
constit=images.iterator();while(it.hasNext()){constimage=it.next();// Print a page index, rectangle and image type
console.log(`Page: ${image.getPage().getIndex()}, R: ${image.getRectangle()}, Type: ${image.getFileType()}`);}}}finally{parser.close();}process.exit(0);