springboot集成pdfbox解析pdf文件

springboot集成pdfbox解析pdf文件

1、引入依赖

c 复制代码
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.24</version> <!-- 请检查最新版本 -->
</dependency>

2、获取PDF文件

c 复制代码
 String filePath = "C:\\xx\\a.pdf"; // pdf的路径
    PDDocument document = PDDocument.load(new File(filePath));
    PDFTextStripper pdfStripper = new PDFTextStripper();
    // 排序文本行按其位置
    pdfStripper.setSortByPosition(true);
    //获取整个pdf文本内容
    String text = pdfStripper.getText(document);
    document.close();

3、获取需要的字段信息

c 复制代码
    private Map<String,String> pdfStr(String invoiceInfo) {
        Map<String,String> map=new HashMap<>();
        //因解析出的括号不确定为中文还是英文,统一替换为英文字符
        invoiceInfo = invoiceInfo.replaceAll("(","(").replaceAll(")",")");
        // 定义正则表达式模式
        Pattern patternInvoiceNumber = Pattern.compile("发票号码:(\\d+)");

        // 创建Matcher对象
        Matcher matcherInvoiceNumber = patternInvoiceNumber.matcher(invoiceInfo);

        // 提取数据
        if (matcherInvoiceNumber.find()) {
            map.put("invoiceNumber",matcherInvoiceNumber.group(1));
        }
        return map;
    }

4、获取多字段时,步骤3需要优化,以下为优化后代码

c 复制代码
public static Map<String, String> pdfStr(String invoiceInfo) {
        // 提取数据
        Map<String, String> result = new HashMap<>();

        invoiceInfo = invoiceInfo.replaceAll("(", "(").replaceAll(")", ")");

        // 定义正则表达式模式
        Map<String, String> patterns = new HashMap<>();
        patterns.put("invoiceNumber", "发票号码:(\\d+)");
        patterns.put("invoiceDate", "开票日期:(\\d{4}年\\d{1,2}月\\d{1,2}日)");
        patterns.put("buyerName", "购 名称:(.+?) 销 名称:(.+?)\n");
        patterns.put("itemDetails", "税 额\\s+(.*?)合 计");
        patterns.put("total", "\\(小写\\)¥(\\d+(\\.\\d+)?)");
        patterns.put("batchNumber", "批号:(.+?)/");
        patterns.put("productionDate", "生产日期:(\\d{4}-\\d{1,2}-\\d{1,2})/");
        patterns.put("expirationDate", "有效期至:(\\d{4}-\\d{1,2}-\\d{1,2})/");
        patterns.put("taxIncludedPrice", "含税单价:(\\d+(\\.\\d+)?)");
        patterns.put("manufacturer", "生产厂家:(.+?)/");
        patterns.put("approvalNumber", "批准文号:(.+?)/");
        patterns.put("issuer", "开票人:(.+)");


        for (Map.Entry<String, String> entry : patterns.entrySet()) {
            Pattern pattern = Pattern.compile(entry.getValue(), Pattern.DOTALL);
            Matcher matcher = pattern.matcher(invoiceInfo);
            if (matcher.find()) {
                result.put(entry.getKey(), matcher.group(1).trim());
            }
        }

        // 处理项目名称、规格型号、单位、数量、单价、金额、税率/征收率、税额
        if (result.containsKey("itemDetails")) {
            String[] details = result.get("itemDetails").replace("\n", " ").split(" ");
            if (details.length >= 8) {
                result.put("productName", details[0].trim() + (details.length > 8 ? details[8].trim() : ""));
                result.put("specification", details[1].trim());
                result.put("unit", details[2].trim());
                result.put("quantity", details[3].trim());
                result.put("unitPrice", details[4].trim());
                result.put("amount", details[5].trim());
                result.put("taxRate", details[6].trim());
                result.put("taxAmount", details[7].trim());
            }
        }
        return result;
    }
相关推荐
武子康9 小时前
Java-07 深入浅出 MyBatis数据库一对多关系模型实战:表结构设计与查询实现
java·后端
花椒技术10 小时前
企业内部 Agent 落地复盘:Gateway、Skill 和二次确认如何串起受控业务执行
后端·agent·ai编程
我是一颗柠檬11 小时前
【MySQL全面教学】MySQL事务与ACID Day9(2026年)
数据库·后端·mysql
枕星而眠12 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端
IT_陈寒12 小时前
React useEffect闭包陷阱差点把我整失业了
前端·人工智能·后端
苍何12 小时前
爆肝两周,我把 Codex 最全实战指南开源了
后端
苏渡苇12 小时前
服务容错的必要性与Spring Cloud Alibaba Sentinel 限流配置实战
spring boot·spring cloud·sentinel
bug菌13 小时前
【SpringBoot 3.x 第254节】夯爆了,数据库访问性能优化实战详解!
数据库·spring boot·后端
Rust研习社13 小时前
从碎片化到标准化:cargo-bp 如何重构 Rust 开发逻辑
后端·rust·编程语言
锋行天下13 小时前
一句mysql复杂查询搞崩一个壮汉
后端·mysql·go