Skip to content

Instantly share code, notes, and snippets.

@juliennonin
Last active April 2, 2022 12:31
Show Gist options
  • Save juliennonin/38cfb7a4712cbd105020b7528b8aeafa to your computer and use it in GitHub Desktop.
Save juliennonin/38cfb7a4712cbd105020b7528b8aeafa to your computer and use it in GitHub Desktop.
é 351
è 350
– 205
È 310
É 311
à 340
ç 347
Ç 307
À 300
— 301
— 204
™ 222
æ 346
â 342
€ 240
ê 312
ÿ 377
û 373
î 356
œ 234
ô 364
ä 344
ß 337
ë 353
‘ 217
’ 220
ü 374
ï 357
ö 366
« 253
» 273
ù 371
¿ 277
× 327
÷ 367
¡ 241
£ 243
§ 247
Æ 306
 302
Ê 312
Ÿ 230
Û 333
Î 316
Π226
Ô 324
Ä 304
Ë 313
Ü 334
Ï 317
Ö 326
Ù 331
“ 215
” 216
… 203
⋅ 267

Extract a set of pages:

gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dFirstPage=294 -dLastPage=523 -sOutputFile=output.pdf input.pdf

Merge a group of pdf files into one:

gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf part1.pdf part2.pdf part3.pdf

Remove existing bookmarks

Just "print" your pdf in pdf format!

Add bookmarks with pdfmark

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

Apply correct page numbers

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

Encoding

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

Ressources for understanding pdfmark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment