Search and Replace author and subject within notes

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

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

francois maurice
User
Posts: 102
Joined: Sat Sep 29, 2012 5:38 am

Search and Replace author and subject within notes

Post by francois maurice »

Hi,

I'd like to be able to change automatically the author or subject of some hundred notes!! Is it possible to search and replace author and subject within notes ?

Thanks
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3603
Joined: Thu Jul 08, 2004 10:36 pm

Re: Search and Replace author and subject within notes

Post by Ivan - Tracker Software »

Yes, it is possible, but you have to use a JavaScript script.

Here you can find a sample, and modify it for your purpose:

Code: Select all

function ReplaceAuthor(oldAuthor, newAuthor)
{
	this.syncAnnotScan();
	for (var i = 0; i < this.numPages; i++)
	{
		var annots = this.getAnnots(i);
		for (var j = 0; j < annots.length; j++)
		{
			if (annots[j].author == oldAuthor)
				annots[j].author = newAuthor;
		}
	}
}

function ReplaceSubject(oldSubject, newSubject)
{
	this.syncAnnotScan();
	for (var i = 0; i < this.numPages; i++)
	{
		var annots = this.getAnnots(i);
		for (var j = 0; j < annots.length; j++)
		{
			if (annots[j].subject == oldSubject)
				annots[j].subject = newSubject;
		}
	}
}

//
ReplaceAuthor("old", "new");
ReplaceSubject("oldSubj", "Subj");
You can open JS console (use Ctlr+J shortcut) and run this script there.

HTH
PDF-XChange Co Ltd. (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.
francois maurice
User
Posts: 102
Joined: Sat Sep 29, 2012 5:38 am

Re: Search and Replace author and subject within notes

Post by francois maurice »

Thanks for the tip.

The script works well with 'Notes' or 'Comments', but in fact I'm using 'Highlighted Text' (I'm not in English since I'm using the french interface) by clicking on the 'blue T on yellow' icon.

Surely it has something to do with 'GetAnnots' method ! Can you tell me which method to use with 'Highlighted Text' ?

Thanks a lot !
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3603
Joined: Thu Jul 08, 2004 10:36 pm

Re: Search and Replace author and subject within notes

Post by Ivan - Tracker Software »

Highlight Text is an annotation too and should works ok.
Can you send me you document by email for checking? (please use ivan@tracker-software.com)
PDF-XChange Co Ltd. (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.
francois maurice
User
Posts: 102
Joined: Sat Sep 29, 2012 5:38 am

Re: Search and Replace author and subject within notes

Post by francois maurice »

Thanks Ivan for the JS code. It works well and you save me many hours of boring job.

For those who are interested, this is the new code from Ivan Poberezhnyk :

Code: Select all

function ReplaceSubject(oldSubject, newSubject)
    {
       this.syncAnnotScan();
       var numReplacements = 0;
       for (var i = 0; i < this.numPages; i++)
       {
          var annots = this.getAnnots(i);
          if (annots != null)
          {
            for (var j = 0; j < annots.length; j++)
            {
               if (annots[j].subject == oldSubject)
               {
                  annots[j].subject = newSubject;
                  numReplacements++;
               }
            }
          }
       }
       console.println("Replacements: " + numReplacements);
    }

    //
    ReplaceSubject("oldSubject", "newsSubject");
Tanks again Ivan,

François Maurice
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm

Re: Search and Replace author and subject within notes

Post by Will - Tracker Supp »

Hi François,

thanks for posting the updated script! :)
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.

Best regards

Will Travaglini
Tracker Support (Europe)
Tracker Software Products Ltd.
http://www.tracker-software.com