Skip to content

Fix structure tree cleanup when flattening form fields - #1800

Merged
andreasrosdalw merged 5 commits into
LibrePDF:masterfrom
yashdotdev13:fix/1673-form-flattening-checkboxes
Dec 3, 2026
Merged

andreasrosdalw merged 5 commits into
LibrePDF:masterfrom
yashdotdev13:fix/1673-form-flattening-checkboxes

Conversation

@yashdotdev13

Copy link
Copy Markdown
Contributor

Description

Fixes [#1673](#1673): Radio buttons and checkboxes are not correctly rendered in Adobe Acrobat after form flattening.

Previously, flattening AcroForm fields removed widget annotations but left stale /OBJR references in the structure tree and corresponding /StructParent entries in the ParentTree.

This change:

  • Tracks widget references and their /StructParent keys during form flattening.
  • Removes references to flattened widgets from the structure tree.
  • Rebuilds the ParentTree without entries belonging to flattened widgets.
  • Adds a regression test using a PDF containing radio buttons and checkboxes.

Testing

  • mvn -pl openpdf-core -Dtest=PdfStamperImpTest test
  • mvn -pl openpdf-core test

Both passed successfully.

@codacy-production

codacy-production Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 21 complexity · 0 duplication

Metric Results
Complexity 21
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@andreasrosdal

Copy link
Copy Markdown
Contributor

Thanks for working on #1673! All builds, CodeQL and CodeFactor pass, but two quality gates fail: Codacy Static Code Analysis and SonarCloud Code Analysis.

Codacy: 2 new issues (0 allowed), both in PdfStamperImpTest.java

  1. Line 37, PMD MethodNamingConventions: JUnit 5 method names must match [a-z][a-zA-Z0-9]*, so underscores are not allowed. Rename the method, for example:
    flatteningFormWithRadioButtonsAndCheckboxes_removesWidgetStructureReferences → flatteningFormWithRadioButtonsAndCheckboxesRemovesWidgetStructureReferences
  2. Line 87, PMD AvoidReassigningParameters: object = PdfReader.getPdfObject(object); in countObjectReferences reassigns a parameter. Use a local variable instead:
    PdfObject direct = PdfReader.getPdfObject(object);
    if (direct instanceof PdfArray array) { ... }
    if (direct instanceof PdfDictionary dictionary) { ... }

SonarCloud: Quality Gate failed (Reliability Rating C on new code, A required)

Code smells in PdfStamperImp.java that you added:

  • L938 (S6201/S1905): use pattern matching instead of the cast:
    if (widgetRef instanceof PRIndirectReference prRef) { flattenedWidgetRefs.add(prRef.getNumber()); }
  • L1252, L1260, L1266 (S6201): in removeWidgetObjectReferences / isFlattenedWidgetObjectReference, replace instanceof X + ((X) obj) with instanceof X x (for example directKids instanceof PdfArray kidsArray && kidsArray.isEmpty(), object instanceof PdfDictionary dict, referencedObject instanceof PRIndirectReference ref).
  • L1221 (S3776, critical): the cognitive complexity of removeWidgetObjectReferences is 19, and 15 is allowed. Split it, for example into removeFromArray(PdfArray, Set<Integer>) and removeFromDictionary(PdfDictionary, Set<Integer>).

The Reliability C rating comes from 8 "possible NullPointerException" bugs (javabugs:S2259) that Sonar reports in PdfStamperImp.java at L483, L1276, L1850, L1889, L1891, L1905, L1907 and L2002. This PR does not change those lines. They are dereferences of reader.getPageSizeWithRotation(..), reader.getPageN(..) and PdfReader.getPdfObject(acro), and Sonar reports them here because the file was modified. To make the gate pass, add null checks before those dereferences. For example, if (pg == null) throw new IllegalArgumentException("Invalid page number " + page);, and in eliminateAcroformObjects return early when acrodic == null. If you would rather not touch unrelated code, say so here and a maintainer can look at these findings in SonarCloud.

The details are in the Codacy report and the SonarCloud report. Please push the fixes to the same branch.

@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@yashdotdev13

yashdotdev13 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @andreasrosdal, Addressed the review feedback in the latest commit.

  • Updated the instanceof checks to use pattern matching.
  • Refactored the recursive structure-tree cleanup logic into smaller helper methods to reduce cognitive complexity.
  • Updated the regression test naming and helper implementation to follow the project conventions.

Verified with mvn -pl openpdf-core -Dtest=PdfStamperImpTest test — all 3 tests pass.

Thanks for the review!

@andreasrosdalw
andreasrosdalw merged commit 4ff970c into LibrePDF:master Dec 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants