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

Split PDF Files based on size

Community Beginner ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

I have a script that will split PDF files based on a set amount, so If I have a 1,000 page pdf, I have it split every 997 pages, I wish to modify it to split based on the actual size of the file.

So if the file is 1,000 pages read it and split it too 2 seperate 500 page documents or if it's a 1,200 page document split it into 5, 300 page documents, so my end goal is too split it evenly if the file is larger than 1,000 pages.

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: filename + "Part "+n + " of " + nParts,nStart: 0,nFontSize:36});

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

n++;

oNewDoc.closeDoc(true);

}

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.4K

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 , Jun 05, 2018 Jun 05, 2018

You're splitting 999 pages, not 600. I recommend you use a variable to hold the number of pages per file, so you would only need to change it once if you want to have a different value.

Votes

Translate

Translate
Community Expert ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

A 1200-page file can't be split into 5 files of 300 (unique) pages... You probably meant a 1500-page file. And you need to define a way of deciding why use one option and not another. Why not 2 files of 750 pages? Or 3 files of 500 pages? etc.

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Ok I've come up with a solution, I just need a help with one part, In order to split by different file sizes I will use a series of If statements based on the size of the file, what I need help with if I have a 1200 page document and I split by 550 towards the end I want to add those renaming pages to the last document as long as it doesn't exceed 998.

if(this.numPages >= 1000 && this.numPages <= 2000){

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

var oNewDoc = this.extractPages({nStart: i,nEnd: (((i+549)>=this.numPages) ? this.numPages-1 : (i+549))});

oNewDoc.newPage({nPage: 0});

oNewDoc.addWatermarkFromText({cText: filename + "Part "+n + " of " + nParts,nStart: 0,nFontSize:36});

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

n++;

oNewDoc.closeDoc(true);

}

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

   }

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Your code is incorrect. The definitions of a lot of variables are missing, such as: n, filename, nParts ...

Anyway, to do what you've described you can check if the number of pages in the file is divisible by 550 (use the modulus operator), and then use the value of (n*550) to get the last page number that you extracted. So the new range will be (n*550) to numPages.

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Sorry my bad, here's all the code

var re = /\.pdf$/i;

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

var n = 1;

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

if(this.numPages >= 1000 && this.numPages <= 2000){

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

var oNewDoc = this.extractPages({nStart: i,nEnd: (((i+549)>=this.numPages) ? this.numPages-1 : (i+549))});

oNewDoc.newPage({nPage: 0});

oNewDoc.addWatermarkFromText({cText: filename + "Part "+n + " of " + nParts,nStart: 0,nFontSize:12});

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

n++;

oNewDoc.closeDoc(true);

}

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

   }

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Your code already does what you asked for...

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Ok I'm sorry I haven't explained very well, but I got my script to split how I want it but it only creates one document so I split 1200 page document the first 600 are saved as a document, but ignores the second half, how do I fix this?

var re = /\.pdf$/i;

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

var n = 1;

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

var nleft = Math.round(this.numPages/nParts);

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

var oNewDoc = this.extractPages({nStart: i,nEnd: nleft -1});

oNewDoc.newPage({nPage: 0});

oNewDoc.addWatermarkFromText({cText: filename + "Part "+n + " of " + nParts,nStart: 0,nFontSize:12});

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

n++;

oNewDoc.closeDoc(true);

}

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

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 ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

LATEST

You're splitting 999 pages, not 600. I recommend you use a variable to hold the number of pages per file, so you would only need to change it once if you want to have a different value.

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