gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dFirstPage=294 -dLastPage=523 -sOutputFile=output.pdf input.pdf
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf part1.pdf part2.pdf part3.pdf
Just "print" your pdf in pdf format!
Create a fine tableofcontents.txt
that contains something like:
[ /Count 2 /Page 13 /View [/XYZ 44 730 1.0] /Title (Part I - Basics) /F 2 /OUT pdfmark
[ /Page 14 /View [/XYZ null null 0] /Title (1. Fundamental Coefficients) /OUT pdfmark
[ /Page 62 /View [/XYZ null null 0] /Title (2. Formal Series and Infinite Matrices) /OUT pdfmark
[ /Count 4 /Page 100 /View [/XYZ 44 730 1.0] /Title (Part II - Methods) /F 2 /OUT pdfmark
[ /Page 101 /View [/XYZ null null 0] /Title (3. Generating Functions) /OUT pdfmark
[ /Page 150 /View [/XYZ null null 0] /Title (4. Hypergeometric Summation) /OUT pdfmark
[ /Page 186 /View [/XYZ null null 0] /Title (5. Sieve Methods) /OUT pdfmark
[ /Page 246 /View [/XYZ null null 0] /Title (6. Enumeration of Patterns) /OUT pdfmark
[ /Count -4 /Page 293 /View [/XYZ 44 730 1.0] /Title (Part III - Topics) /F 2 /OUT pdfmark
[ /Page 294 /View [/XYZ null null 0] /Title (7. The Catalan Connection) /OUT pdfmark
[ /Page 350 /View [/XYZ null null 0] /Title (8. Symmetric Functions) /OUT pdfmark
[ /Page 398 /View [/XYZ null null 0] /Title (9. Counting Polynomials) /OUT pdfmark
[ /Page 456 /View [/XYZ null null 0] /Title (10. Models from Statistical Physics) /OUT pdfmark
/Count n
specifies a section that contains the n
following titles as subsections.
Then run
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf -dPDFSETTINGS=/prepress tableofcontents.txt input.pdf
In tableofcontents.txt
add:
[
{Catalog} <<
/PageLabels <<
/Nums [
0 << /S /R >>
10 << /S /D /St 1 >>
13 << /S /D /St 5 >>
500 << /P (back ) /S /A >>
]
>>
>>
/PUT pdfmark
⚠️ page index starts at 0/S
set the style for the page number (/D
for arabic,/R
for uppercase Roman, ...)/St
set the start page numbering- cf. this great SE answer
cat tableofcontents.txt | sed $(cat ~/chars.txt | sed -e 's/ /\/\\\\/g;s/$/\/\g/;s/^/\s\//g;' | sed -z 's/\n/;/g') > _pdfmarks && gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf -dPDFSETTINGS=/prepress _pdfmarks input.pdf && rm _pdfmarks
The above command replaces the "special characters" found in tableofcontents.txt
by their octal values (as specified in chars.txt
) and stores the result in a temporary file named _pdfmarks
. Then it runs the standard command to add these bookmarks to input.pdf
resulting in an out.pdf
.
The content of chars.txt
to map special characters to their PDF octal values can be found attached and is based on Appendix D of PDF Reference