调用Apache PDFBox库读取PDF数据

PDF发票单,正则表达式提数

在这里插入图片描述

1、引入jar包,fontbox-2.0.31.jar、pdfbox-2.0.31.jar,上链接:
https://dlcdn.apache.org/pdfbox/2.0.31/fontbox-2.0.31.jar
https://dlcdn.apache.org/pdfbox/2.0.31/pdfbox-2.0.31.jar

2、写代码

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public void PDFTextExtractor() throws UnsupportedEncodingException  {
		String invoiceNo = "";
		String invoiceDate = "";
		String invoiceUnit = "";
		
		// 设置传入的文件的编码
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		
		// 服务器目录
		String targetDirectory = ServletActionContext.getServletContext().getRealPath("/uploadExcel/某某公司.pdf");		
        try (
        	PDDocument document = PDDocument.load(new File(targetDirectory))) {
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            String text = pdfTextStripper.getText(document);
            
            // 使用正则表达式匹配开票单位
            Pattern patternUnit = Pattern.compile(".*司.*");
            Matcher matcherUnit = patternUnit.matcher(text);
            while (matcherUnit.find()) {
            	if(!matcherUnit.group().equals("某某有限公司")){
            		invoiceUnit = matcherUnit.group(0);
            	}
            }
            
            // 发票号码
            Pattern patternInvoiceNo = Pattern.compile("[\r\n][0-9]{20}[\r\n]");
            Matcher matcherInvoiceNo = patternInvoiceNo.matcher(text);
            // 开票时间
            Pattern patternDate = Pattern.compile("\\d{4}[年]\\d{2}[月]\\d{2}[日]");
            Matcher matcherDate = patternDate.matcher(text);
            while (matcherInvoiceNo.find() && matcherDate.find()) {
                invoiceNo = matcherInvoiceNo.group();
                invoiceDate = matcherDate.group();
                
            }
            
	        double maxTax = Double.MIN_VALUE; // 初始化最大税率为最小可能值
	        double minTax = Double.MAX_VALUE; // 初始化最小税率为最大可能值
            double maxAmount = Double.MIN_VALUE; // 初始化最大金额为最小可能值

	        // 税率
	        Pattern patternTax = Pattern.compile("(\\d+)%");
	        Matcher matcherTax = patternTax.matcher(text);
	        while (matcherTax.find()) {
	        	String taxStr = matcherTax.group(1); // 使用group(1)获取括号内的匹配部分
	            double tax = Double.parseDouble(taxStr);              
	            if (tax > maxTax) {
	            	maxTax = tax; // 更新最大税率
	            }
	            if (tax < minTax) {
	            	minTax = tax; // 更新最小税率
	            }
	        }
            
            // 开票金额
            Pattern patternInvoiceAmount = Pattern.compile("\\¥(\\d+\\.\\d+)");
            Matcher matcherInvoiceAmount = patternInvoiceAmount.matcher(text);
            while (matcherInvoiceAmount.find()) {
                String amountStr = matcherInvoiceAmount.group(1); // 使用group(1)获取括号内的匹配部分
                double amount = Double.parseDouble(amountStr);              
                if (amount > maxAmount) {
                	maxAmount = amount; // 更新最大金额
                }
            }
            
            if(maxTax == minTax){
            	if(!invoiceUnit.isEmpty() && !invoiceNo.isEmpty() && !invoiceDate.isEmpty() && maxTax != 0 && maxAmount != 0){
            		System.out.println("---------------------------------------------------");
                	System.out.println("开票单位: " + invoiceUnit);
                    System.out.println("发票号码: " + invoiceNo);
                    System.out.println("开票时间: " + invoiceDate);
                    System.out.println("最大税率: " + maxTax);
                    System.out.println("最小税率: " + minTax);
                    System.out.println("最大开票金额: " + maxAmount);
                    System.out.println("---------------------------------------------------");
            	}else{
            		map.put("success", false);
        			map.put("msg", "扫描失败,获取到异常的数据!");
            	}
            }else{
            	map.put("success", false);
    			map.put("msg", "扫描失败,获取到不同的税率!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值