Java에서 PDF의 첨부 파일 제거

PDF 문서에 저장된 첨부 파일은 EmbeddedFiles 컬렉션을 통해 개별적으로 제거하거나 한꺼번에 제거할 수 있습니다.

단일 첨부 파일 제거

PDF에서 이름이 포함된 파일 하나를 삭제해야 하는 경우 이 예를 사용하세요.

  1. 원본 PDF Document를 여세요.
  2. 포함된 파일 컬렉션에서 해당 키로 첨부 파일을 삭제하세요.
  3. 업데이트된 출력 문서를 저장하세요.
public static void removeAttachment(Path inputFile, String attachmentName, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getEmbeddedFiles().deleteByKey(attachmentName);
        document.save(outputFile.toString());
    }
}

모든 첨부 파일 제거

전체 포함된 파일 컬렉션을 지워야 하는 경우 이 접근 방식을 사용합니다.

  1. 원본 PDF Document를 여세요.
  2. 포함된 파일 컬렉션에서 모든 항목을 삭제하세요.
  3. 정리된 출력 문서를 저장하세요.
public static void removeAllAttachments(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getEmbeddedFiles().delete();
        document.save(outputFile.toString());
    }
}