spire.pdf.free解析pdf,根据表格划分

pom

XML 复制代码
  <repositories>
      <repository>
          <id>com.e-iceblue</id>
          <name>e-iceblue</name>
          <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
      </repository>
  </repositories>

  <!-- https://mvnrepository.com/artifact/e-iceblue/spire.pdf.free -->
  <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.pdf.free</artifactId>
      <version>5.1.0</version>
  </dependency>

  <!--操作word文档内容-->
  <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
  </dependency>

pdf解析

java 复制代码
        // 创建一个PdfDocument对象
        PdfDocument pdf = new PdfDocument();
        // 加载一个PDF文档
        pdf.loadFromFile("XXX.pdf");
        // 创建一个PdfTableExtractor对象
        PdfTableExtractor extractor = new PdfTableExtractor(pdf);
        StringBuilder sb = new StringBuilder();
        // 从每一页中提取表格
        for (int pageIndex = 0; pageIndex < pdf.getPages().getCount(); pageIndex++) {
            if(StringUtils.isNotBlank(sb.toString())){
                sb.append("\n");
            }
            PdfTable[] tables = extractor.extractTable(pageIndex);
            // 如果表格不为空,则遍历表格
            if (tables != null) {
                for (int tableIndex = 0; tableIndex < tables.length; tableIndex++) {
                    PdfTable table = tables[tableIndex];
                    // 创建一个StringBuilder对象
                    StringBuilder tableText = new StringBuilder();
                    // 遍历行和列
                    for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) {
                        for (int colIndex = 0; colIndex < table.getColumnCount(); colIndex++) {
                            // 获取单元格文本并移除换行符
                            String cellText = table.getText(rowIndex, colIndex);
                            cellText = cellText.replaceAll("\\r|\\n", "");
                            if (colIndex < table.getColumnCount() - 1) {
                                tableText.append(cellText).append("\t");
                            } else {
                                tableText.append(cellText).append("\n");
                            }
                        }
                    }
                    sb.append(tableText);
                }
            }
        }

注意:如果还需要用到doc,必须要和pdf的pom版本兼容,例如上面的5.1.0和5.2.0。因为是免费版,所以只能解析10页数据,如果超过10页,可以去查找如何摆脱限制,这里不在赘述。

该案例只是将pdf中的表格内容解析(每个表格为一块数据),解析完后,可以对字符串做数据业务处理。

相关推荐
雾时之林几秒前
Linux--软件管理、源码包安装
linux·服务器·数据库
9523638 分钟前
Spring-cloud配置中心
java·spring·spring cloud·微服务
ydd1001001 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
老纪的技术唠嗑局2 小时前
超级干货分享:中小企业使用 OceanBase 实践经验汇总
数据库
CLOUD ACE2 小时前
谷歌云代理|零售商Target 如何利用 Spanner Graph 提升零售发现体验并将数据库维护成本降低 50%
数据库·零售
真上帝的左手2 小时前
10. 软件设计&架构-Spring Security 7 整合CAS SSO 单点登录
java·spring·架构·sso
AI大模型-小雄2 小时前
Codex写分页接口为什么越翻越慢?用Cursor Pagination解决重复与漏数据
大数据·数据库·elasticsearch·搜索引擎·chatgpt·后端开发·codex
Zane19942 小时前
单例线程安全、生产者消费者、死锁:并发面试三连问串讲
java·后端
少晓年3 小时前
从 MySQL 迁移到人大金仓 KingbaseES:完整指南与实践
数据库·mysql
云烟成雨TD3 小时前
Micrometer 系列【42】链路追踪:Span 体系 | 核心 API
java·链路追踪·micrometer