GroupDocs.Parser for Java provides the ability to extract text from image files and PDFs composed of images.
Note
GroupDocs.Parser for Java doesn’t contain a built-in OCR engine. Text recognition is performed by an OCR connector - a class derived from OcrConnectorBase that wraps any paid or free OCR solution. The Parser object must be created with ParserSettings that contain the connector instance. See Use OCR Connector for how to write one. The examples below use AsposeOcrOnPremise - the connector to Aspose.OCR for Java from the GroupDocs.Parser for Java examples.
The list of recognition languages depends on the OCR engine behind the connector. The Language value set by OcrOptions.setLanguage (Language.Eng by default) is only a hint for the connector: the connector must read it from the OcrOptions it receives and pass it to the OCR engine. See OCR Usage Basics for which options reach the connector.
Extract text from a scanned PDF
For PDF documents OCR must be requested explicitly: create TextOptions with useOcr = true. Without it, only the text layer of the PDF is extracted. If the parser was created without an OCR connector, the OCR request fails with UnsupportedOperationException.
The following example shows how to extract text from a scanned PDF:
// Create an instance of ParserSettings class with OCR Connector
ParserSettingssettings=newParserSettings(newAsposeOcrOnPremise());// Create an instance of Parser class with settings
try(Parserparser=newParser("scanned.pdf",settings)){// Create an instance of TextOptions to use OCR
TextOptionsoptions=newTextOptions(false,true);// Extract a text using OCR
try(TextReaderreader=parser.getText(options)){// Print a text or 'not supported' message
System.out.println(reader==null?"Text extraction isn't supported":reader.readToEnd());}}
Extract text from an image
For image files the text is always recognized by the OCR connector, so TextOptions can be omitted. Text extraction from images is supported only if the parser has a connector whose isTextSupported method returns true; otherwise getText returns null.
// Create an instance of ParserSettings class with OCR Connector
ParserSettingssettings=newParserSettings(newAsposeOcrOnPremise());// Create an instance of Parser class with settings
try(Parserparser=newParser("scanned.jpg",settings)){// Extract a text using OCR
try(TextReaderreader=parser.getText()){// Print a text or 'not supported' message
System.out.println(reader==null?"Text extraction isn't supported":reader.readToEnd());}}
Pass TextOptions with OcrOptions to send the recognition language and other settings to the connector:
// Create an instance of ParserSettings class with OCR Connector
ParserSettingssettings=newParserSettings(newAsposeOcrOnPremise());// Create an instance of Parser class with settings
try(Parserparser=newParser("scanned.jpg",settings)){// Set OCR options
OcrOptionsocrOptions=newOcrOptions();ocrOptions.setLanguage(Language.Chinese);// Create an instance of TextOptions to use OCR
TextOptionsoptions=newTextOptions(false,true,ocrOptions);// Extract a text using OCR
try(TextReaderreader=parser.getText(options)){// Print a text or 'not supported' message
System.out.println(reader==null?"Text extraction isn't supported":reader.readToEnd());}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured Java library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.