Collating PDFs using JavaScript

Planet PDF’s Introduction to Acrobat JavaScript Learning Center defines JavaScript as a scripting language that provides ‘…the ability to automate and maintain…PDF documents from within Acrobat.’ In this and other JavaScript tips, I will explain just how to use Acrobat JavaScript to do just that.

Although the end result is convenient and space-efficient, there are many potential issues when considering the conversion of paper documents into electronic archives. For instance, when scanning a large volume of paper pages into PDF with an Automatic Document Feeding (ADF) scanner, it’s common to end up with two separate PDF files: one with all of the odd-numbered pages, and another with the even-numbered ones. Depending on the page count of the document or documents in question, it can be a laborious and time-consuming task to collate the two files manually using Adobe Acrobat.

Here’s helpful script for collating two PDFs:

Note:
Although I originally wrote this to be executed via a custom tool button using ARTS PDF Aerialist, you can also execute it from Acrobat Professional’s JavaScript debugger.


/*
Title: Collate Document
Purpose: User is prompted to select document to insert/collate.
Author: Sean Stewart, ARTS PDF, http://www.debenu.com/arts-pdf/
*/


// create an array to use as the rect parameter in the browse for field

var arRect = new Array();
arRect[0] = 0;
arRect[1] = 0;
arRect[2] = 0;
arRect[3] = 0;

// create a non-visible form field to use as a browse for field

var f = this.addField('txtFilename', 'text', this.numPages - 1, arRect);

f.delay = true;
f.fileSelect = true;
f.delay = false;

// user prompted to select file to collate the open document with

app.alert('Select the PDF file to merge with')

// open the browse for dialog

f.browseForFileToSubmit();
var evenDocPath = f.value;

var q = this.numPages;

// insert pages from selected document into open document

for (var i = 0;i < q; i++) {
var j = i*2;
this.insertPages(j, evenDocPath, i);
}

// remove unused field

this.removeField('txtFilename');

You May Also Like

About the Author: Sean Stewart

Leave a Reply