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中的表格内容解析(每个表格为一块数据),解析完后,可以对字符串做数据业务处理。

相关推荐
Homeey11 分钟前
深入理解ThreadLocal:从原理到架构实践的全面解析
java·后端
ANGLAL13 分钟前
27.短链系统
java
wei_shuo13 分钟前
全场景自动化 Replay 技术:金仓 KReplay 如何攻克数据库迁移 “难验证“ 难题
数据库·自动化·king base
葡萄城技术团队14 分钟前
打破误解!MongoDB 事务隔离级别深度实测:快照隔离竟能防住 8 种异常?
数据库
Gold Steps.16 分钟前
数据库正常运行但是端口变成了0?
数据库·mysql
杂亿稿20 分钟前
增删改查操作
数据库
Code_Geo21 分钟前
在postgres数据库中Postgres FDW 全面详解
数据库·fdw
QT 小鲜肉31 分钟前
【个人成长笔记】将Try Ubuntu里面配置好的文件系统克隆在U盘上(创建一个带有持久化功能的Ubuntu Live USB系统)
linux·开发语言·数据库·笔记·ubuntu
Lisonseekpan31 分钟前
Git 命令大全:从基础到高级操作
java·git·后端·github·团队开发
LWy61042635 分钟前
数据库库、表的创建及处理
数据库