valerybokov opened a new pull request, #569:
URL: https://github.com/apache/pdfbox/pull/569
### 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]