Split PDF by multiple pages using PDFTK?

merge, pdf, pdftk, powershell, split

Solution

This PowerShell script will

- use pdftk to get the number of pages

- loop in steps building a range string

- use the range to extract the pages into a new pdf with appended range to the base name (and store in the same folder).

Change the first two vars to fit your environment.

## Q:\Test\2017\05\06\Split-Pdf.ps1
$pdfPath = 'Q:\Test\2017\05\06\'
$pdfFile = Join-Path $pdfPath "test.pdf"
$SetsOfPages = 3
$Match = 'NumberOfPages: (\d+)'
$NumberOfPages = [regex]::match((pdftk $pdfFile dump_data),$Match).Groups[1].Value
"{0,2} pages in {1}" -f $NumberOfPages, $pdfFile

for ($Page=1;$Page -le $NumberOfPages;$Page+=$SetsOfPages){
  $File = Get-Item $pdfFile
  $Range = "{0}-{1}" -f $page,[math]::min($Page+$SetsOfPages-1,$NumberOfPages)
  $OutFile = Join-Path $pdfPath ($File.BaseName+"_$Range.pdf")
  "processing: {0}" -f $OutFile
  pdftk $pdfFile cat $Range output $OutFile
}

Edited to work with variable sets of pages and to properly handle the overhang. Edited again: found a much easier way do shorten the last set of pages.

Sample output

> .\Split-Pdf.ps1
10 pages in Q:\Test\2017\05\06\test.pdf
processing: Q:\Test\2017\05\06\test_1-3.pdf
processing: Q:\Test\2017\05\06\test_4-6.pdf
processing: Q:\Test\2017\05\06\test_7-9.pdf
processing: Q:\Test\2017\05\06\test_10-10.pdf

Problem

I am finding it hard to phrase this question and could not find an online solution for what I'm trying to do. I know how to split a large PDF into single pages with PDFTK using the following script: ``` pdftk your_file.pdf burst output your_directory/page_%02d.pdf ``` But now I want to split the PDF by every other page, so that each new PDF has TWO (2) pages (e.g. pages 1 + 2 together, pages 3 + 4 together, 5 + 6, etc.). I know that Acrobat does this like a champ, however I need something I can execute from Powershell. I am open to alternatives/workarounds, like taking the single pages and combining them by two's after single bursting.

Original source