Extract text from PDF documents
To extract a text from PDF documents getText and getText(int) methods are used. These methods allow to extract a text from the entire document or a text from the selected page.
Here are the steps to extract a text from PDF document:
- Instantiate Parser object for the initial document;
- Call getText method and obtain TextReader object;
- Read a text from reader.
The following example demonstrates how to extract a text from PDF 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
System.out.println(reader.readToEnd());
}
}
Here are the steps to extract a text from the page of PDF document:
- Instantiate Parser object for the initial document;
- Call getDocumentInfo method and obtain IDocumentInfo object with getPageCount property;
- Call getText(int) method with the page index and obtain TextReader object;
- Read a text from reader.
The following example demonstrates how to extract a text from the page of PDF document:
// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SamplePdf)) {
// Get the document info
IDocumentInfo documentInfo = parser.getDocumentInfo();
// 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 page
System.out.println(reader.readToEnd());
}
}
}
Raw mode allows to increase the speed of text extraction due to poor formatting accuracy. getText(TextOptions) and getText(int, TextOptions) methods are used to extract a text in raw mode.
Here are the steps to extract a raw text from the page of PDF document:
- Instantiate Parser object for the initial document;
- Instantiate TextOptions object with true parameter;
- Call getDocumentInfo method;
- Use getRawPageCount instead of getPageCount to avoid extra calculations;
- Call getText(int, TextOptions) method with the page index and obtain TextReader object;
- Read a text from reader.
The following example demonstrates how to extract a raw text from the page of PDF document:
// 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
DocumentInfo documentInfo = parser.getDocumentInfo() instanceof DocumentInfo
? (DocumentInfo) parser.getDocumentInfo()
: null;
// 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 .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.