Copy link to clipboard
Copied
Dear ALL,
I have a document with multiple Page size eg its mixture of A3, A4 & letter size. Is there is any way that PDF can list the available page sizes & count of the no. eg
I would like to check before printing the document as it get stuck in printer due to non availabity of the right paper, eg A1, letter etc.. Please advice. Any Jawascript may work..?
It's pretty simple to loop through the page sizes using JavaScript; here's a chunk of example code you can run from the console. Add more pages sizes as required, remember Acrobat measures everything in Points (72 pt per inch):
...console.println("There are " + this.numPages + " pages in this document");
// counters
var numA3 = 0;
var numA4 = 0;
var numUSL = 0; // etc.
// helper function to compare a number with some wiggle room - we look for portrait and landscape possibilities
function inRange(wid,hei,t
Copy link to clipboard
Copied
This can be done using a custom-made script. If you're interested in purchasing such a tool feel free to contact me privately at try6767 at gmail.com.
Copy link to clipboard
Copied
Thanks for your reply, looking for free options, I think your tool will be paid software. Anyway thank for your help.
Copy link to clipboard
Copied
It's pretty simple to loop through the page sizes using JavaScript; here's a chunk of example code you can run from the console. Add more pages sizes as required, remember Acrobat measures everything in Points (72 pt per inch):
console.println("There are " + this.numPages + " pages in this document");
// counters
var numA3 = 0;
var numA4 = 0;
var numUSL = 0; // etc.
// helper function to compare a number with some wiggle room - we look for portrait and landscape possibilities
function inRange(wid,hei,targetWid,targetHei) {
var wiggle = 2; // amount of error to allow in Points. Not all PDF software creates perfectly-sized pages
return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) && (hei>=(targetHei-wiggle) && hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) && (wid>=(targetHei-wiggle) && wid<=(targetHei+wiggle))
}
for (pagenum=0;pagenum<this.numPages;pagenum++) {
var pbox = this.getPageBox("Crop",pagenum);
var width = (pbox[2] - pbox[0]);
var height = (pbox[1] - pbox[3]);
var recognized = false;
if (inRange(width,height,841,1188)) { numA3++; recognized = true; }
if (inRange(width,height,595,841)) { numA4++; recognized = true; }
if (inRange(width,height,594,792)) { numUSL++; recognized = true; }
// etc...
if (!recognized) console.println("Page " + (pagenum+1) + " has a width of " + (width/72) + " and a height of " + (height/72) + "inches");
}
console.println(numA3 + " A3 pages");
console.println(numA4 + " A4 pages");
console.println(numUSL + " US Letter pages");
console.println((this.numPages++ - (numA3+numA4+numUSL)) + " other pages");
Copy link to clipboard
Copied
Thanks Dave,
However I am very new to Adobe Java scripting, hence I googled and find some procedure as how to execute , but it throws error message as below
"
ReferenceError: numA3 is not defined
1:Console:Exec
undefined"
Copy link to clipboard
Copied
Open the JavaScript console (Ctrl-J), slear it using the trashcan icon on the bottom corner, and paste in the script.
Select it all (Ctrl-A) then press CTRL+ENTER to execute the whole script. If you don't do that it will only run the last line, hence the error.
Copy link to clipboard
Copied
BINGO !! Thanks a ton Dave, It work flawlessly, only I tweaked the var wiggle = 2 -> 3 and I got the desired output.
<Output>
"There are 209 pages in this document
Page 163 has a width of 17 and a height of 11inches
Page 164 has a width of 17 and a height of 11inches
196 A3 pages
11 A4 pages
0 US Letter pages
2 other pages "
Sorry for requesting again,
Is it possible to add the a button in the Tool Menu, so that I can execute this code anytime and also it gives the above output result in the pop window.
And further, if it can identify the incorrect page no. also in the pop-up result, it will be an icing on the cake.
Please let me know.
Thanks!
Regards
Mahesh
Copy link to clipboard
Copied
Dear Dave,
I tried to add the above script in adobe action, so as to make it handy tool and every time no need to run through console, the code executed, however as you know there is no message box in the code, hence it doesn't display any pop up, please could you help.
Regards
Mahesh
Copy link to clipboard
Copied
A simple change to show you the way is to change console.println to app.alert .
Copy link to clipboard
Copied
Dear Test Screen Name, got it, thanks!
Will try the same, secondly, is there a way to fix the decimal values in the line
console.println("Page " + (pagenum+1) + " has a width of " + (width/72) + " and a height of " + (height/72) + "inches");
sometime its printing till 15 decimal places.
eg one the output is
Page 7 has a width of 16.541666666666668 and a height of 11.694444444444445inches
is there a way that it can rounup to 2 decimal places. Thanks
Copy link to clipboard
Copied
To round a number in JS, use the .toFixed() method. Here's a revised script that also outputs to an alert box:
var msgText = "There are " + this.numPages + " pages in this document\n";
// counters
var numA3 = 0;
var numA4 = 0;
var numUSL = 0;
// helper function to compare a number with some wiggle room
function inRange(wid,hei,targetWid,targetHei) {
var wiggle = 2; // amount of error to allow
return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) && (hei>=(targetHei-wiggle) && hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) && (wid>=(targetHei-wiggle) && wid<=(targetHei+wiggle))
}
for (pagenum=0;pagenum<this.numPages;pagenum++) {
var pbox = this.getPageBox("Crop",pagenum);
var width = (pbox[2] - pbox[0]);
var height = (pbox[1] - pbox[3]);
var recognized = false;
if (inRange(width,height,841,1188)) { numA3++; recognized = true; }
if (inRange(width,height,595,841)) { numA4++; recognized = true; }
if (inRange(width,height,594,792)) { numUSL++; recognized = true; }
if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n");
}
msgText += numA3 + " A3 pages\n" + numA4 + " A4 pages\n" + numUSL + " US Letter pages\n" + (this.numPages++ - (numA3+numA4+numUSL)) + " other pages";
app.alert(msgText);
Copy link to clipboard
Copied
Perfect!! Thanks Dave Merchant and Test Screen Name​ for your help!
I never expected that the community will be so helpful and will get the response so fast! Amazing!
Copy link to clipboard
Copied
Hi.
I found this code very helpful and would like to develop further, please excuse my clumsy attempt at brute force hacking
I would like to have inline a calculated value based on 0.25 cents per A4 page equivalent. for example A3 would be 2 x .25 or .50 cents per page.
I cant seem to get the code right. the calculated text drops down a line
I modified your code for the different paper we use and increased the wiggle room allowance to see scans better. my scanner output varies to ISO standards
--
var msgText = "There are " + this.numPages + " pages in this document\n";
// counters
var numA4 = 0;
var numA3 = 0;
var numA2 = 0;
var numA1 = 0;
var numA0 = 0;
var p = .25;
// helper function to compare a number with some wiggle room
function inRange(wid,hei,targetWid,targetHei) {
var wiggle = 28; // amount of error to allow
return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) && (hei>=(targetHei-wiggle) && hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) && (wid>=(targetHei-wiggle) && wid<=(targetHei+wiggle)) }
for (pagenum=0;pagenum<this.numPages;pagenum++) {
var pbox = this.getPageBox("Crop",pagenum);
var width = (pbox[2] - pbox[0]);
var height = (pbox[1] - pbox[3]);
var recognized = false;
if (inRange(width,height,595,842)) { numA4++; recognized = true; }
if (inRange(width,height,842,1191)) { numA3++; recognized = true; }
if (inRange(width,height,1191,1684)) { numA2++; recognized = true; }
if (inRange(width,height,1684,2384)) { numA1++; recognized = true; }
if (inRange(width,height,2384,3370)) { numA0++; recognized = true; }
if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n"); }
msgText +=
numA4 + (" A4 pages\n" +(numA4*p))
+ numA3 + (" A3 pages\n" +(numA3*p))
+ numA2 + (" A2 pages\n" +(numA2*p))
+ numA1 + (" A1 pages\n" +(numA1*p))
+ numA0 + (" A0 pages\n" +(numA0*p))
+ (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages";
app.alert(msgText);
Copy link to clipboard
Copied
Put the newline character \n at the correct place.
Copy link to clipboard
Copied
Hi Brend
Thanks for replying
I assume at the end of this line?
---
msgText +=
numA4 + (" A4 pages\n" +(numA4*p))\r
+ numA3 + (" A3 pages\n" +(numA3*p))\r
---
Which brings up the error, SyntaxError: illegal character
Some discussions Ive located using Google talk about combinations of using \n and \r\n
Ive tried all of them and all result in an error message for me.
Maybe how Iam trying to solve this is flawed, Maybe trying to put a space after the calculation can be done another way?
In my example image above the document contains 8 pages 7 are A4 and one is an A2. the output gives a zero for page sizes not found so the zero for A3 is butted up against the 1.75 value of the calculated formula for the 7x A4 pages.
Regards
Paul
Copy link to clipboard
Copied
Use this:
msgText +=
numA4 + " A4 pages " + numA4*p + "\n"
+ ...
Copy link to clipboard
Copied
Thanks Bernt
That works
Paul
Copy link to clipboard
Copied
Hi.. Last part of my plan was to add the values.. but I cant get it to display, can anyone help? please
I created a var to hold the value and added them up at the end of the code, the script runs but the sum does not display. wondering if the value is text?
---
var msgText = "There are " + this.numPages + " pages in this document\n";
// counters
var numA4 = 0;
var numA3 = 0;
var numA2 = 0;
var numA1 = 0;
var numA0 = 0;
var p = .25;
// Values
var A4 = numA4*p;
var A3 = numA3*p;
var A2 = numA2*p;
var A1 = numA1*p;
var A0 = numA0*p;
// helper function to compare a number with some wiggle room
function inRange(wid,hei,targetWid,targetHei) {
var wiggle = 28; // amount of error to allow
return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) && (hei>=(targetHei-wiggle) && hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) && (wid>= (targetHei-wiggle) && wid<=(targetHei+wiggle)) }
for (pagenum=0;pagenum<this.numPages;pagenum++) {
var pbox = this.getPageBox("Crop",pagenum);
var width = (pbox[2] - pbox[0]);
var height = (pbox[1] - pbox[3]);
var recognized = false;
if (inRange(width,height,595,842)) { numA4++; recognized = true; }
if (inRange(width,height,842,1191)) { numA3++; recognized = true; }
if (inRange(width,height,1191,1684)) { numA2++; recognized = true; }
if (inRange(width,height,1684,2384)) { numA1++; recognized = true; }
if (inRange(width,height,2384,3370)) { numA0++; recognized = true; }
if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n"); }
msgText +=
numA4 + " A4 pages " + numA4*p + "\n"
+ numA3 + " A3 pages " + numA3*p + "\n"
+ numA2 + " A2 pages " + numA2*p + "\n"
+ numA1 + " A1 pages " + numA1*p + "\n"
+ numA0 + " A0 pages " + numA0*p + "\n"
+ (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages";
+ (this.Value ++ - (A4+A3+A2+A1+A0))+ "Value";
app.alert(msgText);
Copy link to clipboard
Copied
Drop the semi-colon in the line that ends with " other pages";
Also, there's no such thing as "this.Value". I'm not sure what you tried to
do there, but that's not going to work.
Copy link to clipboard
Copied
Thanks, I doing baby steps here.
The changes now look like this
---
+ (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages" + "\n"
+ " Sum " +(A4+A3+A2+A1+A0);
app.alert(msgText);
---
This runs but wont add up, however displays: Sum 0
Any thoughts?
Copy link to clipboard
Copied
Omitted to mention
I have declared Sum as a variable
var Sum = 0
Copy link to clipboard
Copied
If you created a variable for this, why are you not using it?
Copy link to clipboard
Copied
Sorry So is the way I wrote the calculation below ok, I assume this value gets associated to the Variable, really don't know how to reference the var sum
Eg:
+ " Sum " +(A4+A3+A2+A1+A0);
should it be something like:
+(A4+A3+A2+A1+A0)+ " Sum ";
your help is appreciated
Copy link to clipboard
Copied
You can't assume anything. I don't see anywhere in your code that you declared such a variable or assigned a value to it.
And as Bernd correctly pointed out, you're using incorrect names.
Copy link to clipboard
Copied
Thanks. I know nothing about java, just an inspired hacker trying to make it work.
I have adjusted the code to reflect the stored value but the script now return the number of hits, that it where the document included 7x A4 pages and 1x A2 page it returns a sum of 2 rather than totalling the extended values. I will set about creating a variable to hold each calculated value and see if that works
---
var msgText = "There are " + this.numPages + " pages in this document\n";
// counters
var numA4 = 0;
var numA3 = 0;
var numA2 = 0;
var numA1 = 0;
var numA0 = 0;
var p = .25;
// Values
var A4 = numA4*p;
var A3 = numA3*p;
var A2 = numA2*p;
var A1 = numA1*p;
var A0 = numA0*p;
var Sum = 0;
// helper function to compare a number with some wiggle room
function inRange(wid,hei,targetWid,targetHei) {
var wiggle = 28; // amount of error to allow
return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) && (hei>=(targetHei-wiggle) && hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) && (wid>= (targetHei-wiggle) && wid<=(targetHei+wiggle)) }
for (pagenum=0;pagenum<this.numPages;pagenum++) {
var pbox = this.getPageBox("Crop",pagenum);
var width = (pbox[2] - pbox[0]);
var height = (pbox[1] - pbox[3]);
var recognized = false;
if (inRange(width,height,595,842)) { numA4++; recognized = true; }
if (inRange(width,height,842,1191)) { numA3++; recognized = true; }
if (inRange(width,height,1191,1684)) { numA2++; recognized = true; }
if (inRange(width,height,1684,2384)) { numA1++; recognized = true; }
if (inRange(width,height,2384,3370)) { numA0++; recognized = true; }
if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n"); }
msgText +=
numA4 + " A4 pages " + numA4*p + "\n"
+ numA3 + " A3 pages " + numA3*p + "\n"
+ numA2 + " A2 pages " + numA2*p + "\n"
+ numA1 + " A1 pages " + numA1*p + "\n"
+ numA0 + " A0 pages " + numA0*p + "\n"
+ (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages" + "\n"
+ "sum " + (numA4*p+numA3*p+numA2*p+numA1*p+numA0*p);
app.alert(msgText);
---