Search and replace strings in PDF

Q:

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.

A:

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

ContentReplacer replacer = new ContentReplacer();
replacer.AddString(“NAME”,“John Smith”);
replacer.Process(doc.GetPage(1));

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:

http://www.pdftron.com/pdfnet/samplecode.html#ElementEdit

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:

http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/87390348efe7e5ba/47bda8b4bc644d06?lnk=gst&q=TextSearch+rect#47bda8b4bc644d06

http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/db2f2ce5a7a1abe7/e7afb9d5af2729c7?lnk=gst&q=search+and+replace+text#e7afb9d5af2729c7