使用PDFTK将PDF分成多页? [英] Split PDF by multiple pages using PDFTK?
问题描述
我很难用这个短语来表达问题,也找不到我想做的在线解决方案.
I am finding it hard to phrase this question and could not find an online solution for what I'm trying to do.
我知道如何使用以下脚本将大型PDF分为具有PDFTK的单个页面:
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
但是现在我想按每个 other 页拆分PDF,以便每个新的PDF具有两(2)页(例如,第1 + 2页在一起,第3 + 4页在一起,第5 + 6等).
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.).
我知道Acrobat就像冠军一样,但是我需要可以从Powershell执行的东西.
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.
推荐答案
此PowerShell脚本将
This PowerShell script will
- 使用pdftk获取页数
- 逐步循环构建范围字符串
- 使用范围将页面提取到新的pdf中,并将范围附加到基本名称(并存储在同一文件夹中).
更改前两个变量以适合您的环境.
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.
样本输出
> .\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
这篇关于使用PDFTK将PDF分成多页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!