GroupDocs.Parser provides the functionality to extract text from documents.
The Accurate mode is the default text extraction mode: the text quality is the best possible.
You can extract the whole document text or only the text of a document page.
To extract text in Accurate mode, the getText method of the Parser class is used:
parser.getText();// the whole document
parser.getText(pageIndex);// a single page (zero-based index)
The methods return an instance of the TextReader class with the extracted text, or null if text extraction isn’t supported for the document. To retrieve the total number of document pages, use the getDocumentInfo method (see below).
TextReader has the following members:
Member
Description
readLine()
Reads a line of characters from the text reader and returns the data as a string.
readToEnd()
Reads all characters from the current position to the end of the text reader and returns them as one string.
close()
Releases the resources used by the reader.
Extract text
Here are the steps to extract text from the document:
Instantiate the Parser object for the initial document;
Call the getText method and obtain the TextReader object;
Check if reader isn’t null (text extraction is supported for the document);
Read the text from reader.
The following example shows how to extract text from a document:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Create an instance of Parser class
constparser=newgroupdocs.Parser('sample.pdf');try{// Extract a text into the reader
constreader=parser.getText();// If text extraction isn't supported, a reader is null
if(reader==null){console.log("Text extraction isn't supported");}else{try{// Print a text from the document
console.log(reader.readToEnd());}finally{reader.close();}}}finally{parser.close();}process.exit(0);
Extract text from page
Here are the steps to extract text from a document page:
Instantiate the Parser object for the initial document;
Call parser.getFeatures().isText() to check if text extraction is supported for the document;
Call the getDocumentInfo method to get the number of pages;
Call the getText(pageIndex) method with the page index and obtain the TextReader object;
Read the text from reader.
The following example shows how to extract text from document pages:
constgroupdocs=require('@groupdocs/groupdocs.parser');functionrun(){// Create an instance of Parser class
constparser=newgroupdocs.Parser('sample.pdf');try{// Check if the document supports text extraction
if(!parser.getFeatures().isText()){console.log("Document doesn't support text extraction.");return;}// Get the document info
constdocumentInfo=parser.getDocumentInfo();// Check if the document has pages
if(documentInfo.getPageCount()===0){console.log('Document has no pages.');return;}// Iterate over pages
for(letp=0;p<documentInfo.getPageCount();p++){// Print a page number
console.log(`Page ${p+1}/${documentInfo.getPageCount()}`);// Extract a text into the reader
// We ignore null-checking as we have checked text extraction feature support earlier
constreader=parser.getText(p);try{// Print a text from the document page
console.log(reader.readToEnd());}finally{reader.close();}}}finally{parser.close();}}run();process.exit(0);
More resources
Free online document parser App
Along with the full-featured library we provide simple but powerful free apps.
You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our 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.