Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript Page size query

Community Beginner ,
Mar 22, 2022 Mar 22, 2022

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.

TOPICS
JavaScript
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 22, 2022 Mar 22, 2022

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.

@+

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2022 Mar 22, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2022 Mar 22, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2022 Mar 22, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2022 Mar 22, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2022 Mar 22, 2022

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.

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 23, 2022 Mar 23, 2022
LATEST

..well this works a treat, thank you so much 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines