1

Preamble: 'Unbooking' a PDF has been asked before. That said, I haven't found an answer to this specific part of the process. Thanks to extensive research and a lot of help from this site, I'm halfway there. Full explanation of what I'm doing and links to resources at the end of this question.


Primary Question: (focus on this and ignore the rest, if you want)

I have, out of one "booked" PDF, managed to create 2 PDF files. One is the left side, and one is the right side of the original PDF. The left side's pages are in this order: 16, 2, 14, 4, 12, 6, 10, 8 and the right side's pages are in this order: 1, 15, 3, 13, 5, 11, 7, 9.

I want to achieve normal single-page order (1, 2, 3, 4, 5... 16) in a single output file using any combination of Batch, Ghostscript, or PDFtk.

Using either PDFtk or GhostScript and likely some form of a batch for loop, I need to:

  1. (maybe) Identify the total number of pages there are to work with(?)
  2. Loop through them and compile an output file out of two documents in the correct order

Further Question Detail & Testing Resources:

A similar/partial solution may be found here, however, I do not know python well enough to interpret what is happening or test it.

Test_PageNumbers.pdf - Here's a link to an example "booked" PDF, what I'm starting with

IMPORTANT NOTE -- The above is added for clarity. Splitting the above PDF is outside of the scope of this question and has been answered before. See bottom section of this question for details on that.

left-sections.pdf - Here's just the truncated left side to test with

right-sections.pdf - Here's just the truncated right side to test with

DesiredOutput.pdf - Here's what the desired output would look like

Here's a table of what the correct order looks like: Table of Correct Order and Files


What I've Already Done

This script combines the PDFs, but all out of order.

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

This script tells me the number of pages (outputs: NumberOfPages: 8) but I've had a terrible time isolating just the number using pure Batch script to use in a for loop, which is my preferred method so I can run this on multiple machines without an additional library install.

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

This was my attempt at isolating the number. It doesn't work yet. I don't know if this step is necessary. There may be another creative solution, perhaps with a while loop, looking at errorlevel?

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

This script (based on: How can I split a PDF's pages down the middle? and Convert PDF 2 sides per page to 1 side per page and How to set custom page size with Ghostscript) splits the "booked" PDF into two sides. THIS IS BEYOND THE SCOPE OF THE QUESTION, BUT ADDED FOR CONTINUITY AND CLARITY OF WHAT I'M DOING

::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


Final Note

Once I have this final piece, I am good enough (I think) to compile one Batch program that processes a file and outputs a final PDF. I'm NOT(!) asking for an entire batch program from start to finish. This question is ONLY addressing the reordering of pages into one combined PDF where the pages are in order.

Thank you!

Io-oI
  • 9,237
smts
  • 113

1 Answers1

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

You may need more than one (or tow) for loop to do this.. d;)


Final Note

Once I have this final piece, I am good enough (I think) to compile >one Batch program that processes a file and outputs a final PDF. I'm >NOT(!) asking for an entire batch program from start to finish. This >question is ONLY addressing the reordering of pages into one >combined PDF where the pages are in order.

Thank you!

  • The batch below was created and used for the purpose of monitoring and testing the behavior of variables in and out of loops as well as the resulting file...
@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 &quot;%~0&quot;^|findstr /l &quot;!_cat!&quot;)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 ::

  • OutPuts
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 ::


Additional resources:

Io-oI
  • 9,237