Copy link to clipboard
Copied
Hello,
I am new to Acrobat JS and automation field and am trying to automate some tasks in the given sequence using JS and Action wizard, that doesn't require user interaction.
1. Print to PDF
2. Encrypt
3. Save
I am having trouble in the print to PDF step. I want to Print all pdfs in a given folder to its subfolder named "Print", and name the printed files dynamically as well.
I also want to set some of the printParams dynamically based on filename of pdf, to be specific, I want some of the pdfs to be printed using one color profile, and the others using a different color profile.
For example, I have two pdf forms with file names, "Year" and "Quarter".
When I run the action, I want the script to check the filename, in a given switch/if condition, and if it matches "Quarter" file name condition,
I want to run a loop, that changes one of the check box values in that form, prints it using a specific (within condition)hardcoded color profile, and repeats the task until all four(quarter) check boxes have been printed to separate files with filenames of printed files set based on the checkbox that was marked.
But if the "Year" file name condition is met, I want it to print the file with the same name in "Print" subfolder, with a specific different color profile, also hardcoded within condition.
I've exhausted online resources as best as I could, used Acrobat SDK and JS API references to look for something useful but haven't been successful.
Any help or guidance would be appreciated.
The code I came up with is as follows:
When trying to save it an "execute javascript" action step, it gives following error
SyntaxError: illegal character
10: at Line 11
var pp = this.getPrintParams()
pp.interactive = pp.constants.interactionLevel.automatic;
pp.printerName = "Adobe PDF"
var d = app.activeDocs;
for (var i=0; i < d.length; i++)
switch(d[i].info.Title){
case "Quarter":
for(i=1 ; i!=4 ; i++)
this.getField("Quarters").value = `Q${i}`;
pp.colorProfile = "Dot Gain 15%"
this.print(pp)
break;
case "Year":
pp.colorProfile = "Gray Gamma 1.8"
this.print(pp)
break;
default:
this.print(pp)
break;
}
Copy link to clipboard
Copied
Don't use the character `
Copy link to clipboard
Copied
Thank you, this solves the error.
Also, for loop wasn't working as expected so I changed to a while loop and worked like charm. I had to assign "Q" to a string and concat the variable i at its end using concat method, assigning the value to form field.
Now, I'll work on dynamic path and filename setting to automate print and save steps as well.
Thanks once again. Cheers!
Copy link to clipboard
Copied
You can't fully automate this process, since you can't specify the file name to be saved when printing to the Adobe PDF virtual printer. You'll have to manually enter it for each file.
Copy link to clipboard
Copied
I believe this can be achieved by
filenameproperty of printParams.
I'm yet going through API and SDK references to look for a method for getting path, but I am pretty sure there has to be one for getting current path too, or something to get relative path of the folder selected in Action.
Copy link to clipboard
Copied
No, that's like when you use the Print to File option. It will generate a .prn file, not a .pdf file.
Copy link to clipboard
Copied
Oh I see. Are you certain about this?
And in that case, is there any way to atleast open the relative folder to which I want to save the file, so it can save me one step at the least.
Copy link to clipboard
Copied
Yes, and no.
Copy link to clipboard
Copied
I should mention in case you don't know, print PDF to PDF is generally considered a -terrible- thing to do, many say it should never be done. Often it seems a simple solution to a problem, but creates worse ones. But if you tell us why you want to do this bad thing, we may be able to help you find a better alternative.
Copy link to clipboard
Copied
Hello,
I am aware of the security and other concerns around printing PDF to PDF, and yet would still like to proceed. And I am not looking for any alternatives, only automation of this repetitive task, and/or don't think they'd be beneficial.
But yes, if you insist, I have some forms with custom calculation fields, which I often have to share with others, but in doing so, I want the form fields as well as formulas/calculation scripts to be removed, but leave the form editable using Acrobat Edit tool or any other software tools capable of doing that.
My main concern is to secure the calculation scripts and get rid of form fields, however, encrypting the document does no benefit as there are many tools available to remove encryption.
Hope this clears things up!
Copy link to clipboard
Copied
You can simply flatten the fields, then.
Copy link to clipboard
Copied
I want to keep the original file, and flattened file in separate folders, while flatenning some of the forms multiple times as mentioned in original question, after changing a form field dynamically using a loop.
Then there are some forms, also checked using conditions, that I'd like to be flattened in black and white instead of colored.
Is that all possible?
If yes, kindly point me in the right direction and I'll start learning from resources.
Thanks!
Copy link to clipboard
Copied
You will need to open the file in each iteration of the loop, because the fields will be gone once you flatten it.
And you can use the colorConvertPage method to change it to the desired color profile.
This is not a simple task, but it should be possible.
Copy link to clipboard
Copied
And does this method support choosing path and filename using js?
Plus, what is the difference between printing and flattening anyway, when they provide the same result, while print works more efficiently in automation(except the path and filename part, of course)?
Copy link to clipboard
Copied
Yes, because you could then use a simple Save As command to save the file.
There are plenty of differences between flattening and re-frying a file. The former only affects fields and comments, while the latter affects pretty much every kind of object that's present in the file. If you want more details this issue was discussed many times in the past, so you can search the forums, but the general consensus is that re-frying a file is a bad idea and should be avoided if possible.
Copy link to clipboard
Copied
Awesome, I tried and came up with a new script. But new solutions generate new problems, so here I am.
Below is the code I am using. It works for the most part except for color conversion and looping through Quarters PDF file for making separate flattened copies with one of four checkboxes selected each time, one by one.
Could you point out any errors or ommissions?
// Get a color convert action
function convertToBW(pageNo){
var toBW = this.getColorConvertAction();
toBW.matchAttributesAny = -1;
toBW.matchIntent = toBW.constants.renderingIntents.Any;
toBW.convertProfile = "Dot Gain 15%";
toBW.convertIntent = toBW.constants.renderingIntents.Document;
toBW.action = toBW.constants.actions.Convert;
this.colorConvertPage(pageNo,[toBW],[]);
}
switch(this.documentFileName.replace(/.pdf/,"")){
case "Quarters":
var i = 1
while(i<5){
var str = "Q"
this.getField("Quarters").value = str.concat(i);
this.extractPages({cPath:"Print/"+ this.documentFileName.replace(/.pdf/,"").concat(i) + ".pdf"});
this.flattenPages();
var pg = 1
while(pg<this.numPages){
convertToBW(pg);
pg++;
}
this.saveAs({
cPath: "Print/"+ this.documentFileName.replace(/.pdf/,"").concat(i) + ".pdf",
bCopy: false,
bPromptToOverwrite: false
});
i++
}
break;
case "Year":
this.extractPages({cPath:"Print/"+ this.documentFileName});
this.flattenPages();
this.saveAs({
cPath: "Print/"+ this.documentFileName,
bCopy: false,
bPromptToOverwrite: false
});
break;
default:
this.extractPages({cPath:"Print/"+ this.documentFileName});
this.flattenPages();
var pg = 1
while(pg<this.numPages){
convertToBW(pg);
pg++;
}
this.saveAs({
cPath: "Print/"+ this.documentFileName,
bCopy: false,
bPromptToOverwrite: false
});
break;
}
Copy link to clipboard
Copied
>But new solutions generate new problems
This is the essence of programming in a nut-shell... 🙂
You have to close the file at the end of the loop and then re-open it, because once you flatten the pages there are no more fields in it. Also, I would use a variable to point to it, instead of the "this" object.
Copy link to clipboard
Copied
I'm sorry, but I am bit confused on how to point to the current document using a variable, and check that against conditions. Or should it be done within the loop?
Also, using
this.closeDoc()just before the loop closes would accomplish the closing part, right?
TBH, I am out of logic for now, yet want to finish and get done with the script.
Could you share a code example for both your suggestions? And any ideas on why Color Conversion isn't working?
Copy link to clipboard
Copied
To save the reference to the document as a variable simply do this:
var myDoc = this;
Then use myDoc instead of "this" in your code, and when you (re-)open the file apply the return value of openDoc back to myDoc, like this:
myDoc = app.openDoc(originalFilePath);
I don't know why the color conversion is not working. I'll need to see the file to be able to look into it.
Copy link to clipboard
Copied
Hey, Thanks for all the help. Tried as you suggested, described below:
Created a variable at the beginning of script as
var currDoc = this;Replaced all references of "this" keyword with above variable. This is the updated loop:
case "Quarters":
var i = 1
while(i<5){
var str = "Q"
currDoc.getField("Quarters").value = str.concat(i);
currDoc.extractPages({cPath:"Print/"+ currDoc.documentFileName.replace(/.pdf/,"").concat(i) + ".pdf"});
currDoc.flattenPages();
var pg = 0
while(pg<currDoc.numPages){
convertToBW(pg);
pg++;
}
currDoc.saveAs({
cPath: "Print/"+ currDoc.documentFileName.replace(/.pdf/,"").concat(i) + ".pdf",
bCopy: false,
bPromptToOverwrite: false
});
var pathRef = currDoc
var fileName = currDoc.documentFileName.replace(/.pdf/,"")
currDoc.closeDoc();
currDoc = app.openDoc({
oDoc : pathRef,
cPath : "../"+fileName.slice(0, -1)+".pdf",
bHidden : true
})
i++
}
break;This is working now.
I run the action and add a folder that contains required files. It begins the process, extracts them one by one to "Print" folder, flattens, converts color if condition matches, and saves them.
For the Quarters file, it runs the loop, saving it four times to separate files with different checkboxes checked each time.
Color conversion was not working on the first page only, I didn't notice that earlier. That's because I was starting the loop from page 1, and was confused that pages aren't zero indexed. Just changed that and it works.
All is working as expected now, except for one small problem.
After running the loop on "Quarters" file, when all of them are flattened and saved, it gives the below error/warning, and I have to click "No" close the quarters file manually and click "Start" again on the paused Action.
I tried closing the currDoc after loop, right before the "break" statement of case but to vain.
Copy link to clipboard
Copied
Don't close the file at the end of the process, since the Action is doing that by itself.
Copy link to clipboard
Copied
Do you mean "don't close using js after the loop"? If yes, I'm not. That was just a test run to see if that solves the issue.
The current code I'm using is exactly the one I shared in previous message. And it gives the said error, i.e. pauses the action, asking to either exit or not. And when I click "No" then the original "Quarters" file is displayed which I have to close manually and only then, I see the latest flattened file with the Actions side bar open, with the action paused, and I resume it by clicking "Start" button again.
So basically, the automation is not completely automatic yet.
Copy link to clipboard
Copied
It's problematic to do this kind of thing in an Action... Instead of working on the original file try saving it under a new name, open that copy, and then run the loop using that one.
Copy link to clipboard
Copied
What kind of thing specifically?
And should the copy be made using script before the loop runs?
Copy link to clipboard
Copied
- Thing: opening and closing files
- Yes
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more