I fill fields in a pdf with the following code.
private void populateFieldsWithData(Map<String, String> data, PDAcroForm acroForm, List<PDField> fields) throws IOException {
data.forEach((key, value) -> {
PDField field = acroForm.getField(key);
acroForm.getFieldTree().iterator().forEachRemaining(pdField -> {
if (pdField.getFullyQualifiedName().equals(key)) {
try {
field.setValue(value);
field.setReadOnly(true);
fields.add(pdField);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
});
acroForm.flatten();
}
The problem is that the text in the generated PDF is squashed/compressed.
I tried removing acroForm.flatten();
. If I do and I click on the text in the generated PDF the text is adjusted and looks normal. But when I click elsewhere in the generated PDF the text goes back to beeing squashed/compressed.