Author Topic: Script to add a blank page to pdf each n pages  (Read 35182 times)

0 Members and 1 Guest are viewing this topic.

Frank

  • Guest
Script to add a blank page to pdf each n pages
« on: June 13, 2013, 02:43:37 AM »
Hi,

We are using this wonderful tool everyday to merge the n pdf we receive in p pages groups (1pages.pdf, 2pages.pdf, ...)
Now what we would like to do is to use another scrip on every odd pages pdf to add a blank page every n pages...
Say we have a 3pages.pdf with 12 pages which is the merging of 4 pdf with 3 pages... (pdf1, pdf2, pdf3 and pdf 4 are 3 pages pdf and have been merged into a 12p pdf...
I would like to have a scrip that will add a blank page after every 3 pages 123 blank 456 blank 789 blank 10 11 12 blank and on and on...
In a perfect world, the script would read the number of pages between each blank insert on the file name (03pages.pdf would read 3, 11pages.pdf would read 11...)

My goal is to be able to print every pdf in duplex which I can do right only on even pages pdf or on odd pages pdf after have manualy added a blank pages after each sequence of n (n=odd number... )

I hope my poor english let me explain clearly what we are looking, and I hope too that what I want to do is possible.
I don't know a line about coding ! But more and more I think that I should dive in !!!

Anyway, thanks a lot for the feedback.

Frank

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #1 on: June 13, 2013, 04:46:26 PM »
Any inconvenient on "evening" these odd number of pages at the time you merge them with the merge script?

Something like this modified version of that script, that adds a separator blank page when the PDFs being merged have a odd number of pages.
Note:This script needs a blank page PDF at "c:\temp\blank.pdf".
Code: [Select]
//Full path to a blank page pdf
var BlankPagePDFFilename='c:\\temp\\blank.pdf';

//sort selected PDFs by same number of page documents
var ByPageArray = {};
for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    var n = pdfe.SelectedFiles(i).Pages.Count;
    if (!ByPageArray[n]) {
        ByPageArray[n] = new Array();
    }
    ByPageArray[n].push(pdfe.SelectedFiles(i).Filename);
}

//Create a new merger object
var Merger = pdfe.CreateDocumentMerger();

//Now merge all same number of pages documents
var ProgressBar = pdfe.ProgressBar;
for (var p in ByPageArray) {
    if (ByPageArray.hasOwnProperty(p)) {
        var a = ByPageArray[p];
        var addblank=isOdd(p);
        ProgressBar.max = a.length;
        for (var i = 0; i < a.length; i++) {
            var filename = a[i]
            pdfe.echo('Merging ' + filename);
            ProgressBar.position = i + 1;
            Merger.MergeDocument(filename);
            if (addblank) {
              Merger.MergeDocument(BlankPagePDFFilename);           
            }
        }
        Merger.EndAndSaveTo("C:\\Temp\\" + p + "pageDocs.pdf");
    }
}

function isOdd(n) {
    return n % 2
}

Frank

  • Guest
Re: Script to add a blank page to pdf each n pages
« Reply #2 on: June 15, 2013, 03:07:35 AM »
THANK YOU VERY MUCH !!!

It works like a charm.

First script + this one will save us tons of time !

Again, THANK YOU !!!


RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #3 on: June 16, 2013, 01:22:00 AM »
Because it may also be useful, here is a script that fulfill your request to insert the blank pages to the already merged PDFs.
Only processes files that originate from the merge of even number of pages PDFs.

Code: [Select]
//Full path to a blank page pdf
var BlankPagePDFFilename = 'c:\\temp\\Blank.pdf';
var getNumberRegEx = /^\d+/

// Create a new merger object
var Merger = pdfe.CreateDocumentMerger();

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;
for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    var FullPathFilename = pdfe.SelectedFiles(i).Filename;
    var Filename = FullPathFilename.substring(FullPathFilename.lastIndexOf('\\') + 1);
    var n = getNumberRegEx.exec(Filename);
    if (n && isOdd(Number(n[0]))) {
        var step = Number(n[0]);
        pdfe.echo('Processing ' + FullPathFilename);
        ProgressBar.position = i + 1;
        var pages = pdfe.SelectedFiles(i).Pages;
        for (j = 1; j <= pages.count; j += step) {
            var range = j.toString() + "-" + (j + step - 1).toString();
            Merger.MergeDocument(FullPathFilename, range);
            Merger.MergeDocument(BlankPagePDFFilename);
        }
        Merger.EndAndSaveTo("C:\\Temp\\evened-" + Filename);
    }
}

function isOdd(n) {
    return n % 2
}

ken

  • Guest
Re: Script to add a blank page to pdf each n pages
« Reply #4 on: September 10, 2013, 05:37:50 PM »
So I'd like to adapt the script to use the following way:
I have a large 1700 pages PDF from a customer which is a front and back for 850 names. I need to insert 2 blanks between the front and back so that it paginates and print correctly on our digital devices. If I have a PDF which is 2 blank pages, how specifically do I adapt the script so that it inserts every 2 pages within the 1700 page PDF?
page 1 (blank/blank) page 2, page 3 (blank/blank) page 4, page 5 (blank/blank) page 6, etc.

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #5 on: September 10, 2013, 08:29:49 PM »
Something like this should do the job.

