Extract text from HTML documents

To extract a text from HTML documents getText method is used. This method allows to extract a text from the entire document. Pagination and raw mode is not supported for emails.

Here are the steps to extract a text from HTML document:

  • Instantiate Parser object for the initial document;
  • Call getText method and obtain TextReader object;
  • Read a text from reader.
Warning
getText method returns null value if text extraction isn’t supported for the document. For example, text extraction isn’t supported for Zip archive. Therefore, for Zip archive getText method returns null. For an HTML document getText method returns an empty TextReader object (readToEnd method returns an empty string).

The following example demonstrates how to extract a text from HTML document:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleHtml)) {
    // Extract a text into the reader
    try (TextReader reader = parser.getText()) {
        // Print a text from the document
        System.out.println(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 .NET 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.