Extract formatted text from document page
Leave feedback
On this page
GroupDocs.Parser provides the functionality to extract formatted text from a document page by the getFormattedText method:
parser.getFormattedText(pageIndex,options);// options is a FormattedTextOptions object
The method returns an instance of the TextReader class with the extracted text, or null if formatted text extraction isn’t supported for the document. FormattedTextOptions has the following constructor:
newgroupdocs.FormattedTextOptions(mode);// mode is a FormattedTextMode value
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.
Here are the steps to extract Markdown formatted page text from the document:
Instantiate the Parser object for the initial document;
Call parser.getFeatures().isFormattedText() to check if formatted text extraction is supported for the document;
Call the getDocumentInfo method to get the number of pages;
Instantiate FormattedTextOptions with the Markdown text mode;
Call the getFormattedText(pageIndex, options) method with the page index and obtain the TextReader object;
Read the text from reader.
The following example shows how to extract document pages as Markdown text:
constgroupdocs=require('@groupdocs/groupdocs.parser');functionrun(){// Create an instance of Parser class
constparser=newgroupdocs.Parser('sample.docx');try{// Check if the document supports formatted text extraction
if(!parser.getFeatures().isFormattedText()){console.log("Document doesn't support formatted 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 formatted text into the reader
// We ignore null-checking as we have checked formatted text extraction feature support earlier
constreader=parser.getFormattedText(p,newgroupdocs.FormattedTextOptions(groupdocs.FormattedTextMode.Markdown));try{// Print a formatted 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.