Tips & Tricks

Collating PDFs using JavaScript

December 08, 2004


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,

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

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


(ARTS PDF and Planet PDF have been since acquired by Debenu)

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Five visions of a PDF Day

In the world of PDFs or as we like to say Planet (of) PDF, a year isn't a real PDF year without an intense few days of industry knowledge sharing.

May 15, 2018
Platinum Sponsor

Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.


Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.