Using JavaScript to automate tasks

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
JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Using JavaScript to automate tasks

Post by JohnSmithUSA »

Hello,

I want to:

1. Count text fields with specific color using JS (text fields are created manually with PDF-XChange).
2. Insert into them their respective number or string (like SOMETHINGX, SOMETHINGX+1 and so on).
a) clean all fields before step 2
3. Change color of each text box (without mundane, manual work) of each box.

Are these magic tricks possible?

Cheers
Last edited by JohnSmithUSA on Thu Jul 12, 2018 9:00 am, edited 2 times in total.
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm
Location: London, UK
Contact:

Re: Using JavaScript to automate tasks

Post by Will - Tracker Supp »

Hi John,

Thanks for the post - This should be possible using JavaScript. We support most of the features of the Adobe JS API. Please see the reference manual here:
https://www.adobe.com/content/dam/acom/ ... erence.pdf

We don't typically write scripts for our users, the onus is on users to write any scripts they need themselves. On rare occasions (i.e. when it's exceedingly quick simple, or when it's of a benefit to a large amount of users) we may write them.

Thanks,
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
JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Re: Using JavaScript to automate tasks

Post by JohnSmithUSA »

Ok, I understand. I don't have IT background, but will try to fight on my own.

If anybody has too much free time, I'd be really glad to receive any help. Thanks in advance.
User avatar
Will - Tracker Supp
Site Admin
Posts: 6815
Joined: Mon Oct 15, 2012 9:21 pm
Location: London, UK
Contact:

Re: Using JavaScript to automate tasks

Post by Will - Tracker Supp »

Hi John,

If you do struggle, we'd be happy to help guide you. Alternately, if you can get the general outline of the script but just can't seem to get it working properly, feel free to sent the document & script and we can help from there.

Cheers,
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
JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Re: Using JavaScript to automate tasks

Post by JohnSmithUSA »

Adobe API js handbook, page 447

var count = 0;
for ( var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
if ( this.getField(fname).type == "text" ) count++; }
console.println("There are " + count + " text fields.");

Result:
There are 0 text fields.
undefined



This script doesn't work... or it has different understanding of text field. I want to calculate text field created by user.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using JavaScript to automate tasks

Post by Tracker Supp-Stefan »

Hello John,

Those should be actual form text fields - and the script will then work and give you their count:
text_field_count.png
Maybe the objects you have are text box annotations?
Can you share with us a sample file with the objects that you want to modify visible - and we will then be able to advise further!?

Regards,
Stefan
peydisegna
User
Posts: 1
Joined: Thu Jul 12, 2018 6:26 pm

Re: Using JavaScript to automate tasks

Post by peydisegna »

I'll chime in.

The same problem.
I attach my results.

http://docdro.id/3Prdck4
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8547
Joined: Wed Jan 03, 2018 6:52 pm

Re: Using JavaScript to automate tasks

Post by TrackerSupp-Daniel »

Hello Peydisegna,
I see that as Stefan mentioned before, you've used text comments, instead of form fields.
For this to work you will need to use Form fields, This article should help you:
https://www.pdf-xchange.com/knowle ... -PDF-forms
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Re: Using JavaScript to automate tasks

Post by JohnSmithUSA »

Why my script doesn't work? :(

var count = 0;
for ( var i=0; i<this.numComments; i++) {
var fname = this.getNthCommentName(i);
if ( this.getField(fname).type == "comment" ) count++; }
console.println("There are " + count + " comments.");
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17893
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using JavaScript to automate tasks

Post by Tracker Supp-Stefan »

Hello John,

Where are you getting those scripts from?
I could not find methods with those names you try to use here:
https://www.adobe.com/content/dam/acom/ ... erence.pdf
So if you got this code from somewhere else - most likely those were defined elsewhere and you missed their definition?

Please try something like this to work with annotations on a PDf's page.

Code: Select all

var annots = this.getAnnots({
nPage:0, 
nSortBy: ANSB_Author,
bReverse: true
});
console.show();
console.println("Number of Annotations: " + annots.length);
var msg = "%s in a %s annot said: \"%s\"";
for (var i = 0; i < annots.length; i++) 
console.println(util.printf(msg, annots[i].author, annots[i].type,
annots[i].contents));
Cheers,
Stefan
Post Reply