springboot+tabula解析pdf中的表格数据

场景

在日常业务需求中,往往会遇到解析pdf数据获取文本的需求,常见的做法是使用 pdfbox 来做,但是它只适合做一些简单的段落文本解析,无法处理表格这种复杂类型,因为单元格中的文本有换行的情况,无法对应到我们业务具体的属性上面去。而 tabula 在它的基础上做了表格的特殊处理,使用案例如下:

引入依赖

xml 复制代码
<!-- PDF解析,内含pdfbox -->
<dependency>
    <groupId>technology.tabula</groupId>
    <artifactId>tabula</artifactId>
    <version>1.0.5</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.53</version>
</dependency>

代码实现

java 复制代码
package net.lab1024.sa.admin.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import technology.tabula.*;
import technology.tabula.extractors.SpreadsheetExtractionAlgorithm;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PdfUtil {

    public static void main(String[] args) {
        JSONArray jsonArray = readPdfTable("C:\\Users\\admin\\Desktop\\xxx.pdf");
        System.out.println(jsonArray);
    }

    /**
     * 解析pdf的表格
     *
     * @param filePath
     * @return
     */
    public static JSONArray readPdfTable(String filePath) {
        // 表头
        // todo 这里自己先定义了,使用时可读取表头的中文作为key或者将中文翻译成英文作为key
        List<String> fieldList = new ArrayList<>();
        fieldList.add("jydh");
        fieldList.add("jysj");
        fieldList.add("jylx");
        fieldList.add("szqt");
        fieldList.add("jyfs");
        fieldList.add("je");
        fieldList.add("jydf");
        fieldList.add("shdh");

        JSONArray jsonArray = new JSONArray();

        // 表格提取算法
        SpreadsheetExtractionAlgorithm algorithm = new SpreadsheetExtractionAlgorithm();

        try (PDDocument document = PDDocument.load(new File(filePath))) {
            ObjectExtractor extractor = new ObjectExtractor(document);
            PageIterator pi = extractor.extract();
            // 遍历页
            while (pi.hasNext()) {
                Page page = pi.next();
                List<Table> tableList = algorithm.extract(page);
                // 遍历表
                for (Table table : tableList) {
                    List<List<RectangularTextContainer>> rowList = table.getRows();
                    // 遍历行
                    for (List<RectangularTextContainer> row : rowList) {
                        JSONObject jsonObject = new JSONObject();
                        // 遍历列
                        for (int i = 0; i < row.size(); i++) {
                            RectangularTextContainer cell = row.get(i);
                            String text = cell.getText().replace("\r", "");
                            jsonObject.put(fieldList.get(i), text);
                        }
                        jsonArray.add(jsonObject);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

}
相关推荐
叫我阿柒啊3 小时前
Java全栈开发面试实战:从基础到微服务架构
java·vue.js·spring boot·redis·git·full stack·interview
爱上纯净的蓝天4 小时前
迁移面试题
java·网络·c++·pdf·c#
UtopianCoding6 小时前
MinerU:重新定义PDF智能提取的开源利器
docker·pdf·开源
ZZHow10247 小时前
Java项目-苍穹外卖_Day1
java·spring boot·web
ZZHow10248 小时前
Java项目-苍穹外卖_Day2
java·spring boot·web
float_六七8 小时前
Spring Boot 3为何强制要求Java 17?
java·spring boot·后端
RainbowJie19 小时前
Gemini CLI 与 MCP 服务器:释放本地工具的强大潜力
java·服务器·spring boot·后端·python·单元测试·maven
灰原喜欢柯南11 小时前
Spring Boot 自动配置全流程深度解析
java·spring boot·后端
毕设源码-赖学姐12 小时前
【开题答辩全过程】以 基于Springboot+微信小程序的网上家教预约系统的设计与实现-开题为例,包含答辩的问题和答案
spring boot·后端·微信小程序