How do I redact characters

I’m evaluating PDFTron for use in a redaction application.

I’m trying to remove a bunch of characters from text based on the text selection without moving the surrounding characters.

Using the sample code you mentioned earlier I’ve implemented program that does the following

Go through the elements in the pages. If they intersect the selection I use the character iterator and get the glyph bounding box for each character.

For each one I make a new text element with a text matrix adjustment that corresponds to its place on the line.

I try to save and restore graphic state and set a final text position.

This often works on some simple text, but many times, later lines move up and on other pdf it doesn’t work at all.

I’m enclosing a code snippet.

Am I missing some state that needs to be saved and restored when replacing the text element with a group?

Is there a better way than fiddling with the text matrix?

Do I have to know about the underlying text show operator and do different things for each operator?

Finally for complex fonts; should I be walking through the character codes as I do know, and setting them in new runs or should I be walking through the glyphs for each character and setting them as data. Does setting one generate the other.

The relevant code is in the Excise function in the enclosed file.

Thanks for any help.

MyPDFView.cs (30 KB)

I was able to find several versions of the 'character editing' sample
code in the forum. The older posts (e.g.
contained some errors.
As a starting point you may want to take a look at 'TurnTextBlue'
method in (located in the 'Files' section of this
forum; please note that this sample is not part of the SDK
In case you encounter a file where text recoloring does not work you
can send a test file (and/or sample project) to
and we will take a look into it. Since you are deleting some text
characters (instead of modifying their properties) I wonder if the
problem is caused because some character elements are skipped. On the
second thought, it seems that there is a bug in your code related to
partial overlap case… You are walking through a text run using
CharIterator and outputting characters, but are then writing another
text run after the loop? Also you can’t use q/Q (CreateGroupBegin()/
End()) within a text object. For elements that need to be skipped you
could output empty text run or perhaps a space.
Please let me know if this helps.
On a separate note, since you are implementing a custom tool in
PDFView class it is a good idea to Lock() the access to the document
whenever you read or write to the document. Because PDF rendering
thread runs in a separate thread from the GUI thread, it is important
to serialize the access to the document.