admin管理员组

文章数量:1332395

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 bined 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:

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 bined 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./questions/181596/software-that-merges-pdf-every-other-page

Share Improve this question edited Mar 20, 2017 at 10:04 CommunityBot 11 silver badge asked Nov 9, 2012 at 17:46 JustinJDaviesJustinJDavies 2,6934 gold badges32 silver badges54 bronze badges 3
  • Hi. You can not use javascript. If you can use Java then there is a library called iText library. Which you can use for the same. api.itextpdf. – Dhruvenkumar Shah Commented Nov 9, 2012 at 20:07
  • 2 @yms Yes, you can. There are two ways to do this: you could use Doc.insertPages() to add pages from one PDF to another, or if all your pages are already in a single PDF you can use Doc.movePage() to move the pages around. Here's a link to the API reference: adobe./content/dam/Adobe/en/devnet/acrobat/pdfs/… – NullUserException Commented Jan 8, 2013 at 4:58
  • 1 @DhruvenkumarShah You can do pretty much anything you can do on Acrobat using the GUI interface via the JavaScript API - including moving pages around. – NullUserException Commented Jan 8, 2013 at 5:05
Add a ment  | 

4 Answers 4

Reset to default 3

I was able to acplish this following advice from NullUserException :

This script requires a document posed 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 bine 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 bined, open the bined 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();

本文标签: How to write javascript to reorder pages of a pdf documentStack Overflow