Skip to content

Instantly share code, notes, and snippets.

@anyong
Created August 23, 2015 17:02
Show Gist options
  • Save anyong/f6a4e8e84719cc0f39cb to your computer and use it in GitHub Desktop.
Save anyong/f6a4e8e84719cc0f39cb to your computer and use it in GitHub Desktop.
Split Adobe PDF pages down the middle or with an offset, and exclude variable number of pages from the front of doc.
cut = function(offset, start)
{
start = start || 1;
offset = offset || 0;
// create a new document
app.beginPriv();
var newDoc = app.newDoc();
app.endPriv();
// get the filename of our current file
var i = 0;
while (i < start - 1) {
newDoc.insertPages( {
nPage: newDoc.numPages - 1,
cPath: this.path,
nStart: i
});
i++;
}
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
});
// we did this twice so that we can then split each copy of the page into a left
// and right half.
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 twice
for (i=start - 1; i<newDoc.numPages; i++)
{
// determine the crop box of the page
var cropRect = newDoc.getPageBox("Crop", i);
var halfWidth = (cropRect[2]-cropRect[0])/2;
var cropLeft = new Array();
cropLeft[0] = cropRect[0];
cropLeft[1] = cropRect[1];
cropLeft[2] = cropRect[0] + halfWidth + offset;
cropLeft[3] = cropRect[3];
var cropRight = new Array();
cropRight[0] = cropRect[2] - halfWidth + offset;
cropRight[1] = cropRect[1];
cropRight[2] = cropRect[2];
cropRight[3] = cropRect[3];
if ((start-1) % 2 == 0) {
if (i%2 == 0) {
newDoc.setPageBoxes( {
cBox: "Crop",
nStart: i,
rBox: cropLeft
});
} else {
newDoc.setPageBoxes( {
cBox: "Crop",
nStart: i,
rBox: cropRight
});
}
} else {
if (i%2 == 1) {
newDoc.setPageBoxes( {
cBox: "Crop",
nStart: i,
rBox: cropLeft
});
} else {
newDoc.setPageBoxes( {
cBox: "Crop",
nStart: i,
rBox: cropRight
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment