How to write javascript to reorder the pages of a PDF document?

I have a two-sided document as two separate PDF files - front-side pages in one document and back pages in the second.

front.pdf rear.pdf 

I also combined them into one document with all pages, but with all the front pages before the pages facing the back panel. The order of page placement is {1,3,5,7,...,[n],2,4,6,8,...,[n-1 OR n+1]}

 all.pdf 

I want to write simple javascript that can be run from Adobe Abrobat X Pro. Ideally, it will read the pages of the document all.pdf , handle both cases when there is either an odd or even number of common pages, and then reorder them so that they are in the original order:

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

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

+4
source share
3 answers

I managed to execute the following NullUserException tip:

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

I entered "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
source

Thanks, that was a great help. I just want to indicate which restriction I found, this only works with an even number of pages. Although it may be possible to write a more complex calculation script, I made a simple exit and simply added a blank page to the end of my 17-page test document. However, I added a warning, so I will not forget to add an extra page if 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(); 
+1
source

As mentioned in some other exchanges, you can use the Java console to bind the pages of two PDF files.

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

Then, once they are merged, open the merged 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);

Step-by-step details for running such code:

1) Open pdf.

2) Go to the second page. Performing this method will let you notice if a change has occurred. You do not need to take this step, but it helps.

2) Press Control + J

3) In the window that appears, I always go to the "View" drop-down menu and set it to "Script and console."

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

"Built-in Acrobat EScript 10.0 Acrobat SOAP 10.0 Features"

with

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

5) Press once. Pressing it again may run the code again (which you don't need).

6) Check that you have viewed pages to see if pages have been bound. If not, repeat step 5.

7) Now you are a Java master. Congratulations.

0
source

Source: https://habr.com/ru/post/1445114/


All Articles