Author Topic: Script to merge N pdf files into n files regrouped by number of pages  (Read 6895 times)

0 Members and 1 Guest are viewing this topic.

Frank

  • Guest
Hello,

Every month we receive lots of rar files, each containing lots of PDFs...
The monthly routine is to unrar every file into a folder, then sort the PDFs by number of pages and then merge all X pages PDF into a new XpageDOCS.pdf
It takes a LOOOOOOOT of time !

I am pretty sure a script could be written so when I select all the PDF inside any uncompressed folder (folder with lots of PDFs), I could execute the script who would do this.

I am not a programmer but could ask or pay for someone to do it for me if it is possible with PDF ShellTools which by the way if by FAR the most reliable and robust PDF tool software we have been using so far (and believe me, we have tried a lots of one before ending up with it, for our great pleasure !)

Anyway, thanks a lot for your time.

Frank


RTT

  • Administrator
  • *****
  • Posts: 907
Re: Script to merge N pdf files into n files regrouped by number of pages
« Reply #1 on: March 27, 2013, 12:41:39 AM »
If I'm understanding it correctly, if you have some PDFs with 10 pages, some other with 15 pages and the rest with 20 pages, you will get 3 new documents that merge all the same number of pages in a unique one, i.e. 10PageDocs.pdf, 15PageDocs.pdf and 20PageDocs.pdf?
Any specific order needed between same page documents? Alphabetical, creation date, etc.?


Frank

  • Guest
Re: Script to merge N pdf files into n files regrouped by number of pages
« Reply #2 on: March 27, 2013, 01:27:15 AM »
That's quick !!!

Thank you very much.

It's exactly what you describe.
For the order, no we don't need any specific order, except obviously for the order of the pages inside say a 4 pages docs (P1DOC1,P2DOC1,P3DOC1,P4DOC1)

Again, thanks a lot !

RTT

  • Administrator
  • *****
  • Posts: 907
Re: Script to merge N pdf files into n files regrouped by number of pages
« Reply #3 on: March 27, 2013, 01:42:31 AM »
Then the next MyScripts script, in JScript, will do that easily ;)
The merge documents are created at a "C:\Temp" folder, so make sure that folder exists, or change the script accordingly to your needs.

Code: [Select]
//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 page documents
var ProgressBar = pdfe.ProgressBar;
for (var p in ByPageArray) {
    if (ByPageArray.hasOwnProperty(p)) {
        var a = ByPageArray[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);
        }
        Merger.EndAndSaveTo("C:\\Temp\\" + p + "pageDocs.pdf");
    }
}

Frank

  • Guest
Re: Script to merge N pdf files into n files regrouped by number of pages
« Reply #4 on: March 27, 2013, 04:29:06 AM »
THANK YOU !!!

Really nice !

You just saved me 1 hour a month !!