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();
}
}