Как выполнять извлечение таблиц из PDF на Java с помощью GroupDocs.Parser
Извлечение таблиц из PDF‑файлов часто требуется, когда необходимо превратить статические документы в структурированные данные. В этом руководстве вы узнаете как извлекать таблицы из PDF с помощью библиотеки GroupDocs.Parser для Java. Мы рассмотрим настройку окружения, конфигурацию макета таблиц и то, как экспортировать таблицы PDF в CSV для последующей обработки. К концу вы сможете интегрировать надёжное извлечение таблиц в любой Java‑ориентированный конвейер данных.
Быстрые ответы
- What is the primary library? GroupDocs.Parser for Java
- Can I extract tables from scanned PDFs? Only after OCR; see “extract tables scanned pdf” note below
- Do I need a license? A trial license works for development; a full license is required for production
- Which Java version is required? Java 8 or higher
- Is batch processing supported? Yes – the API is optimized for large‑scale extraction
Что такое извлечение таблиц из PDF на Java?
Извлечение таблиц из PDF на Java — это процесс программного обнаружения табличных структур внутри PDF, интерпретации границ ячеек и получения текста в машинно‑читаемом формате, таком как CSV или Excel. Это позволяет выполнять последующий анализ, отчётность или миграцию без ручного копирования‑вставки.
Почему стоит использовать GroupDocs.Parser для извлечения таблиц из PDF на Java?
GroupDocs.Parser обеспечивает точное определение макета более чем для 50 + входных и выходных форматов и может обрабатывать многосотенные PDF, удерживая потребление памяти ниже 200 МБ. Он поддерживает пакетную обработку, предлагает простую зависимость Maven и бесшовно интегрируется с GroupDocs OCR для сценариев со сканированными документами.
Предварительные требования
Перед началом убедитесь, что у вас есть следующее:
- Java 8+ установлен и настроен в вашей IDE или системе сборки.
- Maven для управления зависимостями.
- Доступ к лицензии GroupDocs.Parser (пробной или полной).
Требуемые библиотеки и зависимости
Вам понадобится:
- Библиотека GroupDocs.Parser для Java (версия 25.5 или новее).
- Maven, установленный в системе, для управления зависимостями.
Настройка окружения
Убедитесь, что ваша среда разработки настроена с совместимой версией Java (Java 8 или выше).
Необходимые знания
Базовое понимание программирования на Java и знакомство с работой с файлами в Java будут полезны.
Настройка GroupDocs.Parser для Java
Чтобы начать использовать GroupDocs.Parser, интегрируйте его в ваш проект следующим образом:
Maven setup
Add the following configuration to your pom.xml file to include GroupDocs.Parser as a dependency:
<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>
Direct download
Alternatively, download the latest version of GroupDocs.Parser for Java from GroupDocs releases.
Приобретение лицензии
Start with a free trial, obtain a temporary license, or purchase a full license. Visit the GroupDocs licensing page for details.
Базовая инициализация и настройка
Initialize GroupDocs.Parser in your Java application as follows:
import com.groupdocs.parser.Parser;
public class DocumentParser {
public static void main(String[] args) {
final String filePath = "YOUR_DOCUMENT_DIRECTORY/SampleInvoicePagesPdf.pdf";
try (Parser parser = new Parser(filePath)) {
// Ready to perform operations on the document
} catch (Exception e) {
System.err.println("Error creating Parser instance: " + e.getMessage());
}
}
}
Руководство по реализации
Давайте пройдёмся по каждому элементу, который вам нужно освоить, чтобы извлекать таблицы из PDF.
Функция 1: разбор документа с помощью GroupDocs
Overview
To interact with a PDF document, create an instance of the Parser class.Parser is the entry point class for reading PDF content in GroupDocs.Parser. This enables various operations on the document.
Creating a parser instance
The Parser class is the entry point for reading PDF content in GroupDocs.Parser. It loads the document into memory and exposes methods for extracting text, tables, and other structures.
import com.groupdocs.parser.Parser;
public class CreateParserInstance {
public static void main(String[] args) {
final String filePath = "YOUR_DOCUMENT_DIRECTORY/SampleInvoicePagesPdf.pdf";
try (Parser parser = new Parser(filePath)) {
// Document is ready for operations
} catch (Exception e) {
System.err.println("Error creating Parser instance: " + e.getMessage());
}
}
}
Функция 2: проверка возможности извлечения таблиц
Overview
Before extracting tables, verify that the PDF supports table extraction.
Checking table support
The hasTables() method returns a boolean indicating whether the loaded PDF contains detectable tabular data.hasTables() checks if the document contains any tables.
import com.groupdocs.parser.Parser;
public class CheckTableSupport {
public static void main(String[] args) {
final String filePath = "YOUR_DOCUMENT_DIRECTORY/SampleInvoicePagesPdf.pdf";
try (Parser parser = new Parser(filePath)) {
boolean isTablesSupported = parser.getFeatures().isTables();
if (!isTablesSupported) {
System.out.println("Document doesn't support tables extraction.");
}
} catch (Exception e) {
System.err.println("Error checking table extraction capability: " + e.getMessage());
}
}
}
Функция 3: конфигурация макета таблицы
Overview
Configuring the layout of your tables can enhance accuracy in data extraction.
Setting up table layoutTemplateTableLayout defines the expected column widths and row heights.TemplateTableLayout specifies custom column widths and row heights for table detection. Adjusting these values helps the engine align cell boundaries with the visual grid.
import com.groupdocs.parser.templates.TemplateTableLayout;
import java.util.Arrays;
public class ConfigureTableLayout {
public static void main(String[] args) {
final double[] columnWidths = {50.0, 95.0, 275.0, 415.0, 485.0, 545.0};
final double[] rowHeights = {325.0, 340.0, 365.0, 395.0};
TemplateTableLayout layout = new TemplateTableLayout(
Arrays.asList(columnWidths),
Arrays.asList(rowHeights));
}
}
Функция 4: настройка параметров извлечения таблиц
Overview
Set up options for extracting tables with specific configurations to improve extraction accuracy.
Configuring extraction optionsTableExtractionOptions lets you specify whether to include header rows, merge cells, or ignore empty rows.TableExtractionOptions configures extraction behavior such as including headers or merging cells.
import com.groupdocs.parser.options.PageTableAreaOptions;
import com.groupdocs.parser.templates.TemplateTableLayout;
public class SetExtractionOptions {
public static void main(String[] args) {
TemplateTableLayout layout = new TemplateTableLayout(
Arrays.asList(new Double[]{50.0, 95.0, 275.0, 415.0, 485.0, 545.0}),
Arrays.asList(new Double[]{325.0, 340.0, 365.0, 395.0}));
PageTableAreaOptions options = new PageTableAreaOptions(layout);
}
}
Функция 5: извлечение таблиц из документа
Overview
Extract tables using configured options and process them as needed.
Extraction process
The getTables() method returns a collection of Table objects, each representing a detected table on the requested pages.getTables() retrieves all detected tables from the document.Table represents a single extracted table with rows and cells.
import com.groupdocs.parser.Parser;
import com.groupdocs.parser.options.PageTableAreaOptions;
import com.groupdocs.parser.data.PageTableArea;
public class ExtractTables {
public static void main(String[] args) {
final String filePath = "YOUR_DOCUMENT_DIRECTORY/SampleInvoicePagesPdf.pdf";
PageTableAreaOptions options = new PageTableAreaOptions(/* layout from previous feature */);
try (Parser parser = new Parser(filePath)) {
Iterable<PageTableArea> tables = parser.getTables(options);
for (PageTableArea table : tables) {
// Process each table as needed
}
} catch (Exception e) {
System.err.println("Error extracting tables: " + e.getMessage());
}
}
}
Функция 6: перебор строк и столбцов таблицы
Overview
After extraction, iterate over rows and columns to access individual cells.
Iterate and access cells
Each Table provides getRows() and each Row provides getCells(). You can read the cell text via getText() and write it to CSV or any other format.Row represents a single row within a Table.getRows() returns the list of rows in a table.getCells() returns the cells of a row.getText() retrieves the textual content of a cell.
import com.groupdocs.parser.data.PageTableArea;
import com.groupdocs.parser.data.PageTableAreaCell;
public class IterateTables {
public static void main(String[] args) {
PageTableArea table = /* reference to a specific PageTableArea object */;
for (int row = 0; row < table.getRowCount(); row++) {
for (int column = 0; column < table.getColumnCount(); column++) {
PageTableAreaCell cell = table.getCell(row, column);
if (cell != null) {
// Process the cell text as needed
}
}
}
}
}
Распространённые проблемы и решения
| Проблема | Почему происходит | Совет |
|---|---|---|
| No tables returned | The PDF is scanned (image‑based) | Run OCR first or use GroupDocs OCR before parsing. |
| Incorrect column alignment | Layout coordinates are off | Fine‑tune TemplateTableLayout values to match the visual grid. |
| Memory spikes on large PDFs | Parser loads whole document into memory | Process pages in batches and close the Parser after each batch. |
Часто задаваемые вопросы
1. Могу ли я извлекать таблицы из сканированных PDF или только из цифровых PDF?
Answer: GroupDocs.Parser primarily works with digital, selectable PDFs that contain embedded text. For scanned PDFs, you’ll need to run OCR first—either with GroupDocs OCR or another OCR engine—so that the text becomes searchable before table extraction.
2. Как работать с таблицами сложного макета или со слитными ячейками?
Answer: Customize the TemplateTableLayout with precise column and row coordinates, or enable the mergeCells flag in TableExtractionOptions. Post‑processing may be required to interpret merged regions correctly.
3. Подходит ли GroupDocs.Parser для больших документов или пакетной обработки?
Answer: Yes. The library is built for high‑throughput scenarios and can process PDFs with hundreds of pages while keeping memory consumption low. Use page‑range options and dispose of the Parser instance after each batch to maximise performance.
4. Могу ли я экспортировать извлечённые данные таблиц в форматы CSV или Excel?
Answer: GroupDocs.Parser returns raw table data (rows and cells). You can easily write this data to CSV using OpenCSV or to Excel using Apache POI. This fulfills the export pdf tables csv use‑case without additional licensing.
5. Есть ли поддержка извлечения таблиц с нескольких страниц одновременно?
Answer: Absolutely. Call parser.getTables(pageOptions) with a page range or iterate over all pages. The API aggregates tables across pages, letting you build a single consolidated dataset.
Заключение
Java pdf table extraction становится простой задачей с GroupDocs.Parser. Инициализировав Parser, проверив поддержку таблиц, настроив макет и параметры извлечения, а затем перебрав полученные объекты Table, вы сможете превратить статические PDF в структурированные CSV или Excel файлы. Производительность библиотеки, поддержка более 50 форматов и бесшовная интеграция с OCR делают её идеальным выбором для автоматизации обработки счетов, миграции данных и масштабных аналитических конвейеров. Следуя приведённым шагам, вы готовы внедрить надёжное извлечение таблиц в любое Java‑приложение.
Last Updated: 2026-09-17
Tested With: GroupDocs.Parser 25.5 (Java)
Author: GroupDocs