Skip to main content
Usman Ahmad Saeed
Participating Frequently
April 26, 2021
Question

Acrobat print automation with printParams set conditionally

  • April 26, 2021
  • 3 replies
  • 6471 views

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;
}

 

 

This topic has been closed for replies.

3 replies

Brainiac
April 26, 2021

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. 

Usman Ahmad Saeed
Participating Frequently
April 26, 2021

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!

Thanks &amp; RegardsUsman Ahmad SaeedUsmanAhmadSaeed.me
Usman Ahmad Saeed
Participating Frequently
April 26, 2021

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.


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;
}

 

Thanks &amp; RegardsUsman Ahmad SaeedUsmanAhmadSaeed.me
try67
Community Expert
April 26, 2021

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.

Usman Ahmad Saeed
Participating Frequently
April 26, 2021

I believe this can be achieved by

filename

property 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.

Thanks &amp; RegardsUsman Ahmad SaeedUsmanAhmadSaeed.me
try67
Community Expert
April 26, 2021

No, that's like when you use the Print to File option. It will generate a .prn file, not a .pdf file.

Bernd Alheit
Community Expert
April 26, 2021

Don't use the character `

Usman Ahmad Saeed
Participating Frequently
April 26, 2021

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!

Thanks &amp; RegardsUsman Ahmad SaeedUsmanAhmadSaeed.me