Skip to main content
Known Participant
April 20, 2018
Answered

Script to add a page to a PDF

  • April 20, 2018
  • 3 replies
  • 2002 views

I have a script that extracts and renames files that have been extracted.

What I require is during the process I wish to add a page to the beginning of the extracted Files and have it say something like, "Part 1 of 2".

var re = /\.pdf$/i;

var filename = this.documentFileName.replace(re,"");

   try {for (var i = 0; i < this.numPages; i+= 5)         this.extractPages({

            nStart: i,

            nEnd: (((i+4)>=this.numPages) ? this.numPages-1 : (i+4)),

            cPath: "/K/Penta/Split/"+filename+"_" +"Pages_" + (i+1) + "-" + (((i+5)>=this.numPages) ? this.numPages : (i+5))  +".pdf"

         });        

   } catch (e) { console.println("Aborted: " + e) }

The Script extracts a set number of pages and saves them with their new name

Message was edited by: Joshua Adams

This topic has been closed for replies.
Correct answer Thom Parker

var re = /\.pdf$/i;

var filename = this.documentFileName.replace(re,"");

var n = 1;

var nParts = Math.ceil(this.numPages/998);

   try {for (var i = 0; i < this.numPages; i+= 998){   

var oNewDoc = this.extractPages({

            nStart: i,

            nEnd: (((i+997)>=this.numPages) ? this.numPages-1 : (i+997)),

           

         });

oNewDoc.newPage({nPage: 0});

oNewDoc.addWatermarkFromText({cText: "Part "+n + " of " + nParts,

nStart: 0});

oNewDoc.saveAs({cPath: "/K/Penta/Split/"+filename+"_" +"Pages_" + (i+1) + "-" + (((i+998)>=this.numPages) ? this.numPages : (i+998))  +".pdf"});

n++;

}

   } catch (e) { console.println("Aborted: " + e) }

Work Just as I want, Thanks


Excellent!! How about marking a correct answer?

3 replies

Known Participant
April 23, 2018

Thank You Thom, I'm sorry for bothering you, I just need help with the newPage function is their a way, to make it say after extracts list what pages are in this file. Example: The File saves as img-322123814-0001_Pages_1-5 and extracts out of a 30 page document so I want it say in the new page Part 1 of 6​ or something along those lines?

Thom Parker
Community Expert
Community Expert
April 23, 2018

Yes, there are a few different ways to do this. The easiest is to add a form field to the bottom of the page and write the text into it.  Another method is to add a watermark to the page.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
April 24, 2018

Ok I found a function called addwatermarkfromtext, how could I implement it to count how many files the original was extracted to, and for every extracted file say ​Part 1 of 6​, or how many files it was extracted too

Thom Parker
Community Expert
Community Expert
April 21, 2018

The function for adding a new page is "doc.newPage()";

To use it you'll need to make a small adjustment to your script.  Remove the cPath argument from the "extractPages" function so it will return a doc object for the extracted pages. Add the new page function and follow it with a "doc.saveAs()" to path removed from "extractPages".

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
April 21, 2018

So if I remove the cpath and add the new function it will add a new page too the beginning of the extracted files, could you show me the modified script i cant access adobe pro at the moment

Thom Parker
Community Expert
Community Expert
April 21, 2018

Well, here are the JS reference entries for the 3 functions you need to implement this script.

Acrobat DC SDK Documentation

Acrobat DC SDK Documentation

Acrobat DC SDK Documentation

This script should look something like this

var oNewDoc = this.extractPages(...)

oNewDoc.newPage(...)

oNewDoc.saveAs(...)

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
April 20, 2018

I recommend you add those pages to the original file, and then extract them along with the rest of the pages.

Doing it after the fact is trickier.