Hi,
I am moving from iText to PDFBox to fill out a form in a PDF document.
This is mainly my code:
Resource contractResource =
ctx.getResource("classpath:/META-INF/pdfs/contract.pdf");
PDDocument doc = PDDocument.load(contractResource.getInputStream());
PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm form = docCatalog.getAcroForm();
setField(doc, "field1", field1Value);
setField(doc, "field2", field2Value);
setField(doc, "field3", field3Value);
response.setStatus(200);
response.setContentType("application/pdf");
doc.save(response.getOutputStream());
And the setField method populates the fields:
private void setField(PDDocument doc, String fieldName, String value) throws
IOException {
PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm form = docCatalog.getAcroForm();
PDField field = form.getField(fieldName);
if (field != null) {
if (!Strings.isNullOrEmpty(value)) {
field.setValue(value);
} else {
LOG.debug("Value for {} is empty", fieldName);
}
} else {
LOG.debug("Field {} not found", fieldName);
}
}
Unfortunately, I get a NPE as soon as I try to set the field.
java.lang.NullPointerException
at
org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.calculateFontSize(PDAppearance.java:558)
Any tips?
brgds,
Papick