当前位置:   article > 正文

调用Apache PDFBox库读取PDF数据_pdfbox读取pdf表格

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();
        }
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/987639?site
推荐阅读
相关标签
  

闽ICP备14008679号