Author Topic: Add blank pages to all files to be divisible by 4  (Read 4443 times)

0 Members and 1 Guest are viewing this topic.

nightslayer23

  • Newbie
  • *
  • Posts: 98
Add blank pages to all files to be divisible by 4
« on: August 19, 2019, 07:15:34 AM »
Hi, needing to batch check hundreds of files and make them divisible by 4.
So if the page count is a multiple of 4, do nothing. If not a multiple of 4, add blank pages til they are.

could you help with this one please?

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Add blank pages to all files to be divisible by 4
« Reply #1 on: August 20, 2019, 12:51:48 AM »
Check if this script works for you.
Code: [Select]
var MultipleOf = 4;
var OverwriteSourceFile = false;

/******************************************************************************
Leave empty to save the changed files into the source folder.
Fill it with a folder path, e.g. var DestFolder ="c:\\PDFs\\Processed\\"; to
specify a fixed save to folder.*/
var DestFolder = "";
/******************************************************************************/

var Merger = pdfe.CreateDocumentMerger();
var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    var File = pdfe.SelectedFiles(i);
    var Filename = File.Filename;
    pdfe.echo(Filename);
    ProgressBar.position = i + 1;

    var BlanksNeeded = MultipleOf - File.NumPages % MultipleOf;
    if (BlanksNeeded < MultipleOf) {

        Merger.MergeDocument(Filename);

        var LastPage = File.pages(File.pages.count - 1);
        for (var n = 0; n < BlanksNeeded; n++)
        Merger.MergeBlankPage(LastPage.width, LastPage.height);

        File.close();

        if (DestFolder) {
            NewFilename = DestFolder + Filename.substr(Filename.lastIndexOf('\\') + 1);
        } else if (OverwriteSourceFile) {
            var NewFilename = Filename;
        } else {
            NewFilename = Filename.substring(0, Filename.lastIndexOf('.')) + '_' + (BlanksNeeded) + 'BlanksAdded.pdf';
        }
        Merger.EndAndSaveTo(NewFilename);

        pdfe.echo(' : ' + (BlanksNeeded) + ' blank pages added', 0, true);

    } else pdfe.echo(' : Already multiple of ' + MultipleOf, 0, true);
}
pdfe.echo('All done.');

nightslayer23

  • Newbie
  • *
  • Posts: 98
Re: Add blank pages to all files to be divisible by 4
« Reply #2 on: August 20, 2019, 02:39:22 AM »
great! works perfect.could you adjust to save new files to an output folder instead?
I've tried to do this myself but I'm confusing it HA ;D

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Add blank pages to all files to be divisible by 4
« Reply #3 on: August 21, 2019, 01:28:43 AM »
I've updated the above script with the possibility to specify an output folder.
Just change the DestFolder variable accordingly to your needs.