Copy link to clipboard
Copied
Hi all, is there a Javascript I could add to an Action that would order pages in a pdf by size, i.e. dimensions? for background I occasionally need to sort through a mixed page size pdf and group pages of the same dimensions together for printing purposes. Thanks in advance for any help.
Copy link to clipboard
Copied
Hi,
Try this script:
var pPages=[];
var sPages=[];
var lPages=[];
for (var p=0; p<this.numPages; p++) {
var aRect=this.getPageBox("Trim", p);
var width=aRect[2]-aRect[0];
var height=aRect[1]-aRect[3];
if (width<height) pPages.push([width,height,p]);
else if (width==height) sPages.push([width,p]);
else lPages.push([width,height,p]);
}
pPages.sort(function(a, b){return a[0]-b[0]});
sPages.sort(function(a, b){return a[0]-b[0]});
lPages.sort(function(a, b){return a[0]-b[0]});
var newDoc=app.newDoc();
for (var i=0; i<pPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: pPages[i][2]
});
}
for (var i=0; i<sPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: sPages[i][1]
});
}
for (var i=0; i<lPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: lPages[i][2]
});
}
newDoc.deletePages(0);
newDoc.saveAs({
cPath: this.path.replace(/.pdf$/i," (sorted by page orientation and size).pdf"),
});
this.closeDoc();
You can either copy then paste it in the console or past it for an action wizard.
It will sort the pages by orientation (portrait, square, landscape) and dimensions of the width then the height.
You will get a new file with sorted pages.
Let me know if that suits you or if you want change the sort.
@+
Copy link to clipboard
Copied
It's possible, but you need to clearly define how it should work.
What are the criteria for deciding which page should come first? Width? Height? Total page area (W * H)?
Also, pages have multiple "boxes" which define them. Do you want to use a specific one, like the Crop Box?
What if page 1 has a larger Crop Box than page 2, but a smaller Trim Box? etc.
Copy link to clipboard
Copied
Hi, many thanks for that, I would require them to be matched by trim box width and height so pages of the same dimensions are grouped together, it doesnt matter which order the groups are in as I just need to extract pages of the same size for creating print ready files our RIP will multi-up.
Copy link to clipboard
Copied
The general approach is to use a loop to iterate over all the pages, adding them to an array that contains both the box sizes and page numbers. Then sort that array using a custom sorting function that compares the page dimension arrays. Then sort the document (or, more easily, create a new one) with the original pages in the sorted order.
This is not a trivial task and requires quite an advanced level of Acrobat JS know-how.
Copy link to clipboard
Copied
..yes that does sound a bit complicated, I'll stick to the manual process and try to get these pages grouped at source. Many thanks for the swift response.
Copy link to clipboard
Copied
Hi,
Try this script:
var pPages=[];
var sPages=[];
var lPages=[];
for (var p=0; p<this.numPages; p++) {
var aRect=this.getPageBox("Trim", p);
var width=aRect[2]-aRect[0];
var height=aRect[1]-aRect[3];
if (width<height) pPages.push([width,height,p]);
else if (width==height) sPages.push([width,p]);
else lPages.push([width,height,p]);
}
pPages.sort(function(a, b){return a[0]-b[0]});
sPages.sort(function(a, b){return a[0]-b[0]});
lPages.sort(function(a, b){return a[0]-b[0]});
var newDoc=app.newDoc();
for (var i=0; i<pPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: pPages[i][2]
});
}
for (var i=0; i<sPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: sPages[i][1]
});
}
for (var i=0; i<lPages.length; i++) {
newDoc.insertPages ({
nPage: newDoc.numPages-1,
cPath: this.path,
nStart: lPages[i][2]
});
}
newDoc.deletePages(0);
newDoc.saveAs({
cPath: this.path.replace(/.pdf$/i," (sorted by page orientation and size).pdf"),
});
this.closeDoc();
You can either copy then paste it in the console or past it for an action wizard.
It will sort the pages by orientation (portrait, square, landscape) and dimensions of the width then the height.
You will get a new file with sorted pages.
Let me know if that suits you or if you want change the sort.
@+
Copy link to clipboard
Copied
..well this works a treat, thank you so much

