Search and replace strings in PDF


I am looking at PDF tools and in particular I am looking for a tool that can search and replace text in a pdf. I think PDFNet can do it but before I get to far I would like to make sure. I am on a very short timeline.

Can PDFNet search and replace strings in a pdf?

Do you have a code example of doing this?

Which versions do I need at a minimum?

Any help would be of great benefit.


If you are working with PDF templates, you would use the ContentReplacer like this:

ContentReplacer replacer = new ContentReplacer();
replacer.AddString(“NAME”,“John Smith”);

This would replace any instances of the template string “[NAME]” with “John Smith”

Please see the ContentReplacerTest sample in the trial package.

Alternatively, you can use TextSearch class to find the rectangle region of the text to replace.

Then use ContentReplacer to replace all existing text in that rectangle with your new text.

Please see the TextSearchTest sample for this.

ContentReplacer itself is a utility class that is using ElementWriter/ElementBuilder as shown in the ElementEdit sample:

ContentReplacer was added in PDFNet v.5.8, ElementWriter/ElementBuilder were part of the SDK for a long time.

If the PDF’s that you’re working with does not contain the template text with [ ]…

You would have to find the coordinates of the text area.

With TextSearch, you can use the Highlights information to get the quads coordinates, then calculating the corresponding Rect object.

You can find an example of this in our TextSearchTest project.

After you have determined where the coordinates of the text area, use the Context replacer as such:

replacer.AddText(new Rect(x1, y1, x2, y2), "New text to replace");

Also note that the Rect region must be large enough to contain the text to be replaced.

For more ways to implement search and replace using TextExtractor and ElementEdit, see the following discussion: