Author Topic: Split multi-page & multi-page.size PDF into respective sizes  (Read 7711 times)

0 Members and 1 Guest are viewing this topic.

nightslayer23

  • Newbie
  • *
  • Posts: 94
Split multi-page & multi-page.size PDF into respective sizes
« on: August 03, 2017, 02:20:04 AM »
Hey bud,

So I have a need for splitting Mulitpage pdfs that have multiple page sizes right, but I need to split PDF results to be in respect to page sizes.

So, eg: 300page PDF. A4 & A3.

Pages 1-100 are A4
Pages 101-149 are A3
Pages 150-300 are A4

Desired Result:
Filenames:
01 (original filename).pdf - containing pages 1-100
02 (original filename).pdf - containing the next size being A3 101-149
03 (original filename).pdf - containing the remaining pages of A4

Could this be a script you could help me with? It would be some sort of edit to the original split pdf function? I am just not up with how to code is all :S

RTT

  • Administrator
  • *****
  • Posts: 907
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #1 on: August 04, 2017, 01:05:15 AM »
This should do the job ;)
Code: [Select]
var Merger = pdfe.CreateDocumentMerger();
var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    ProgressBar.position = i + 1;
    try {
        var PreviousPSizeStr = '',
            SplitGroupIndex = 1,
            StartIndex = 1;
        var File = pdfe.SelectedFiles(i),
            Filename = File.Filename,
            Pages = File.Pages;
        var Path = Filename.substr(0, Filename.lastIndexOf('\\') + 1),
            Name = Filename.substring(Path.length, Filename.lastIndexOf('.'));

        for (var PageIndex = 0; PageIndex < Pages.Count; PageIndex++) {
            var Page = Pages(PageIndex);
            if (Page) {
                //Note: this rotation angle check should be removed for versions>3.1.x
                if (Page.Rotation == 90 || Page.Rotation == 270) {
                    var w = Page.Height;
                    var h = Page.Width;
                } else {
                    var w = Page.Width;
                    var h = Page.Height;
                }

                var PSizeStr = w.toFixed() + 'x' + h.toFixed();
                if (PageIndex == 0) {
                    PreviousPSizeStr = PSizeStr
                } else if (PreviousPSizeStr != PSizeStr) {
                    Split(Filename, Path + pad(SplitGroupIndex, 2) + ' (' + Name + ').pdf', StartIndex, PageIndex);

                    PreviousPSizeStr = PSizeStr;
                    StartIndex = PageIndex + 1;
                    SplitGroupIndex++;
                }
            }
        }
        if (StartIndex <= Pages.Count) //split remaining
        Split(Filename, Path + pad(SplitGroupIndex, 2) + ' (' + Name + ').pdf', StartIndex, Pages.Count);
    } catch (e) {
        pdfe.echo(Filename + ' :' + e.message, 0xFF0000);
    }
}

pdfe.echo("Done");

function Split(Filename, NewFilename, StartPage, EndPage) {
    if (Merger.MergeDocument(Filename, StartPage + '-' + EndPage) && Merger.EndAndSaveTo(NewFilename)) {
        pdfe.echo(NewFilename + ' [OK]', 0)
    } else {
        pdfe.echo(NewFilename + ' [Failed]', 0xFF0000);
    }
}

function pad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

conrad.drake

  • Newbie
  • *
  • Posts: 35
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #2 on: September 11, 2017, 06:31:47 AM »
A variation on the theme: I have some scanned documents, which are a mix of A4 and A3 sheets - the A3 being two pages.

Any suggestions on how I might split the A3 sheets into two A4 sheets?   I could probably fake it with PDF cropping. 

Right now, the scan is a JPEG so my current workflow will be to extract the images and cut these in half with my favourite image editor.

I suspect this is a whole new can-o-worms rather than a simple script.  Any suggestions?

RTT

  • Administrator
  • *****
  • Posts: 907
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #3 on: September 12, 2017, 01:54:07 AM »
Using the cropping idea, the next script creates a new PDF with all the A3 pages split into two A4. The split is done by adjusting the media and crop boxes and duplication of the page reference.
Code: [Select]
var Merger = pdfe.CreateDocumentMerger();
var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    ProgressBar.position = i + 1;
    try {
        var File = pdfe.SelectedFiles(i),
            Filename = File.Filename,
            Pages = File.Pages;
        var Path = Filename.substr(0, Filename.lastIndexOf('\\') + 1),
            Name = Filename.substring(Path.length, Filename.lastIndexOf('.'));

        pdfe.echo('>' + Filename);

        for (var PageIndex = 0; PageIndex < Pages.Count; PageIndex++) {
            var Page = Pages(PageIndex);
            if (Page) {

                var w = Math.min(Page.Width, Page.Height);
                var h = Math.max(Page.Width, Page.Height);

                //A3=297x420
                if (Math.abs(w - 297) < 15 && Math.abs(h - 420) < 15) {
                    pdfe.echo('  Splitting page ' + (PageIndex + 1).toString());
                    var box = Page.CropBox;
                    if (box.Right - box.Left < box.Top - box.Bottom) {
                        if (Page.Rotation == 90) {
                            var oriBoxTop = box.Top;
                            box.Top = box.Top + (box.Bottom - box.Top) / 2;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                            Merger.MergePage(Page);

                            box.Bottom = box.Top;
                            box.Top = oriBoxTop;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                        } else {
                            var oriBoxBottom = box.Bottom;
                            box.Bottom = box.Top + (box.Bottom - box.Top) / 2;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                            Merger.MergePage(Page);

                            box.Top = box.Bottom;
                            box.Bottom = oriBoxBottom;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                        }
                    } else {
                        if (Page.Rotation == 270) {
                            var oriBoxLeft = box.Left;
                            box.Left = box.Left + (box.Right - box.Left) / 2;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                            Merger.MergePage(Page);

                            box.Right = box.Left;
                            box.Left = oriBoxLeft;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                        } else {
                            var oriBoxRight = box.Right;
                            box.Right = box.Left + (box.Right - box.Left) / 2;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                            Merger.MergePage(Page);

                            box.left = box.right;
                            box.right = oriBoxRight;
                            Page.MediaBox = box;
                            Page.CropBox = box;
                        }
                    }

                }
                Merger.MergePage(Page);
            }
        }

        var NewFilename = Path + Name + '_A3To2A4.pdf';
        if (Merger.EndAndSaveTo(NewFilename)) {
            pdfe.echo('  Saving to: ' + NewFilename + ' [OK]', 0)
        } else {
            pdfe.echo('  Saving to: ' + NewFilename + ' [Failed]', 0xFF0000);
        }

    } catch (e) {
        pdfe.echo(Filename + ' :' + e.message, 0xFF0000);
    }
}

pdfe.echo("Done");

conrad.drake

  • Newbie
  • *
  • Posts: 35
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #4 on: September 21, 2017, 07:40:02 AM »
Thanks - I'll give that a go (I've been a little busy for the past week ro so!)

Braud

  • Newbie
  • *
  • Posts: 1
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #5 on: October 13, 2017, 02:59:22 PM »
Thanks - I'll give give Har Vokse a go as I've been a little busy for the past week ro so!

This works like a charm. Thank you RTT!

conrad.drake

  • Newbie
  • *
  • Posts: 35
Re: Split multi-page & multi-page.size PDF into respective sizes
« Reply #6 on: January 12, 2018, 05:23:41 AM »
This works like a charm. Thank you RTT!
+1
I had a junior staffer use it last time, today the pleasure was all mine.  A few things to learn from that script!