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.

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

  1. John Bernhardt says:

    Thanks for the articles on this. I’ve customized it for my needs, but the essential parts are the same. I’d like to be able for other people to use it even if they have Reader DC. It works great in Acrobat 9 Pro, but I cannot get it to work in Reader DC. I’m on Windows 10. Just to test if I got something wrong, I’ve tried it with your script also and I’ve gotten the same results.

    Error message “NotAllowedError: Security settings prevent access to this property or method. App.newDoc”

    I’ve tried to put the script in trusted user and app javascript folders. I’ve tried ticking and unticking every security feature I could find. No luck. Really I’ve tried anything and everything I could find on the internet and I’m stumped. Do you have any advice or could you point me in the right direction. Thanks.

  2. Karl Heinz Kremer says:

    John, the “Reader” is just what the name implies: It reads PDF documents (at least for the most part). It lacks almost all of the features that Acrobat has to modify PDF files. The page splitter works by heavily modifying a PDF file, so it’s no surprise that Reader will not do that for you. You need Adobe Acrobat Standard or Pro.

  3. John Bernhardt says:

    Okay understood. Thank you.

Leave a Reply

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