
pagecount is a small open-source program that prints the number of pages in a PDF. It finds each page in the file and counts them. On a damaged file it gives the count that most PDF tools give, by a published rule, and says what damage it counted through. It refuses to give a number only when it cannot count, or when the tools it is compared with cannot agree.
How a PDF holds its pages
A PDF holds its pages in a tree of lists. The file names a first list. Each list names pages, or further lists. pagecount follows every list and counts the pages it reaches.
The first list also holds a total, written by the program that made the PDF. pagecount does not rely on that total. It also does not search the file for the label that marks a page, because the same label can appear inside ordinary text.
When the file is damaged
- The stored total is wrong. A file that says it has 7 pages and holds 3 is inconsistent. pagecount prints 3, the number it counted, and gives both numbers in a warning.
- A list leads back to a list above it. pagecount skips it, so the loop does not stop the count.
- The file’s index is damaged. The index says where each item sits in the file. pagecount rebuilds it by scanning the file.
pagecount still refuses an encrypted file, a file that does not say where its index is, and a damaged file where the other tools tie.
For people running it from a script: warnings go to stderr, and a refusal is exit code 2.
The rules for damaged files, the consistency check against seven tools (pdfinfo, mutool, qpdf, pdfcpu, Ghostscript, pdf.js and pdfium) on 245 files with 6 refusals, downloads and source are on the pagecount tool page.
peter@docs2info.com