Radio buttons and checkboxes are not correctly rendered in Adobe Acrobat after form flattening
Description
We are experiencing an issue when flattening PDF forms using OpenPDF 2.0.3 (same problem with 3.x, but we have to stick to java 17).
After filling in an AcroForm PDF and enabling form flattening with PdfStamper.setFormFlattening(true), radio buttons and checkboxes are not correctly rendered in Adobe Acrobat.
In particular, selected radio buttons appear as unselected when the generated PDF is opened in Adobe Acrobat. We are experiencing the same issue with checkboxes.
Interestingly, the generated PDF is displayed correctly in web browsers (such as Chrome or Firefox). The issue appears to be specific to Adobe Acrobat.
Additionally, Adobe Acrobat displays an error when opening the generated PDF.
Attached files
We will provide two PDF documents to help reproduce and investigate the issue:
- Original PDF form – the source PDF containing the AcroForm fields, including radio buttons and checkboxes.
- Generated PDF – the output produced by OpenPDF after filling in the fields and applying form flattening.
test_flattening_form.pdf test_flattening_flat.pdf
Steps to reproduce
- Load the original PDF form using
PdfReader. - Create a
PdfStamper. - Enable form flattening using
setFormFlattening(true). - Fill in the form fields using
AcroFields.setField(...). - Save the generated PDF.
- Open the resulting PDF in Adobe Acrobat.
Expected behavior
- The generated PDF should open without errors in Adobe Acrobat.
- Selected radio buttons should remain visibly selected.
- Checked checkboxes should remain visibly checked.
- The flattened form should have consistent rendering across PDF viewers, including Adobe Acrobat and web browsers.
Actual behavior
- Adobe Acrobat displays an error when opening the generated PDF.
- Radio buttons that were selected before flattening appear unselected.
- Checkboxes exhibit the same rendering issue.
- The generated PDF appears to render correctly in web browsers.
Code used
The following is a simplified version of the code used to generate the PDF:
public byte[] merge(
final Map<String, String> values,
final byte[] cerfa,
final boolean readonly) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (
final PdfReader pdfReader = new PdfReader(cerfa);
final PdfStamper pdfStamper =
new PdfStamper(pdfReader, outputStream)
) {
if (readonly) {
pdfStamper.setFormFlattening(true);
}
final AcroFields form = pdfStamper.getAcroFields();
final Set<String> unsupportedFields = new HashSet<>();
for (final Map.Entry<String, Item> entry :
form.getAllFields().entrySet()) {
final String field = entry.getKey();
if (!values.containsKey(field)) {
unsupportedFields.add(field);
continue;
}
final String value = values.get(field);
if (value != null && !form.setField(field, value)) {
LOGGER.warn(
"Value '{}' could not be merged into field '{}'",
value,
field
);
}
}
if (!unsupportedFields.isEmpty() && LOGGER.isInfoEnabled()) {
LOGGER.info(
"Unsupported field(s) in CERFA mapping: '{}'",
String.join(", ", unsupportedFields)
);
}
} catch (final Exception e) {
throw new PresaisieException(
"Error while generating the CERFA PDF",
e
);
}
return outputStream.toByteArray();
}
Environment
- Library: OpenPDF 2.0.3
- PDF processing API:
PdfReader,PdfStamper,AcroFields - PDF viewer affected: Adobe Acrobat
- PDF viewers where rendering appears correct: Web browsers (Chrome / Firefox)
Thank you for your help!
I have submitted a fix for this issue in PR #1800.
The fix cleans up stale widget /OBJR references from the structure tree and removes the corresponding /StructParent entries from the ParentTree when form fields are flattened.
I have also added a regression test using the provided PDF containing radio buttons and checkboxes.
Validation completed:
mvn -pl openpdf-core -Dtest=PdfStamperImpTest test— passedmvn -pl openpdf-core test— 2,088 tests passed, 0 failures, 3 skipped.
PR: #1800
Thank you for reporting this issue!