Java에서 PDF의 양식 삭제

이 예에서는 필드 값만 변경하는 대신 페이지에서 양식 리소스를 제거합니다.

페이지에서 모든 양식 리소스 제거

선택한 페이지의 모든 양식 리소스를 한 번의 작업으로 제거해야 하는 경우 이 예를 사용합니다.

  1. 원본 PDF Document를 여세요.
  2. 대상 페이지의 XFormCollection에 접근하세요.
  3. 컬렉션을 지우고 업데이트된 문서를 저장하세요.
public static void removeAllForms(Path inputFile, int pageNum, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        XFormCollection forms = document.getPages().get_Item(pageNum).getResources().getForms();
        forms.clear();
        document.save(outputFile.toString());
    }
}

특정 양식 리소스 제거

Typewriter 양식과 같이 선택된 양식 리소스만 삭제해야 하는 경우 이 예를 사용합니다.

  1. 원본 PDF Document를 여세요.
  2. 대상 페이지의 XFormCollection에 접근하세요.
  3. 제거하려는 XForm 리소스를 필터링하고 컬렉션에서 삭제하세요.
  4. 업데이트된 PDF Document를 저장하세요.
public static void removeSpecifiedForm(Path inputFile, int pageNum, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        XFormCollection forms = document.getPages().get_Item(pageNum).getResources().getForms();
        List<String> formNames = new ArrayList<>();
        for (XForm form : forms) {
            if ("Typewriter".equals(form.getIT()) && "Form".equals(form.getSubtype())) {
                formNames.add(forms.getFormName(form));
            }
        }
        for (String formName : formNames) {
            forms.delete(formName);
        }
        document.save(outputFile.toString());
    }
}