We are using TextFragmentAbsorber
to find and redact text in a PDF. The redactions are marked with square brackets like [Reason]
, and the logic does the following:
- Finds all fragments matching regex:
\[(?s)(.*?)\]
- Updates the
Text
property to remove[
]
and newline characters\r\n
- Applies
TextState.ForegroundColor
andTextState.BackgroundColor
(e.g., white text on black background)
However, after updating textFragment.Text
, the rendered output in the final PDF shows inconsistent coloring — e.g., only partial background color applied, or text overlaps (see attached screenshots).
Observed Behavior
- Background color is only partially applied (e.g., half black, half white areas in same fragment)
- Some fragments lose proper layout/alignment after
.Text
is reassigned - Issue is worse when the original
[Reason]
includes a line break (\r\n
)
We noticed that adding the following line improves formatting in some cases:
csharp
CopyEdit
textFragment.TextState.FontSize = textFragment.TextState.FontSize;
It seems to trigger a layout recalculation, but it doesn’t resolve the issue for all fragments — especially longer or multi-line ones.
Attachments
- Original PDF before redaction (optional)
- Output PDF showing issue
- Screenshot: incorrect formatting (attached)
- Sample code block (see below)
Code Snippet
csharp
CopyEdit
var searchTerm = "\\[(?s)(.*?)\\]";
TextFragmentAbsorber absorber = new TextFragmentAbsorber(searchTerm);
absorber.TextSearchOptions = new TextSearchOptions(true);
doc.Pages.Accept(absorber);
var fragments = absorber.TextFragments;
foreach (var fragment in fragments)
{
string text = fragment.Text;
StringBuilder sb = new StringBuilder(text.Length);
foreach (char c in text)
{
if (c != '\r' && c != '\n' && c != '[' && c != ']')
sb.Append(c);
}
fragment.Text = sb.ToString();
fragment.TextState.FontSize = fragment.TextState.FontSize; // partial fix
fragment.TextState.ForegroundColor = Color.White;
fragment.TextState.BackgroundColor = Color.Black;
}
Request for Support
- Is there a recommended way to force proper re-rendering after modifying
TextFragment.Text
? - Is there any known issue when replacing text containing line breaks and applying formatting?
- Can this be fixed in a future release, or is there an internal method to force layout recalculation?
attachments for Pdf.zip (9.4 MB)