Search code examples
javascriptpdfbatch-processingacrobat

Extracting multiple separate pages from a pdf


I have a 1590 page pdf and I'm looking to extract every 15th page starting with page 8.

I'm very inexperienced in javascript, how would I do something like this?

This is my code so far but I get an error.

this.extractPages(5, 8, 23, 38, 53, 68, 83 ,98, 113, 128, 143, 158, 173, 188, 203, 218, 233, 248, 263, 278, 293, 308, 323, 338, 353, 368, 383, 398, 413, 428, 443, 458, 473, 488, 503, 518, 533, 548, 563, 578, 593, 608, 623, 638, 653, 668, 683, 698, 713, 728, 743, 758, 773, 788, 803, 818, 833, 848, 863, 878, 893, 908, 923, 938, 953, 968, 983, 998, 1013, 1028, 1043, 1058, 1073, 1088, 1103, 1118, 1133, 1148, 1163, 1178, 1193, 1208, 1223, 1238, 1253, 1268, 1283, 1298, 1313, 1328, 1343, 1358, 1373, 1388, 1403, 1418, 1433, 1448, 1463, 1478, 1493, 1508, 1523, 1538, 1553, 1568, 1583, "TestExtract1.pdf")


Solution

  • You can use the following in the Acrobat JS console with the PDF open. Since it counts pages starting at 0 you need to start with 7, not 8.

    If you set the start page & interval as variables you can edit and reuse the script more easily.

    var start=7;
    var interval=15;
    
    for (var i=start; i<this.numPages; i+=interval) {     
          if(i<=this.numPages){
           console.println(i)
           this.extractPages(i); 
          }
          else {
            console.println("pg does not exist at " + i);
          }      
     }
    

    This opens each extracted page as a separate file, which you then have to rename and save to your desired location. Messed around w/that tutorial to rename & save files, but it doesn't work as explained anymore. Also not sure if the if/else check is necessary, but not sure what'd happen if adding 15pgs exceeds the length & it tries to extract a non-existent page.

    This should be enough to get you the pages you need. Will edit if I get the naming & saving part working. Adobe JS documentation is kind of a mess and this console is awful ...