GroupDocs.Parser provides the functionality to extract images from a document page area by the getImages(PageAreaOptions) and getImages(int, PageAreaOptions) methods:
parser.getImages(options)// returns Iterable<PageImageArea> or null
parser.getImages(pageIndex,options)// returns Iterable<PageImageArea> or null
These methods return 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
The PageAreaOptions parameter is used to customize the images extraction process. This class has the following members:
Member
Description
getRectangle()
The rectangular area that contains images.
Here are the steps to extract images from the upper-right area of the pages:
Instantiate the Parser object for the initial document;
Instantiate PageAreaOptions with the rectangular area;
Call the getImages(PageAreaOptions) 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 only images from the specified page area:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Create an instance of Parser class
constparser=newgroupdocs.Parser('images.pdf');try{// Create the options which are used for images extraction
constoptions=newgroupdocs.PageAreaOptions(newgroupdocs.Rectangle(newgroupdocs.Point(340,150),newgroupdocs.Size(300,100)));// Extract images from the page area
constimages=parser.getImages(options);// Check if images extraction is supported
if(images==null){console.log("Page 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);