Извлечение текста из PDF в Java с помощью GroupDocs.Parser OCR
В современных конвейерах обработки документов быстрое и надёжное extract text from PDF java является необходимым. Если вам нужно оцифровать исторические бумажные архивы или создать сервис чтения счетов, который должен read image text java из определённых зон, OCR‑движок GroupDocs.Parser предоставляет чистый, программируемый способ сделать это. Это руководство проведёт вас через установку библиотеки, настройку OCR для конкретного прямоугольника и обработку ошибок, чтобы ваше приложение оставалось надёжным.
Быстрые ответы
- What does “extract text from PDF” mean? It converts the visual content of a scanned PDF into searchable, editable text.
- Which Java library provides OCR? GroupDocs.Parser with the built‑in Aspose OCR connector.
- Is a license required for production? Yes—use a free trial for testing, then obtain a paid license for deployment.
- Can OCR be limited to a region? Absolutely; pass a
RectangletoOcrOptionsto target only the area you need. - Do I need special error handling? Yes—wrap OCR calls in try‑catch blocks to keep the app stable if a page is corrupted.
Что такое extract text from PDF java?
Extract text from PDF java — это процесс применения оптического распознавания символов (OCR) к страницам PDF, содержащим изображения, чтобы символы стали машинно‑читаемым текстом. Это позволяет выполнять полнотекстовый поиск, индексацию и последующее извлечение данных в Java‑приложениях, позволяя разработчикам программно анализировать и манипулировать содержимым документов.
Почему использовать GroupDocs.Parser для OCR в Java?
GroupDocs.Parser поддерживает 50+ input and output formats и может обрабатывать многосотенные PDF без загрузки всего файла в память, обеспечивая до 40 % ускорения, когда OCR ограничен прямоугольником. Его бесшовная интеграция с движком Aspose OCR даёт высокую точность распознавания «из коробки», особенно для распространённых латинских языков.
Требования
- Java Development Kit 8 или новее.
- GroupDocs.Parser library – установить через Maven или скачать напрямую.
- Базовое знакомство с Java try‑with‑resources и обработкой исключений.
Настройка GroupDocs.Parser для Java
Установка через Maven
Add the repository and dependency to your pom.xml:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/parser/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-parser</artifactId>
<version>25.5</version>
</dependency>
</dependencies>
Прямое скачивание
Alternatively, download the latest version from GroupDocs.Parser for Java releases.
Приобретение лицензии
Start with a free trial or request a temporary license for full feature access. For production, purchase a permanent license.
Базовая инициализация и настройка
After adding the library, you’re ready to tap into its OCR capabilities.
Руководство по реализации
Как извлечь текст из отсканированного PDF с заданным прямоугольником
Targeting a specific area improves speed and accuracy, especially when you only need to read image text java from a known region.
Direct answer: Load the PDF with Parser using OCR‑enabled settings, define a Rectangle that encloses the desired text, and call extractText – the entire operation finishes in two to three lines of code and returns the recognized string.
Шаг 1: настройка параметров OCR
ParserSettings is the central configuration object that tells GroupDocs.Parser which OCR engine to use.
ParserSettings settings = new ParserSettings(new AsposeOcrOnPremise());
Шаг 2: инициализация парсера
Parser is the entry point for all document‑reading operations.
try (Parser parser = new Parser("YOUR_DOCUMENT_DIRECTORY", settings)) {
// Proceed to define OCR area and extract text.
}
Шаг 3: определение области для OCR
Rectangle represents a rectangular region on a page, defined by its X/Y origin and width/height in pixels.
OcrOptions ocrOptions = new OcrOptions(new Rectangle(0, 0, 400, 200));
Этот прямоугольник начинается в левом верхнем углу (0,0) и имеет ширину 400 px и высоту 200 px.
Шаг 4: настройка параметров текста
OcrOptions lets you enable OCR only for the rectangle you defined, leaving the rest of the page untouched.
TextOptions options = new TextOptions(false, true, ocrOptions);
false disables language‑specific restrictions, while true activates the OCR area.
Шаг 5: извлечение текста
extractText returns the OCR‑processed string for the specified page and region.
try (TextReader reader = parser.getText(options)) {
String resultText = reader == null ? "Text extraction isn't supported" : reader.readToEnd();
// Use extracted text as needed.
}
Шаг 6: обработка ошибок при обработке OCR
Wrap the whole operation in a try‑catch block to capture any issues, such as unsupported image formats or memory pressure.
try {
// Include main OCR processing logic here (refer to previous section).
} catch (Exception ex) {
System.out.println("An error occurs: " + ex.getMessage());
}
Это гарантирует стабильную работу вашего приложения даже если OCR‑движок столкнётся с неожиданным форматом.
Практические применения
- Invoice processing – Pull key fields from scanned invoices automatically.
- Document digitization – Convert legacy paper archives into searchable PDFs.
- Data‑entry automation – Eliminate manual typing by reading image text java from forms.
Соображения по производительности
- Resource usage – Monitor memory, especially with large PDFs; GroupDocs.Parser processes pages lazily to keep the heap low.
- Java memory management – Use try‑with‑resources (as shown) to close streams promptly.
- Batch processing – Parallelize OCR across multiple documents when possible; the library is thread‑safe for read‑only operations.
Распространённые проблемы и решения
| Проблема | Решение |
|---|---|
| Out‑of‑memory errors on large files | Process pages in smaller batches; increase JVM heap (-Xmx2g) if needed. |
| Poor OCR accuracy | Increase source image DPI to 300 + or supply language hints in ParserSettings. |
| Unsupported file format | Verify the file is a supported PDF or image type; convert unsupported formats to PNG first. |
Часто задаваемые вопросы
Q: What is OCR in the context of Java development?
A: Optical Character Recognition (OCR) converts images of text into machine‑encoded characters, and GroupDocs.Parser provides a Java‑friendly API to do this without external native dependencies.
Q: How do I define a rectangular area for OCR extraction?
A: Create a Rectangle object with the desired X, Y, width, and height, then pass it to OcrOptions when calling extractText.
Q: What are common errors during OCR processing, and how can I handle them?
A: Errors include unsupported formats or mis‑configured settings; always surround OCR calls with try‑catch blocks and log the exception details.
Q: Can I use GroupDocs.Parser without a license?
A: A free trial is available for evaluation, but a licensed version is required for production deployments.
Q: How can I optimise OCR performance in Java applications?
A: Limit OCR to necessary regions, reuse ParserSettings across documents, and run OCR in parallel batches when processing many files.
Ресурсы
- Documentation: GroupDocs.Parser Documentation
- API reference: API Reference Guide
- Download: Latest Releases
- GitHub repository: GroupDocs.Parser GitHub
- Free support: GroupDocs Forum
- Temporary license: Obtain a Temporary License
Last Updated: 2026-09-02
Tested With: GroupDocs.Parser 25.5
Author: GroupDocs