The following code sample shows how to handle loading of HTML external resources.
// Create an instance of ParserSettings to pass External Resource Handler
ParserSettingssettings=newParserSettings(newHandler());// Create an instance of Parser class to generate spreadsheet page previews
try(Parserparser=newParser(Constants.SampleHtmlWithImages,settings)){// Extract images from HTML document
Iterable<PageImageArea>images=parser.getImages();// Iterate over extracted images
for(PageImageAreai:images){// Print the type of image
System.out.println(i.getFileType());}}/**
* This class provides the ability to filter extracted images.
**/classHandlerextendsExternalResourceHandler{// Called before any external resource loads. It allows to skip unnecessary file loading.
@OverridepublicvoidonLoading(ExternalResourceLoadingArgsargs){// Check if the file name ends with installation.png
if(!args.getUri().endsWith("installation.png")){// Otherwise skip this file
args.setSkipped(true);}super.onLoading(args);}}
Gets or sets the content of the external resource. Set the bytes to provide the resource content yourself instead of loading it.
Warning
Loading of external resources is enabled by default, which means that parsing a document from an untrusted source may cause outbound HTTP requests to hosts named inside that document — for example remote images or stylesheets referenced by an HTML or MHTML file or by the HTML body of an email.
ExternalResourceHandler is applied to HTML, MHTML and XHTML documents only, when the document is laid out: image extraction, page areas and the page count. Other extraction methods (for example, getFormattedText) and other formats, including email messages, do not route through it. To guarantee that no outbound request is made, block egress at the network or process level. See Network access and data privacy for details.
External resources in email messages
The HTML body of an email message can refer to external resources, for example images stored on a remote server. Whether they are downloaded depends on the method that is called:
Email messages don’t support ExternalResourceHandler yet: onLoading isn’t called and the external images are still downloaded. Hyperlinks from a message body are never followed: the parser downloads external resources like images and style sheets only.
The following example shows the calls that cause the external images of an email message to be downloaded:
// Extract a plain text; the external image isn't required to build it
try(Parserparser=newParser(filePath)){try(TextReaderreader=parser.getText()){reader.readToEnd();}}// Extract a formatted text; the message body is laid out as an HTML document,
// therefore the external image is downloaded
try(Parserparser=newParser(filePath)){try(TextReaderreader=parser.getFormattedText(newFormattedTextOptions(FormattedTextMode.Html))){reader.readToEnd();}}// Extract a structure; the external image is downloaded as well
try(Parserparser=newParser(filePath)){parser.getStructure();}
The complete example, which also prints the requested addresses, is available as ExternalResourcesInEmailMessages in the GitHub examples.
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: