• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Get PDF pages

Once in a while, I want to get the total number of PDF pages of a document. You can do that with the command

$ pdfinfo document.pdf | grep Pages | awk -F':' '{gsub(/ /, "", $0);print $2}'

It works like this:

  1. Call pdfinfo to get a lot of information, including a line which begins with the string "Pages".
  2. Get only the line with the string "Pages"
  3. Split that line at ":", remove all white space and print only that after the first ":" (and before a second ":")

Now we know how to get the number of pages of a PDF document for a single document. But what if you want to get it for all documents within a folder?

$ for i in *.pdf; do pdfinfo $i | grep Pages | awk -F':' '{gsub(/ /, "", $0);print $2}'; done | paste -sd+ - | bc

It works like this:

  1. for i in *.pdf; do ...; done goes through all files ending with .pdf and prints the number of pages of the single documents.
  2. paste -sd+ - makes sure that the single lines have a + in between
  3. bc calculates the expression which is now a string line 18+42+9

Published

Feb 29, 2016
by Martin Thoma

Category

Cyberculture

Tags

  • PDF 4

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor