Extract text in Accurate Mode

GroupDocs.Parser provides the functionality to extract text from documents.

The Accurate mode is default text extraction mode and it means that text quality will be the best possible.

You can extract the whole document text or only a document page.

To extract a text from the document in the Accurate mode, getText and getText(int) methods are used:

TextReader getText();
TextReader getText(int pageIndex);

The methods return an instance of TextReader class with an extracted text. The first method extracts a text from the whole document. The second method extracts a text from the document page. To retrieve the total number of document pages getDocumentInfo method is used (see below).

TextReader class extends java.io.Reader and adds the following members:

MemberDescription
readLineReads a line of characters from the text reader and returns the data as a string.
readToEndReads all characters from the current position to the end of the text reader and returns them as one string.

Extract text

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

  • Instantiate Parser object for the initial document;
  • Call getText method and obtain TextReader object;
  • Check if reader isn’t null (text extraction is supported for the document);
  • Read a text from reader.

The following example shows how to extract a text from a document:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SamplePdf)) {
    // Extract a text into the reader
    try (TextReader reader = parser.getText()) {
        // Print a text from the document
        // If text extraction isn't supported, a reader is null
        System.out.println(reader == null ? "Text extraction isn't supported" : reader.readToEnd());
    }
} 

Extract text from page

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

  • Instantiate Parser object for the initial document;
  • Call isText property to check if text extraction is supported for the document;
  • Call getDocumentInfo method;
  • Call getText(int) method with the page index and obtain TextReader object;
  • Read a text from reader.

The following example shows how to extract a text from the document page:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SamplePdf)) {
    // Check if the document supports text extraction
    if (!parser.getFeatures().isText()) {
        System.out.println("Document isn't supports text extraction.");
        return;
    }
    // Get the document info
    IDocumentInfo documentInfo = parser.getDocumentInfo();
    // Check if the document has pages
    if (documentInfo.getPageCount() == 0) {
        System.out.println("Document hasn't pages.");
        return;
    }
    // Iterate over pages
    for (int p = 0; p < documentInfo.getPageCount(); p++) {
        // Print a page number
        System.out.println(String.format("Page %d/%d", p + 1, documentInfo.getPageCount()));
        // Extract a text into the reader
        try (TextReader reader = parser.getText(p)) {
            // Print a text from the document
            // We ignore null-checking as we have checked text extraction feature support earlier
            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 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.