将 PDF 文件拆分为多个同样页数 pdf 文件的 Windows Powershell 脚本

先从网上下载免费的 pdftk 命令行工具
官网 https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ 的安装版或者某些下载网站的绿色版都可以
先打开 Windows 的 Powershell
第一次使用需要开启 Powershell 执行策略,方法如下:
管理员运行 Powershell
执行命令 Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine

然后在 Powershell 中 cd 切换到到 pdftk 命令的目录
复制下面的脚本保存文件扩展名 ps1,例如为 test.ps1
修改下面脚本的 $pdfFile 中的 test.pdf 的值为 pdf 文件名(最好为英文)
$SetsOfPages 的值为要切分的页数
在 Powershell 中运行 .\test.ps1,然后查看输出过程和目录中新建的拆分后多个 pdf 文件吧

$pdfPath = '.\'
$pdfFile = Join-Path $pdfPath "test.pdf"
$SetsOfPages = 2
$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
}

参考 https://www.it1352.com/1619663.html ,本作者做了修改并测试成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值