GroupDocs.Parser allows to extract formatted text from documents for those cases when simple plain text is not enough and you need to keep formatting like text style, table layout etc.
This feature allows to extract text with integrated HTML tags or Markdown syntax. Even PlainText mode allows to convert your documents to high quality text with integrated ASCII formatting symbols for tables, lists etc.
Extract formatted text from documents
To extract a formatted text from documents simply call the getFormattedText(options) method:
parser.getFormattedText(options);// returns TextReader or null
This method returns an instance of the TextReader class with an extracted text, or null if formatted text extraction isn’t supported for the document. FormattedTextOptions has the following constructor:
newgroupdocs.FormattedTextOptions(mode);
The FormattedTextMode enumeration has the following members:
Member
Description
FormattedTextMode.Html
HTML format.
FormattedTextMode.Markdown
Markdown format.
FormattedTextMode.PlainText
Plain text format.
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 resources used by the reader.
Here are the steps to extract a HTML formatted text from the document:
Instantiate the Parser object for the initial document;
Instantiate FormattedTextOptions with HTML text mode;
Call the getFormattedText(options) method and obtain the TextReader object;
Check if reader isn’t null (formatted text extraction is supported for the document);
Read a text from reader.
The following example shows how to extract a document text as HTML text:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Create an instance of Parser class
constparser=newgroupdocs.Parser('sample.docx');try{// Extract a formatted text into the reader
constoptions=newgroupdocs.FormattedTextOptions(groupdocs.FormattedTextMode.Html);constreader=parser.getFormattedText(options);// If formatted text extraction isn't supported, a reader is null
if(reader===null){console.log("Formatted text extraction isn't supported");}else{try{// Print a formatted text from the document
console.log(reader.readToEnd());}finally{reader.close();}}}finally{parser.close();}process.exit(0);
More resources
Advanced usage topics
To learn more about document data extraction features and get familiar how to extract text, images, forms and more, please refer to the advanced usage section.
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 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.