Extract text in Raw Mode

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

The Raw mode is the fastest text extraction mode and it means that performance wlll be the best possible.

Raw mode usually retrieves text in worse quality than Accurate mode, but in some cases performance is more important than quality.

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

To extract a text from the document in the Raw mode, getText(TextOptions) and getText(int, TextOptions) class are used:

TextReader getText(TextOptions options);
TextReader getText(int pageIndex, TextOptions options);

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 raw text from the document:

  • Instantiate Parser object for the initial document;
  • Instantiate TextOptions object with true parameter;
  • Call getText(TextOptions) method with TextOptions parameter 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 raw text from a document:

// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SamplePdf)) {
    // Extract a raw text into the reader
    try (TextReader reader = parser.getText(new TextOptions(true))) {
        // 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 raw text from the document page:

The following example shows how to extract a raw text from a 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 == null || documentInfo.getRawPageCount() == 0) {
        System.out.println("Document hasn't pages.");
        return;
    }
    // Iterate over pages
    for (int p = 0; p < documentInfo.getRawPageCount(); 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, new TextOptions(true))) {
            // 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.