• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to add a page to a PDF

Community Beginner ,
Apr 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 24, 2018 Apr 24, 2018

Excellent!! How about marking a correct answer?

Votes

Translate

Translate
Community Expert ,
Apr 20, 2018 Apr 20, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2018 Apr 21, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 21, 2018 Apr 21, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2018 Apr 21, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

Hi Tom I want to thank you for your help, I almost have it working, I just need your advice on why Adobe Javascript keeps telling me I'm missing a set of : when I know I'm not, also If you have any other suggestions to fix my script I'm open to suggestions

var re = /\.pdf$/i;

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

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

 

   var oNewDoc = this.extractPages({

            nStart: i,

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

         });

oNewDoc.newPage();

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

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

I can't test my script until I figure why Adobe Javascript is being diffcult

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

The "oNewDoc.saveAs" line is incorrectly formed. Remove "cPath:". This syntax is only used to indicate a member of an object, which is one way to pass parameters into a function. But in this case your missing the rest of the object definition syntax.  Doesn't matter anyway cause you've only got one input. So just remove the offending bit.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2018 Apr 23, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

The watermark function just adds fixed text. You need to do any counting and make the string labels in the normal way, before watermarking. You do the extracting so you can count each one.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

In your script, add another increment variable in the loop for keeping track of the number of sections. Then this can be used to create the watermark text. Something like this.

var n = 1;

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

for(....)

{

  ....

oNewDoc = ....

....

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

oNewDoc.saveAs...

}

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2018 Apr 24, 2018

Copy link to clipboard

Copied

LATEST

Excellent!! How about marking a correct answer?

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines