重新排序“未预订” PDF 的页面 - Batch / GhostScript / PDFtk

重新排序“未预订” PDF 的页面 - Batch / GhostScript / PDFtk

序言:之前有人问过“取消预订”PDF。话虽如此,我还没有找到该过程这一特定部分的答案。多亏了广泛的研究和本网站的大量帮助,我才找到一半。我正在做的事情的完整解释和资源链接在本问题的末尾。


主要问题:(如果您愿意,请关注这一点并忽略其余部分)

我已经从一个“已编好”的 PDF 中创建了 2 个 PDF 文件。一个是原始 PDF 的左侧,一个是右侧。左侧页面的顺序如下:16, 2, 14, 4, 12, 6, 10, 8右侧页面的顺序如下:1, 15, 3, 13, 5, 11, 7, 9

我想实现正常的单页顺序(1, 2, 3, 4, 5... 16单一输出文件Batch使用、Ghostscript或的任意组合PDFtk

使用 PDFtk 或 GhostScript 以及可能某种形式的批量 for 循环,我需要:

  1. (也许)确定需要处理的总页数(?)
  2. 循环遍历它们并按照正确的顺序从两个文档中编译出一个输出文件

进一步的问题细节和测试资源:

可能会找到类似/部分解决方案这里但是,我对 Python 了解不够多,无法解释正在发生的事情或对其进行测试。

测试页码.pdf- 这是一个示例“预订” PDF 的链接,这是我开始使用的

重要提示——以上内容是为了清晰起见而添加的。拆分上述 PDF 超出了本问题的范围,之前已经回答过。有关详细信息,请参阅本问题底部部分。

左侧部分.pdf- 这里只截取左侧部分进行测试

右侧部分.pdf- 这里只是截断的右侧用于测试

期望输出.pdf- 期望的输出如下

下表显示了正确的顺序: 正确顺序及文件表


我已经做了什么

该脚本合并了 PDF,但是顺序全都是乱的。

pdftk ^
  A=right-sections.pdf ^
  B=left-sections.pdf ^
  shuffle A B ^
  output single-pages-output.pdf

该脚本告诉我页数(输出:)NumberOfPages: 8,但是我花了很长时间才使用纯批处理脚本在循环中隔离出数字for,这是我的首选方法,因此我可以在多台机器上运行它而无需安装额外的库。

SET d=test
pdftk left-sections.pdf dump_data | findstr NumberOfPages>pgs
set /p d= < pgs
echo %d%

这是我尝试隔离数字。但目前还不起作用。我不知道这一步是否必要。可能还有另一种创造性的解决方案,也许使用循环while,看看errorlevel

for %%f in ("%d:\=" "%") do for /f %%n in ('echo %%f^|findstr /b /e /r "\"[0-9]*\""') do (
    echo %%~n
)

该脚本(基于:如何将 PDF 页面从中间拆分?将 PDF 每页 2 面转换为每页 1 面如何使用 Ghostscript 设置自定义页面大小) 将“已预订”的 PDF 拆分为两边。这超出了问题的范围,但是为了保持连续性和清晰度而添加的

::Left Sections

gswin32c.exe ^
    -o left-sections.pdf ^
    -sDEVICE=pdfwrite ^
    -g5040x6120 ^
    -c "<</PageOffset [0 0]>> setpagedevice" ^
    -f Test_PageNumbers.pdf -q

::Right Sections

gswin32c.exe ^
    -o right-sections.pdf ^
    -sDEVICE=pdfwrite ^
    -g5040x6120 ^
    -c "<</PageOffset [-504 0]>> setpagedevice" ^
    -f Test_PageNumbers.pdf -q

最后说明

一旦我有了这最后一部分,我就可以(我认为)编译一个批处理程序来处理文件并输出最终的 PDF。我不是(!)要求从头到尾完成整个批处理程序。这个问题只解决将页面重新排序为一个合并的 PDF,其中页面按顺序排列

谢谢你!

答案1

@echo off

setlocal enabledelayedexpansion

2>nul del /q /f /a: "%~dp0OutPut.pdf"
set /a "_cnt=0, _pag=0" & set "_cat=" & set "_file="
set "_pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"

for /f tokens^=* %%i in ('dir /on /b /a-d "%~dp0*-*.pdf"'
    )do for /f "tokens=2delims=: " %%p in ('""!_pdftk!" "%%~i" dump_data^|find "Pages:""'
    )do if "!_file!" == ""  (set "_file=%%~nxi")else (set "_pag=%%~p" & set "_file=!_file!,%%~nxi")

for /l %%L in (1,2,!_pag!)do if %%~L equ !_pag! (set "_cat=!_cat!R%%~L !_cat!L%%~L "
    )else set /a "_cnt=%%~L+1" && call set "_cat=!_cat!R%%~L L!_cnt! "
    
for /l %%L in (!_pag!,-2,1)do set /a "_cnt=%%~L-1" && call set "_cat=!_cat!R%%~L L!_cnt! "
cmd.exe /v /s /c ""!_pdftk!" R="!_file:*,=!" L="!_file:,%_file:*,=%=!" cat !_cat! OutPut "%~dp0OutPut.pdf""

endlocal

您可能需要多个(或两个) for 循环来执行此操作.. d;)


最后说明

一旦我有了这最后一部分,我就可以(我认为)编译一个批处理程序来处理文件并输出最终的 PDF。我并不是要求从头到尾完成整个批处理程序。这个问题只是解决将页面重新排序为一个页面有序的组合 PDF 的问题。

