简体   繁体   中英

How to write javascript to reorder pages of a pdf document?

I have a double-sided document as two separate pdf files — front-facing pages in one document and rear-facing pages in the second.

front.pdf
rear.pdf

I have also combined them into a single document with all the pages but with all the front-facing pages before the rear-facing pages. The page ordering is of the form, {1,3,5,7,...,[n],2,4,6,8,...,[n-1 OR n+1]}

all.pdf

I wish to write a simple javascript that can be run from inside Adobe Abrobat X Pro. Ideally, it would count the pages of the document all.pdf , handle both occasion when there are either an odd or even number of total pages and then reorder them such that they are in their original order:

page [1>3>4>2] => page [1>2>3>4]

The tiny leading code snippet above is from the answer by user171577 on SuperUser in this question: https://superuser.com/questions/181596/software-that-merges-pdf-every-other-page

I was able to accomplish this following advice from NullUserException :

This script requires a document composed of all the odd pages followed by all the even pages. It will cope with cases where there are n even pages and n+1 odd pages.

I entered a 'Document JavaScript' called InterleavePages , with the following code:

function InterleavePages() {

var n = this.numPages;
var nOdd = Math.floor(n / 2);
var nEven = n - nOdd;
var x;
var y;
var i;

for(i = 0; i < nEven; i++) {
                         // movePage x, toAfterPage y
                         // note page numbers are 0-indexed
    x = nOdd + (i);      //
    y = i * 2     ;      //  
    this.movePage(x,y); 
   }
}
InterleavePages();

Thanks, this was a great help. Just wanted to point out the limitation I found is it only works as written with an even number of pages. Although it's probably possible to write a more sophisticated calculation script, I took the easy way out and just added a blank page to the end of my 17-page test document. I did, however, add an alert so I wouldn't forget to add an extra page when necessary...

function InterleavePages() {

var n = this.numPages;
var nOdd = Math.floor(n / 2);
var nEven = n - nOdd;
var x;
var y;
var i;

app.alert({
cMsg: "Total pages must be an even number",
cTitle: "Even Number of Pages Required!",
nIcon: 1, nType: 1
});

this.pageNum = 0;

for(i = 0; i < nEven; i++) {
                         // movePage x, toAfterPage y
                         // note page numbers are 0-indexed
    x = nOdd + (i);      //
    y = i * 2     ;      //  
    this.movePage(x,y); 
   }
}
InterleavePages(); 

As mentioned in some other exchanges, to interweave the pages two pdfs, you can use the Java console.

The first step is to combine the pdfs into a single document. I would do this by highlighting both files, and then right-clicking on one of them. There should be an option to "Combine supported files in Acrobat".

Then, once they are combined, open the combined file, where you want to run the code

for (i = 0; i <= this.numPages/2-1; i++) this.movePage(this.numPages-1,this.numPages/2-i-1);

The step-by-step details for running such code are:

1) Open the pdf.

2) Go to the second page. Doing this way will allow you to notice whether the change has taken place. You don't have to do this step, but it helps.

2) Press Control + J

3) In the window that pops up, I always go to the "View" Dropdown menu, and set it to "Script and Console".

4) In the bottom window, replace the text that should read something like

"Acrobat EScript Built-in Functions Version 10.0 Acrobat SOAP 10.0"

with

for (i = 0; i <= this.numPages/2-1; i++) this.movePage(this.numPages-1,this.numPages/2-i-1);

5) Press enter once. Pressing twice might run the code again (which you do not want).

6) Check you pdf to see if the pages have been interlaced. If not, try step 5 again.

7) You are now a Java wizard. Congratulations.

I had the same issue, my scanner was single sided only, and the scanner software, after being done with the auto-feeder, asked if there's more to scan. If you grab the stack, turn it over and feed it again, you'll end up with a single PDF where the n-page document is arranged as 1f, 2f, 3f ... nb, (n-1)b, (n-2)b ... 1b (f=front, b=back, 1-based page numbers). By definition you'd have an even number of scanned pages, the javascript for re-arranging the whole thing (careful, only works with even number of pages in this context!) is:

// rearrange the pages from single-side scanner, 0-based page# where
// pages 0 .. n/2-1 are front, pages n/2 .. n-1 are back
function Rearrange() {
    var tpn=0;      // target page number
    for (count = 0; count < this.numPages/2; count++)   { 
        this.movePage(this.numPages-1,tpn);
        tpn=tpn+2;
    }
}
Rearrange();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM