Skip to content

PDFBOX-6311: Fix nested marked content in actual text - #569

Closed
valerybokov wants to merge 2 commits into
apache:trunkfrom
valerybokov:fix-nested-marked-content-in-ActualText
Closed

valerybokov wants to merge 2 commits into
apache:trunkfrom
valerybokov:fix-nested-marked-content-in-ActualText

Conversation

@valerybokov

Copy link
Copy Markdown

Problem

PDFTextStripper loses or leaks text when a marked-content sequence is nested in a sequence that has
/ActualText:

  1. A nested sequence without /ActualText ends the replacement too early:

    /Span <</ActualText (XYZ)>> BDC  /Foo BMC (abc) Tj EMC  (def) Tj  EMC
    

    Expected XYZ, but abc def is extracted (the ActualText is lost).

  2. A nested sequence with /ActualText ends the outer replacement when it ends:

    /Span <</ActualText (X)>> BDC  (a) Tj  /Span <</ActualText (Y)>> BDC (b) Tj EMC  (tail) Tj  EMC
    

    X Y tail is extracted: text that the outer ActualText replaces leaks into the output.

Cause

currentMarkedContents is a stack, so that "another BDC within /ActualText... BDC block" doesn't
cause confusion, but actualText is a single field:

  • beginMarkedContentSequence() overwrites it with the nested sequence's ActualText, which is null when
    the nested sequence doesn't have one.
  • endMarkedContentSequence() sets it to null when any sequence with ActualText ends, including a
    nested one.

Fix

  • A new field, actualTextMarkedContent, remembers the marked content that set actualText.
  • beginMarkedContentSequence() takes the ActualText only if no replacement is active. The outermost
    /ActualText wins, because it replaces all of its content, nested content included
    (ISO 32000-2, 14.9.4).
  • endMarkedContentSequence() resets actualText only when that same marked content ends.

Note: for nested /ActualText, the inner one is now ignored, so example 2 extracts X.

Tests

TestTextStripper#testActualTextWithNestedMarkedContent and TestTextStripper#testNestedActualText cover
the two examples above. Both fail without the fix. TestTextStripper, BidiTest and
PDFTextStripperByAreaTest pass.


Per https://www.apache.org/legal/generative-tooling.html: portions of this PR were
produced with assistance from Claude Code (Anthropic), based on a bug was described in Apache PDFBOX Issue Tracker.

I've reviewed the generated code and confirm to the best of my knowledge that the output does not include any
third-party copyrighted material and is compatible with the Apache License 2.0.

valerybokov and others added 2 commits November 17, 2026 23:15
…ted in an ActualText sequence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Text sequence

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@valerybokov

Copy link
Copy Markdown
Author

Other issues. These are not execution errors, but it’s unclear whether this was done intentionally or by accident.

1 PDShadingType6 is public. It has a generatePatch method. But the class Patch is package private
2 the same for PDShadingType7
3 PlainText is public. It has a getLines method. But the class Line is package private.

@THausherr

THausherr commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Re "other issues", I fixed the first one everywhere thanks. The third one I can't change, there is a usage and it's for the size of the list. So the actual type doesn't matter. This was package private in the past but there were two versions of the Plaintext class, now there is only one.
Re marked content, please create a ticket in JIRA. From your description this is a bug and not just a code style issue. Thank you for finding this.

@valerybokov valerybokov changed the title Fix nested marked content in actual text [PDFBOX-6311] Fix nested marked content in actual text Nov 18, 2026
@valerybokov valerybokov changed the title [PDFBOX-6311] Fix nested marked content in actual text PDFBOX-6311: Fix nested marked content in actual text Nov 18, 2026
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.

2 participants