Tuesday, August 12, 2014

The Coding Humanist: Apply Roman and Alphabetic Numbering to PDFs

No New Testament or textual criticism or both this time, just sharing some simple home-brewed javascript actions I use to make my PDF files more accessible.

An important element for my PDFs is page numbering: a typical book has front matter, part of which is not numbered and should in a PDF have "a", "b", ..., and part of which is usually numbered "i", "ii", ... The body matter itself then has the normal decimal numbering. But most PDF files do not come that way, which I find annoying.

In the full version of Acrobat so-called "Actions" can be defined, which can also execute javascript. It took me some time to figure out how it could be done, but the following works, thanks to the setPageLabels method.

The first script applies Roman numbering to the pages preceding the current one:
var myDecimalPage = this.pageNum;
this.setPageLabels(0, ["r", "", 1]);
this.setPageLabels(myDecimalPage, ["D", "", 1]); 
If you want upper case Roman numbering, use "R" instead of "r".

The second script applies alphabetic numbering to the pages preceding the current one; it assumes the PDF already has both Roman and decimal numbering, in that order:
var myRomanPage = this.pageNum;
var i = 0;
while(this.getPageLabel(i) != 1)
    {i++;}
myDecimalPage = i;
if (myRomanPage < 10)
    {this.setPageLabels(0, ["a", "", 1]);}
else
    {this.setPageLabels(0, ["A", "", 1]);}
this.setPageLabels(myRomanPage, ["r", "", 1]);
this.setPageLabels(myDecimalPage, ["D", "", 1]);
This script is a bit more complicated, for two reasons:
1. the setPageLabels method does not allow to specify a page range, but blindly applies the numbering from the first page that is given to the end of the PDF; for that reason, the script first has to find out which page actually has the normal number "1", which is done in the while statement;
2. I like to have lower case alphabetic numbering ("a", "b", ...) for the front matter, but that is not a good idea when lower case roman numbering is used and the front matter has 10 pages or more, because then "i" becomes ambiguous. If you apply different case to the alphabetic numbering and the Roman numbering, the if statement can be simplified.

So if you want to use these scripts, feel free (they are of course ohne Gewähr):
1. create the two actions in Acrobat;
2. go to the page that should have number "1", and apply the first script;
3. go to the page that should have number "i", and apply the second script.

No comments: