How to write javascript to reorder pages of a pdf

2019-05-07 08:17发布

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

3条回答
成全新的幸福
2楼-- · 2019-05-07 08:56

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();
查看更多
我想做一个坏孩纸
3楼-- · 2019-05-07 08:58

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(); 
查看更多
甜甜的少女心
4楼-- · 2019-05-07 09:11

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.

查看更多
登录 后发表回答