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

相关推荐
Full Stack Developme8 小时前
java.text 包详解
java·开发语言·python
刘梦凡呀9 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
盒马coding9 小时前
第19节-非规范化数据类型-Composite-types
数据库·postgresql
best_virtuoso9 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian20149 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
-雷阵雨-9 小时前
MySQL——桥梁JDBC
数据库·mysql·oracle
亿坊电商9 小时前
在PHP框架里如何进行数据库连接?
数据库·oracle·php
满昕欢喜9 小时前
SQL Server从入门到项目实践(超值版)读书笔记 28
数据库·sql·sqlserver
楚韵天工10 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
helloworddm10 小时前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#