You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iText5合并PDF后无法删除原文件,寻求解决方法

iText5合并PDF后无法删除原文件,调用reader.close()仍失败

使用iText5进行PDF合并操作,合并完成后尝试删除原PDF文件,即使调用了reader.close()方法,仍无法删除原文件,执行后始终显示DELETE PDF NOK。以下是我的代码:

public static void main(String[] args) {
    System.out.println("CONCAT PDF");
    pdfConcat2("C:\\Users\\File1.pdf",
            "C:\\Users\\File2.pdf",
            "C:\\Users\\File_FINAL.pdf");
    System.out.println("-------------ACTION DELETE");
    if (FileTools.deleteFile("C:\\Users\\File1.pdf")) {
        System.out.println("DELETE PDF OK");
    } else {
        System.out.println("DELETE PDF NOK");
    }
    if (FileTools.deleteFile("C:\\Users\\File2.pdf")) {
        System.out.println("DELETE PDF OK");
    } else {
        System.out.println("DELETE PDF NOK");
    }
}

public static void pdfConcat2(String pathFile1, String pathFile2, String destinationPDF) {
    Document document = null;
    FileOutputStream outputStream = null;
    PdfCopy copy = null;
    PdfReader reader = null;

    try {
        document = new Document();
        outputStream = new FileOutputStream(destinationPDF);
        copy = new PdfSmartCopy(document, outputStream);
        document.open();
        
        reader = new PdfReader(pathFile1);
        copy.addDocument(reader);
        copy.freeReader(reader);
        reader.close();
        
        reader = new PdfReader(pathFile2);
        copy.addDocument(reader);
        copy.freeReader(reader);
        reader.close();
        
        outputStream.flush();
        document.close();
        outputStream.close();
        copy.flush();
        copy.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

解决思路

  • 修正资源关闭顺序:iText中Document.close()会自动触发关联PdfCopy及输出流的关闭操作,手动重复关闭可能导致资源释放不彻底。应确保PdfReader先关闭,再由Document.close()处理剩余资源。
  • 使用try-with-resources自动管理:Java 7+的try-with-resources可自动关闭所有实现AutoCloseable的资源,避免手动关闭的顺序错误或遗漏,确保文件句柄完全释放。
  • 排查删除工具类:FileTools.deleteFile可能存在逻辑缺陷(如未处理权限、未检测文件占用),可先用Java原生API(new File(path).delete()或Files.delete(Paths.get(path)))测试删除,排除工具类问题。
  • 彻底释放PdfReader资源:调用copy.freeReader(reader)后,可将reader置为null,辅助GC回收资源,避免残留引用占用文件句柄。

修正后的代码示例(使用try-with-resources)

public static void main(String[] args) {
    System.out.println("CONCAT PDF");
    pdfConcat2("C:\\Users\\File1.pdf",
            "C:\\Users\\File2.pdf",
            "C:\\Users\\File_FINAL.pdf");
    System.out.println("-------------ACTION DELETE");
    
    // 改用原生API测试删除
    File file1 = new File("C:\\Users\\File1.pdf");
    System.out.println(file1.delete() ? "DELETE PDF OK" : "DELETE PDF NOK");
    
    File file2 = new File("C:\\Users\\File2.pdf");
    System.out.println(file2.delete() ? "DELETE PDF OK" : "DELETE PDF NOK");
}

public static void pdfConcat2(String pathFile1, String pathFile2, String destinationPDF) {
    try (Document document = new Document();
         FileOutputStream outputStream = new FileOutputStream(destinationPDF);
         PdfCopy copy = new PdfSmartCopy(document, outputStream)) {
        
        document.open();
        
        // 处理第一个PDF,自动关闭reader
        try (PdfReader reader = new PdfReader(pathFile1)) {
            copy.addDocument(reader);
            copy.freeReader(reader);
        }
        
        // 处理第二个PDF,自动关闭reader
        try (PdfReader reader = new PdfReader(pathFile2)) {
            copy.addDocument(reader);
            copy.freeReader(reader);
        }
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    }
}

内容的提问来源于stack exchange,提问作者Titi Titi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:48:19