Text area represents a rectangular page area with a text. Text area can be simple or composite. The simple text area contains only a text and getAreas property is always an empty collection (not null). The composite text area doesn’t have its own text. Text property is calculated by its children texts which are contained in getAreas property.
Extract text areas
Here are the steps to extract text areas from the whole document:
Instantiate Parser object for the initial document;
Check if collection isn’t null (text areas extraction is supported for the document);
Iterate through the collection and get rectangles and text.
The following example shows how to extract all text areas from the whole document:
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleImagesPdf)){// Extract text areas
Iterable<PageTextArea>areas=parser.getTextAreas();// Check if text areas extraction is supported
if(areas==null){System.out.println("Page text areas extraction isn't supported");return;}// Iterate over page text areas
for(PageTextAreaa:areas){// Print a page index, rectangle and text area value:
System.out.println(String.format("Page: %d, R: %s, Text: %s",a.getPage().getIndex(),a.getRectangle(),a.getText()));}}
Extract text areas from a document page
Here are the steps to extract text areas from the document page:
Instantiate Parser object for the initial document;
Call isTextAreas property to check if text areas extraction is supported for the document;
Check if collection isn’t null (text areas extraction is supported for the document);
Iterate through the collection and get rectangles and text.
The following example shows how to extract text areas from a document page:
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleImagesPdf)){// Check if the document supports text areas extraction
if(!parser.getFeatures().isTextAreas()){System.out.println("Document isn't supports text areas extraction.");return;}// Get the document info
IDocumentInfodocumentInfo=parser.getDocumentInfo();// Check if the document has pages
if(documentInfo.getPageCount()==0){System.out.println("Document hasn't pages.");return;}// Iterate over pages
for(intpageIndex=0;pageIndex<documentInfo.getPageCount();pageIndex++){// Print a page number
System.out.println(String.format("Page %d/%d",pageIndex+1,documentInfo.getPageCount()));// Iterate over page text areas
// We ignore null-checking as we have checked text areas extraction feature support earlier
for(PageTextAreaa:parser.getTextAreas(pageIndex)){// Print a rectangle and text area value:
System.out.println(String.format("R: %s, Text: %s",a.getRectangle(),a.getText()));}}}
Extract text areas with options
PageTextAreaOptions parameter is used to customize text areas extraction process. This class has the following members:
Check if collection isn’t null (text areas extraction is supported for the document);
Iterate through the collection and get rectangles and text.
The following example shows how to extract only text areas with digits from the upper-left corner:
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleImagesPdf)){// Create the options which are used for text area extraction
PageTextAreaOptionsoptions=newPageTextAreaOptions("\\s[a-z]{2}\\s",newRectangle(newPoint(0,0),newSize(300,100)));// Extract text areas which contain only digits from the upper-left corner of a page:
Iterable<PageTextArea>areas=parser.getTextAreas(options);// Check if text areas extraction is supported
if(areas==null){System.out.println("Page text areas extraction isn't supported");return;}// Iterate over page text areas
for(PageTextAreaa:areas){// Print a page index, rectangle and text area value:
System.out.println(String.format("Page: %d, R: %s, Text: %s",a.getPage().getIndex(),a.getRectangle(),a.getText()));}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: