zouyu
2025-11-27 eed98e551c817ead7965e08820d4b7adbc4a47f0
src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java
@@ -12,6 +12,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.util.filetypedetector.FileType;
import org.apache.poi.util.IOUtils;
import javax.swing.*;
@@ -281,6 +282,21 @@
        }
    }
    /**
     * 输出文件
     * @param newFileName 新文件名
     * @param file 源文件
     * @param fileSuffix 文件后缀
     * @param targetPath 目标路径
     */
    private void writeFile(String newFileName,File file,String fileSuffix,File targetPath) throws IOException {
        String outputFileName = newFileName + fileSuffix;
        File outputFile = new File(targetPath, outputFileName);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        IOUtils.copy(Files.newInputStream(file.toPath()),outputFile);
    }
    /**
     * 处理合同编号方法
@@ -297,6 +313,7 @@
        }
        log("开始处理文件...");
        fileIndex = new AtomicInteger(1);
        //识别到的合同编号列表
        final List<ContractNumberExcelData> contractNumberList = new CopyOnWriteArrayList<>();
@@ -307,37 +324,35 @@
            for (File file : selectedFiles) {
                processCount++;
                //获取识别到的第一个内容
                String text = file.getName().replace(".pdf","");
                try {
                    //截取pdf选区图像
                    String pathStr = capturePdfArea(file, prefs);
//                    ToFile.preprocessImage(pathStr);
                    //读取图像内容
                    String ocrFullText = FileNameValidator.validateAndCleanFileName(ocrService.ocr(pathStr.replaceFirst("/", "")));
                    //获取识别到的第一个内容
                    String text = file.getName().replace(".pdf","");
                    if(StringUtils.isNotBlank(ocrFullText) && !StringUtils.equals(ocrFullText,text)){
                        text = ocrFullText;
                        String finalText = text;
                        //如果合同编号重复,则在文件名后加一个序号
                        if(contractNumberList.stream().anyMatch(f -> f.getContractNumber().equals(finalText))){
                            text+="("+ fileIndex.get() +")";
                        String finalOcrFullText = ocrFullText;
                        if(contractNumberList.stream().anyMatch(f -> f.getContractNumber().equals(finalOcrFullText))){
                            ocrFullText+="("+ fileIndex.get() +")";
                            fileIndex.getAndIncrement();
                        }
                        //将识别的内容设置为文件名,导出到指定目录
                        String outputFileName = text + ".pdf";
                        File outputFile = new File(outputDirectory, outputFileName);
                        if (!outputFile.getParentFile().exists()) {
                            outputFile.getParentFile().mkdirs();
                        }
                        IOUtils.copy(Files.newInputStream(file.toPath()),outputFile);
                        writeFile(ocrFullText,file, ".pdf",outputDirectory);
                    }
                    successCount++;
                    contractNumberList.add(new ContractNumberExcelData(text));
                    contractNumberList.add(new ContractNumberExcelData(ocrFullText));
                    log("处理成功("+processCount+"/"+selectedFiles.size()+"): " + file.getName());
                } catch (Exception e) {
                    failCount++;
                    e.printStackTrace();
                    log("处理失败: " + file.getName() + " - " + e.getMessage());
                    //处理失败的文件也输出
                    try {
                        writeFile(text+"_fail", file, ".pdf",outputDirectory);
                    } catch (IOException ex) {
                    }
                }finally {
                    //删除临时目录
                    ToFile.deleteTempFiles(OcrSwingConstants.cacheDir);