Using JavaScript to automate tasks

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

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

Post Reply
JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Using JavaScript to automate tasks

Post by JohnSmithUSA » Thu Jul 12, 2018 8:34 am

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: 6353
Joined: Mon Oct 15, 2012 9:21 pm
Location: London, UK
Contact:

Re: Using JavaScript to automate tasks

Post by Will - Tracker Supp » Thu Jul 12, 2018 8:54 am

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 » Thu Jul 12, 2018 8:58 am

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: 6353
Joined: Mon Oct 15, 2012 9:21 pm
Location: London, UK
Contact:

Re: Using JavaScript to automate tasks

Post by Will - Tracker Supp » Thu Jul 12, 2018 9:12 am

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 » Thu Jul 12, 2018 11:26 am

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: 13182
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using JavaScript to automate tasks

Post by Tracker Supp-Stefan » Thu Jul 12, 2018 12:15 pm

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 » Thu Jul 12, 2018 6:39 pm

I'll chime in.

The same problem.
I attach my results.

http://docdro.id/3Prdck4

User avatar
TrackerSupp-Daniel
Site Admin
Posts: 1887
Joined: Wed Jan 03, 2018 6:52 pm

Re: Using JavaScript to automate tasks

Post by TrackerSupp-Daniel » Thu Jul 12, 2018 10:56 pm

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.tracker-software.com/knowle ... -PDF-forms
Daniel McIntyre
Support Technician
Tracker Software Products (Canada) LTD

Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623

JohnSmithUSA
User
Posts: 6
Joined: Wed Apr 25, 2018 2:00 pm

Re: Using JavaScript to automate tasks

Post by JohnSmithUSA » Fri Jul 13, 2018 8:55 am

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: 13182
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using JavaScript to automate tasks

Post by Tracker Supp-Stefan » Fri Jul 13, 2018 9:54 am

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