Search and Replace in Comments  SOLVED

The PDF-XChange Viewer for End Users
+++ FREE +++

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
joepierson
User
Posts: 5
Joined: Wed Apr 23, 2014 1:50 pm

Search and Replace in Comments

Post 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!
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17765
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Search and Replace in Comments

Post 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;
    }
  }
}
joepierson
User
Posts: 5
Joined: Wed Apr 23, 2014 1:50 pm

Re: Search and Replace in Comments

Post by joepierson »

thanks! That worked great except it changed the text font size to 12, is that some default size that I can alter?
joepierson
User
Posts: 5
Joined: Wed Apr 23, 2014 1:50 pm

Re: Search and Replace in Comments

Post 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.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Search and Replace in Comments

Post 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).
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
joepierson
User
Posts: 5
Joined: Wed Apr 23, 2014 1:50 pm

Re: Search and Replace in Comments

Post 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.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17765
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Search and Replace in Comments

Post 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
joepierson
User
Posts: 5
Joined: Wed Apr 23, 2014 1:50 pm

Re: Search and Replace in Comments

Post 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.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17765
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Search and Replace in Comments

Post 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
Lazi
User
Posts: 2
Joined: Fri Sep 03, 2021 1:48 pm

Re: Search and Replace in Comments

Post 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?
User avatar
Sean - Tracker
Site Admin
Posts: 175
Joined: Wed Sep 14, 2016 5:42 pm
Location: British Columbia

Re: Search and Replace in Comments

Post 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,
Sean Godley
Technical Writer
Tracker Software Products (Canada) LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623
Lazi
User
Posts: 2
Joined: Fri Sep 03, 2021 1:48 pm

Re: Search and Replace in Comments  SOLVED

Post by Lazi »

Thank you for quick answer.
So, I have to do workaround by exporting all comments and edit them in the notepad :roll:
User avatar
Chris - Tracker Supp
Site Admin
Posts: 795
Joined: Tue Apr 14, 2009 11:33 pm

Re: Search and Replace in Comments

Post by Chris - Tracker Supp »

:)
If posting files to this forum - you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded - thank you.


Chris Attrell
Tracker Sales & Support North America
http://www.tracker-software.com
Post Reply