Code: [Select]
//Full path to a 2 blank pages pdf
var BlankPagesPDFFilename = 'c:\\temp\\2BlankPages.pdf';

// Create a new merger object
var Merger = pdfe.CreateDocumentMerger();

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;
for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    var FullPathFilename = pdfe.SelectedFiles(i).Filename;
    var Filename = FullPathFilename.substring(FullPathFilename.lastIndexOf('\\') + 1);

    //insert each two pages rule
    var step = 2;

    pdfe.echo('Processing: ' + FullPathFilename);
    ProgressBar.position = i + 1;

    var pages = pdfe.SelectedFiles(i).Pages;

    Merger.MergeDocument(FullPathFilename, '1');

    for (j = 2; j <= pages.count; j += step) {

        Merger.MergeDocument(BlankPagesPDFFilename);

        var range = j.toString() + "-" + (j + step - 1).toString();
        Merger.MergeDocument(FullPathFilename, range);
    }

    //save the new file
    Merger.EndAndSaveTo("C:\\Temp\\2BlanksInserts_" + Filename);
    pdfe.echo('done');
}
pdfe.echo('finished');

Don't forget to change the two hard coded file paths to your needs.

Ravi

  • Guest
Re: Script to add a blank page to pdf each n pages
« Reply #6 on: February 06, 2015, 06:22:07 AM »
Hi,

1. How to use this Script in Adobe Acrobat 11 Pro?

2. I need to insert a blank page in between all pages in a pdf file, so what could be the Script?

Thanks,
Ravi

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #7 on: February 06, 2015, 06:47:37 PM »
Hi,

1. How to use this Script in Adobe Acrobat 11 Pro?

2. I need to insert a blank page in between all pages in a pdf file, so what could be the Script?

Thanks,
Ravi

Using the PDF-ShellTools, this forum is all about, scripting environment and automating Acrobat Pro to do the inserts, something like this perhaps?
Code: [Select]
var BlankPagePDF = "C:\\temp\\BlankPage.pdf";

var app = pdfe.CreateObject("AcroExch.App");
var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");
var insertDoc = pdfe.CreateObject("AcroExch.PDDoc");
insertDoc.open(BlankPagePDF);

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

var FullPathFilename;
var Filename;
var FileFolder;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    FullPathFilename = pdfe.SelectedFiles(i).Filename;
    Filename = FullPathFilename.substring(FullPathFilename.lastIndexOf('\\') + 1);
    FileFolder = FullPathFilename.substring(0, FullPathFilename.lastIndexOf('\\') + 1);
    pdfe.echo('Processing: ' + FullPathFilename);
    ProgressBar.position = i + 1;

    baseDoc.Open(FullPathFilename);
    n = baseDoc.GetNumPages();
    for (var j = isOdd(n) ? n : n - 1; j >= 0; j--) {
        baseDoc.InsertPages(j, insertDoc, 0, 1, 1);
    }

    baseDoc.Save(1, FileFolder + 'BlanksInserted_' + Filename);
    baseDoc.close();
}

insertDoc.close();
app.Exit();
pdfe.echo('finished');

function isOdd(n) {
    return n % 2
}


irshadcyber

  • Newbie
  • *
  • Posts: 1
Re: Script to add a blank page to pdf each n pages
« Reply #8 on: February 23, 2016, 03:37:50 AM »
HI... can i get a script / program only to add blank pages to all pdf files in a folder which has odd pages

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #9 on: February 23, 2016, 11:10:11 AM »
HI... can i get a script / program only to add blank pages to all pdf files in a folder which has odd pages

Check if this one is what you need:
Code: [Select]
// Create a new merger object
var Merger = pdfe.CreateDocumentMerger();

//set up the progress bar
var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    var File = pdfe.SelectedFiles(i);
    if (isOdd(File.NumPages)) {

        var Filename = File.Filename;

        pdfe.echo('Processing ' + Filename);
        ProgressBar.position = i + 1;

        //merge the full document
        Merger.MergeDocument(Filename);

        //add a blank page of the same size of the last one in the document   
        var LastPage = File.pages(File.pages.count - 1);
        Merger.MergeBlankPage(LastPage.width, LastPage.height);

        //save to a new file name
        var NoExtFilename = Filename.substring(0, Filename.lastIndexOf('.'));
        Merger.EndAndSaveTo(NoExtFilename + '_evenedup.pdf');

        pdfe.echo(' : done', 0, true);
    }
}
pdfe.echo('All done.');

/************************************************************/
function isOdd(n) {
    return n % 2
}

If the reason you need this functionality is to merge all these documents later, or print in batch, and have each document starting in a new paper sheet, then you may also use the merge tool, that has this functionality of adding a blank page when merging documents with an odd number of pages.

Mamal

  • Guest
Re: Script to add a blank page to pdf each n pages
« Reply #10 on: January 30, 2017, 07:58:52 PM »
Hi There, where do you run these scripts? I am new on these! I do have Adobe Standard XI. Cheers!

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Script to add a blank page to pdf each n pages
« Reply #11 on: January 30, 2017, 11:23:23 PM »
This ones you run on the scripts tool of the PDF-ShellTools.