Extract formatted text from document
GroupDocs.Parser provides the functionality to extract a formatted text from documents by the getFormattedText(FormattedTextOptions) method:
TextReader getFormattedText(FormattedTextOptions options);
The method returns an instance of TextReader class with an extracted text. FormattedTextOptions has the following constructor:
FormattedTextOptions(FormattedTextMode mode);
FormattedTextMode enumeration has the following members:
Member | Description |
---|---|
Html | HTML format. |
Markdown | Markdown format. |
PlainText | Plain text format. |
TextReader class extends java.io.Reader and adds 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. |
Here are the steps to extract a HTML formatted text from the document:
- Instantiate Parser object for the initial document;
- Instantiate FormattedTextOptions with HTML text mode;
- Call getFormattedText(FormattedTextOptions) method and obtain 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:
// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleDocx)) {
// Extract a formatted text into the reader
try (TextReader reader = parser.getFormattedText(new FormattedTextOptions(FormattedTextMode.Html))) {
// Print a formatted text from the document
// If formatted text extraction isn't supported, a reader is null
System.out.println(reader == null ? "Formatted text extraction isn't suppported" : reader.readToEnd());
}
}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Free online document parser App
Along with full featured Java 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.