谢谢你!

  • 创建并用于以下批次的目的是监视和测试循环内外变量的行为以及生成的文件......
@echo off

setlocal enabledelayedexpansion

2>nul del /q /f /a: "%~dp0OutPut.pdf"
set /a "_cnt=0, _pag=0" & set "_cat=" & set "_file="
set "_pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"

for /f tokens^=* %%i in ('dir /on /b /a-d "%~dp0*-*.pdf"'
    )do for /f "tokens=2delims=: " %%p in ('""!_pdftk!" "%%~i" dump_data^|find "Pages:""'
    )do if "!_file!" == ""  (set "_file=%%~nxi")else (set "_pag=%%~p" & set "_file=!_file!,%%~nxi")

for /l %%L in (1,2,!_pag!)do if %%~L equ !_pag! (set "_cat=!_cat!R%%~L !_cat!L%%~L "
    )else set /a "_cnt=%%~L+1" && call set "_cat=!_cat!R%%~L L!_cnt! "
    
for /l %%L in (!_pag!,-2,1)do set /a "_cnt=%%~L-1" && call set "_cat=!_cat!R%%~L L!_cnt! "
cmd.exe /v /s /c ""!_pdftk!" R="!_file:*,=!" L="!_file:,%_file:*,=%=!" cat !_cat! OutPut "%~dp0OutPut.pdf""

echo\Your range pages: cat !_cat!
echo\Your cat command: pdftk R=input_#1.pdf L=input_#2.pdf cat ^^!_cat^^! OutPut.pdf & echo\
for /f tokens^=*useback %%i in (`type "%~0"^|findstr /l "!_cat!"`)do set /p "'=%%~i"<nul & echo\

endlocal

:: R1 <==> "Right-sections.pdf == Page 1 <==> OuPut.pdf Page 01 :: 
:: L2 <==>  "Left-sections.pdf == Page 2 <==> OuPut.pdf Page 02 :: 
:: R3 <==> "Right-sections.pdf == Page 3 <==> OuPut.pdf Page 03 :: 
:: L4 <==>  "Left-sections.pdf == Page 4 <==> OuPut.pdf Page 04 :: 
:: R5 <==> "Right-sections.pdf == Page 5 <==> OuPut.pdf Page 05 :: 
:: L6 <==>  "Left-sections.pdf == Page 6 <==> OuPut.pdf Page 06 :: 
:: R7 <==> "Right-sections.pdf == Page 7 <==> OuPut.pdf Page 07 :: 
:: L8 <==>  "Left-sections.pdf == Page 8 <==> OuPut.pdf Page 08 :: 
:: R8 <==> "Right-sections.pdf == Page 8 <==> OuPut.pdf Page 09 :: 
:: L7 <==>  "Left-sections.pdf == Page 7 <==> OuPut.pdf Page 10 :: 
:: R6 <==> "Right-sections.pdf == Page 6 <==> OuPut.pdf Page 11 :: 
:: L5 <==>  "Left-sections.pdf == Page 5 <==> OuPut.pdf Page 12 :: 
:: R4 <==> "Right-sections.pdf == Page 4 <==> OuPut.pdf Page 13 :: 
:: L3 <==>  "Left-sections.pdf == Page 3 <==> OuPut.pdf Page 14 :: 
:: R2 <==> "Right-sections.pdf == Page 2 <==> OuPut.pdf Page 15 :: 
:: L1 <==>  "Left-sections.pdf == Page 1 <==> OuPut.pdf Page 16 :: 
  • 输出
Your range pages: cat R1 L2 R3 L4 R5 L6 R7 L8 R8 L7 R6 L5 R4 L3 R2 L1
Your cat command: pdftk R=input_#1.pdf L=input_#2.pdf cat !_cat! OutPut.pdf

:: R1 <==> "Right-sections.pdf == Page 1 <==> OuPut.pdf Page 01 ::
:: L2 <==>  "Left-sections.pdf == Page 2 <==> OuPut.pdf Page 02 ::
:: R3 <==> "Right-sections.pdf == Page 3 <==> OuPut.pdf Page 03 ::
:: L4 <==>  "Left-sections.pdf == Page 4 <==> OuPut.pdf Page 04 ::
:: R5 <==> "Right-sections.pdf == Page 5 <==> OuPut.pdf Page 05 ::
:: L6 <==>  "Left-sections.pdf == Page 6 <==> OuPut.pdf Page 06 ::
:: R7 <==> "Right-sections.pdf == Page 7 <==> OuPut.pdf Page 07 ::
:: L8 <==>  "Left-sections.pdf == Page 8 <==> OuPut.pdf Page 08 ::
:: R8 <==> "Right-sections.pdf == Page 8 <==> OuPut.pdf Page 09 ::
:: L7 <==>  "Left-sections.pdf == Page 7 <==> OuPut.pdf Page 10 ::
:: R6 <==> "Right-sections.pdf == Page 6 <==> OuPut.pdf Page 11 ::
:: L5 <==>  "Left-sections.pdf == Page 5 <==> OuPut.pdf Page 12 ::
:: R4 <==> "Right-sections.pdf == Page 4 <==> OuPut.pdf Page 13 ::
:: L3 <==>  "Left-sections.pdf == Page 3 <==> OuPut.pdf Page 14 ::
:: R2 <==> "Right-sections.pdf == Page 2 <==> OuPut.pdf Page 15 ::
:: L1 <==>  "Left-sections.pdf == Page 1 <==> OuPut.pdf Page 16 ::

其他资源:

相关内容