나는 3 개의 레이블 (이름, 성 및 사진)으로 간단한 PDF 문서를 만들었습니다. 그런 다음 Adobe Acrobat PRO DC를 사용하여 2 개의 '텍스트 필드'와 하나의 '이미지 필드'가있는 AcroForm 레이어를 추가했습니다. 나는 일반 Acrobat Reader가이 PDF 파일을 열고 이름, 성을 입력하여 채우고 사진을 삽입하기 위해 할 수있는 양식을 채우려면Java PDFBox를 사용하여 프로그래밍 방식으로 AcroForm 필드에 이미지를 삽입하는 방법은 무엇입니까?
그래서 이미지 자리 표시를 클릭 열린 대화 상자에서 사진을 선택하십시오.
하지만 프로그래밍 같은 일을 할 수 있습니까? Apache PDFBox 라이브러리 (버전 2.0.7)를 사용하여 양식 필드를 찾고 값을 삽입하는 간단한 Java 응용 프로그램을 만들었습니다.
난 쉽게 텍스트 편집 필드를 채울 수 있지만 이미지 삽입 할 수있는 방법을 알아낼 수 없습니다 : 나는 이상한 것은 구별 한
public class AcroFormPopulator {
public static void main(String[] args) {
AcroFormPopulator abd = new AcroFormPopulator();
try {
abd.populateAndCopy("test.pdf", "generated.pdf");
} catch (IOException e) {
e.printStackTrace();
}
}
private void populateAndCopy(String originalPdf, String targetPdf) throws IOException {
File file = new File(originalPdf);
PDDocument document = PDDocument.load(file);
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
Map<String, String> data = new HashMap<>();
data.put("firstName", "Mike");
data.put("lastName", "Taylor");
data.put("photo_af_image", "photo.jpeg");
for (Map.Entry<String, String> item : data.entrySet()) {
PDField field = acroForm.getField(item.getKey());
if (field != null) {
if (field instanceof PDTextField) {
field.setValue(item.getValue());
} else if (field instanceof PDPushButton) {
File imageFile = new File(item.getValue());
PDPushButton pdPushButton = (PDPushButton) field;
// do not see way to isert image
} else {
System.err.println("No field found with name:" + item.getKey());
}
} else {
System.err.println("No field found with name:" + item.getKey());
}
}
document.save(targetPdf);
document.close();
System.out.println("Populated!");
}
}
- 그것은 내가 이미지 필드를 추가 있다고 아크로뱃 프로 DC에서, 그러나 유일한 필드는 생성 된 이름으로 얻을 : 'photo_af_image'는 button 유형입니다 - PDPushButton (그 이유는 (instanceof PDPushButton)),하지만 Image와는 아무 관련이 없습니다.
Acrobat Pro DC로 만든 상자의 크기에 맞게 이미지를 AcroForm 'photo_af_image'필드에 어떻게 삽입합니까?