Page Splitter – For The 3rd Time – Splitting Tri-Fold Brochures

We’ve covered page splitting before, see these two articles for some background information:

Both these posts deal with splitting a page into halfs, this tine we can to look at how to split a page into thirds. To modify the previous script (from the “Redux” post), to handle thirds instead of halfs, we need to make a few changes. In general, instread of creating two copies of every page in the output document, we need to create three copies, and instead of creating two crop boxes (left and right), we need three (left, middle and right).

For the splitting into two, we did not consider the actual page order, the same is true here: Chances are you would read such a trifold brochure, in a certain order, we are not taking that order into account, and instead are just extracting the pages from the right (right third first, then middle third, followed by left most third):

Screenshot of a tri-fold document in Adobe Acrobat - pges are numbered 1, 2 and 3 from the right.

Here is the updated script:

/* Split a tri-fold brochure into individual pages */

// create a new document
var newDoc = app.newDoc();

// get the filename of our current file

var i = 0;
while (i < this.numPages) {
	newDoc.insertPages({
		nPage: newDoc.numPages - 1,
		cPath: this.path,
		nStart: i
	});
	newDoc.insertPages({
		nPage: newDoc.numPages - 1,
		cPath: this.path,
		nStart: i
	});
	newDoc.insertPages({
		nPage: newDoc.numPages - 1,
		cPath: this.path,
		nStart: i
	});
	// We did this three times so that we can then split each copy of the page into a left, middle
	// and right potion of the page. 
	i++;
}

if (newDoc.numPages > 1) {
	newDoc.deletePages(0); // this gets rid of the page that was created with the newDoc call.
}

// At this point we have a documnent with every page from the source document
// copied three times.


for (i = 0; i < newDoc.numPages; i++) {
	// determine the crop box of the page
	var cropRect = newDoc.getPageBox("Crop", i);
	var thirdWidth = (cropRect[2] - cropRect[0]) / 3;

	var cropLeft = new Array();
	cropLeft[0] = cropRect[0];
	cropLeft[1] = cropRect[1];
	cropLeft[2] = cropRect[0] + thirdWidth;
	cropLeft[3] = cropRect[3];
	
	var cropMiddle = new Array();
	cropMiddle[0] = cropRect[0] + thirdWidth;
	cropMiddle[1] = cropRect[1];
	cropMiddle[2] = cropRect[2] - thirdWidth;
	cropMiddle[3] = cropRect[3];

	var cropRight = new Array();
	cropRight[0] = cropRect[2] - thirdWidth;
	cropRight[1] = cropRect[1];
	cropRight[2] = cropRect[2];
	cropRight[3] = cropRect[3];

	if (i % 3 == 0) {
		newDoc.setPageBoxes({
			cBox: "Crop",
			nStart: i,
			rBox: cropRight
		});
	} else if (i % 3 == 1) {
	newDoc.setPageBoxes({
		cBox: "Crop",
		nStart: i,
		rBox: cropMiddle
	});
	} else {
		newDoc.setPageBoxes({
			cBox: "Crop",
			nStart: i,
			rBox: cropLeft
		});
	}
}

// save the new document
var re = /(.+)(\.\w+)$/;

var total = this.path.match(re);
var filename = total[1];
var extension = total[2];

var newName = filename + "-split" + extension;

newDoc.saveAs({
	cPath: newName
});
newDoc.closeDoc();

You can use this script by running it in the JavaScript console, in an Action, or in a Custom Command.

This entry was posted in Acrobat, JavaScript, Programming, Tutorial and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *