Page 1 of 1

Search and Replace in Comments

Posted: Wed Apr 23, 2014 2:13 pm
by joepierson
Is there a way to search and replace words in multiple comments? I need to replace a word in about 200 comments. Maybe javascript?
thanks!

Re: Search and Replace in Comments

Posted: Wed Apr 23, 2014 3:38 pm
by Tracker Supp-Stefan
Hello joepierson,

Please modify the string1 and string2 in the code below to the values you need (string1 is the string to be replaced and 2 is the new one you want) and it should work. Please have in mind that this will change the text of typewriter/text box annotations only.

Code: Select all

var annots = this.getAnnots();
annots.length;
for (i = 0; i < annots.length; i++)
{
  if (annots[i].type == "FreeText")
  {
    var str = annots[i].contents;
    var res = str.replace("string1","2String");
    if (str != res) 
    {
      annots[i].contents = res;
    }
  }
}

Re: Search and Replace in Comments

Posted: Wed Apr 23, 2014 7:53 pm
by joepierson
thanks! That worked great except it changed the text font size to 12, is that some default size that I can alter?

Re: Search and Replace in Comments

Posted: Wed Apr 23, 2014 8:28 pm
by joepierson
More info, when I exported the comments and reviewed the objects the /Contents line item was correct but the /RC line item was still the old text.

Re: Search and Replace in Comments

Posted: Wed Apr 23, 2014 11:42 pm
by Ivan - Tracker Software
You can use the following script which preserves text formatting:

Code: Select all

var annots = this.getAnnots();
annots.length;
for (i = 0; i < annots.length; i++)
{
  if (annots[i].type == "FreeText")
  {
    var rc = annots[i].richContents;
    if (rc.length > 0)
    {
      var bModified = false;
      for (var j = 0; j < rc.length; j++)
      {
        var str = rc[j].text.replace("string1", "2String");
        if (rc[j].text != str)
        {
          rc[j].text = str;
          bModified = true;
        }
      }
      if (bModified)
        annots[i].richContents = rc;
    }
    else
    {
      var str = annots[i].contents;
      var res = str.replace("string1","2String");
      if (str != res)
      {
        annots[i].contents = res;
      }
    }
  }
}
Please note, it will not replace the text ("string1") if it consists from more than one style.
For example, if "string1" is partly bold, and partly normal (like string1).

Re: Search and Replace in Comments

Posted: Tue Apr 29, 2014 3:05 pm
by joepierson
Thanks! That worked great.

Another question, is it possible (theoretically) to write a java script that extracts the pdf text contained inside a comment text box and transfer the pdf text to the comment box?

I work with schematics (pdf's) and would like to draw text boxes around circuitry, and then have all the parts in the circuit appear in the text box.

Re: Search and Replace in Comments

Posted: Tue Apr 29, 2014 3:30 pm
by Tracker Supp-Stefan
Hello joepierson,

Have you seen the Edit -> Preferences -> Commenting window and the
Comments Creating Options -> Copy Encircled text into Drawing comment pop-ups checkbox?
Is this what you are after?

Regards,
Stefan

Re: Search and Replace in Comments

Posted: Wed Apr 30, 2014 2:38 pm
by joepierson
No I wasn't aware of that, that is very close to what I need. What I would like to do is parse the encircled text (for very specific text, namely the part numbers in the schematic, and ignore the other text in the encircled area) in javascript and insert it into a typewriter box.

Re: Search and Replace in Comments

Posted: Wed Apr 30, 2014 2:48 pm
by Tracker Supp-Stefan
Hi Joe,

It should be possible to write a JS that will go through all the text in your document - and if it finds the one you need - create a typewriter annotation out of it. Though I don't think you can write a JS that will do something like the "Copy Encircled text" but then apply extra logic to it and remove unnecessary parts of the encircled text.

Regards,
Stefan

Re: Search and Replace in Comments

Posted: Fri Sep 03, 2021 2:50 pm
by Lazi
Hi!
The need what I have is the same or very similar to subject of this thread. I'm looking for such feature like we have in the MS notepad or MS Word called replace (CTRL + H) only for comments. Important for me is to have highlighted found text to see what I'm changing. That's why script probably is not for me :-(. Is there any function like this?

Re: Search and Replace in Comments

Posted: Fri Sep 03, 2021 3:56 pm
by Sean - Tracker
Hi Lazi,

I'm afraid there is no default feature to replace comment text. However, you can specify in the Search Tool to find only text in comments, as detailed on this page of the manual:

https://help.pdf-xchange.com/pdfxe ... _ed_2.html

Note that in the Options dropdown menu, you can select only "Include Comments" to search for only the text of comments in documents:

image.png

This will enable you to locate the text - but there is no way to automatically update it.

Hope this help and best regards,

Re: Search and Replace in Comments  SOLVED

Posted: Fri Sep 03, 2021 4:33 pm
by Lazi
Thank you for quick answer.
So, I have to do workaround by exporting all comments and edit them in the notepad :roll:

Re: Search and Replace in Comments

Posted: Fri Oct 22, 2021 5:43 pm
by Chris - Tracker Supp
:)