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;
    }

}
相关推荐
南星沐2 小时前
Spring Boot 常用依赖介绍
java·前端·spring boot
老李不敲代码3 小时前
榕壹云外卖跑腿系统:基于Spring Boot+MySQL+UniApp的智慧生活服务平台
spring boot·mysql·微信小程序·uni-app·软件需求
smileNicky6 小时前
SpringBoot系列之集成Redisson实现布隆过滤器
java·spring boot·redis·布隆过滤器
李白的粉7 小时前
基于springboot的个人博客系统
java·spring boot·后端·毕业设计·课程设计·源代码·个人博客系统
小蒜学长8 小时前
机动车号牌管理系统设计与实现(代码+数据库+LW)
开发语言·数据库·spring boot·后端·spring·oracle
半个脑袋儿8 小时前
Maven版本号管理规范:为何父POM是统一依赖版本的最佳实践?
spring boot·maven
howard20058 小时前
3.1.3.2 Spring Boot使用Servlet组件
spring boot·servlet
来自星星的坤9 小时前
如何在 Postman(测试工具) 中实现 Cookie 持久化并保持同一会话
java·开发语言·spring boot·后端
2401_8906658611 小时前
免费送源码:Java+SpringBoot+MySQL SpringBoot网上宠物领养管理系统 计算机毕业设计原创定制
java·vue.js·spring boot·python·mysql·pycharm·html5