
pagecount prints the number of pages in a PDF. When it cannot be sure of the count, it refuses (exit code 2) instead of printing a number.
- Your documents stay with you. pagecount reads the one file you give it. No networking code is built into it; checked by nonet_test.go.
- Nothing to operate. One program file. No server, no account.
- Nothing is metered. Open source; never a charge per document or per month.
It is run from a command line:
% pagecount three.pdf
three.pdf: 3 pages
How it was checked
Nobody has counted the pages of these files by hand, so pagecount was compared with seven other PDF tools on the same 245 files: 200 court filings and 45 files we damaged on purpose.
- Court filings: on all 200, pagecount gave the same count as every tool that gave a count.
- Damaged files: pagecount gives the count most of the tools give. It refused 6, where the tools tied or most of them refused.
Agreement with other tools shows that pagecount is consistent with them, not that it is right: the tools could all be wrong together.
The rest of this page is the full record, for readers who want to check the work.
Results
No file has a human-checked page count, so pagecount’s count is checked by agreement with seven
other PDF tools on the same 245 files: 200 RECAP court filings and 45 damaged files written by
the author. On damaged files pagecount returns what most tools return, or refuses on a tie.
replication/ runs the comparison and holds its inputs and outputs (replication/README.md).
Question. How many pages does this PDF have? The correct answer is the number of pages a reader sees when the file is opened in a viewer. pagecount measures that by walking the page tree, which is what viewers read; on a damaged file it follows what most viewers and PDF tools do (Damaged files: a choice). “Refused” is correct when the tools disagree on what a reader would see.
Wrong if. pagecount is wrong if it prints a count that differs from what the majority of the
compared tools show, or prints a count when they tie. Tests:
TestCountPages (internal/pdf/pdf_test.go), TestDamaged (internal/pdf/damaged_cases_test.go),
TestPolicyVote (replication/replication_test.go), and
go run ./cmd/summary -refusals replication/refusals.tsv FILE.tsv.
The cmd/summary command compares pagecount’s output with replication/refusals.tsv,
which records the intended result (a count, or refuse) for 32 of the 45 damaged files: the 26 on
which pagecount warns or refuses, so a damage rule decides the result, and 6 it reads with no
warning. The other 13 have no decision because pagecount reads them with no warning and no rule
applies. Run on
replication/compare.tsv, section 4 of its output lists each decided file where
pagecount’s result differs from the decision, and reports 0 of 32.
Ground truth. The ground truth is the page count a viewer shows. pdfium (Chrome) and pdf.js (Firefox) are viewers; pdfinfo, mutool and gs are the engines of other viewers; qpdf and pdfcpu are PDF libraries. For the 200 RECAP files every tool that returned a count agrees (mutool refused 1, pdfcpu 2), so the ground truth is known. For the 45 damaged files the majority result is taken as ground truth; where the tools tie, it is unknown and pagecount refuses.
Key result. 200 RECAP court filings: pagecount agrees with pdfinfo, qpdf, gs, pdf.js and pdfium on all 200, with mutool on the 199 it counts and pdfcpu on the 198 it counts, and refuses none; 0 disagreements in 200 gives an upper bound of 1.5% (3/200) on the disagreement rate at 95% confidence. 45 damaged files written by the author: pagecount differs from pdfium on 5 and refuses 6. On those 5 files the two tools give:
| File | pagecount | pdfium |
|---|---|---|
07-typed-intermediate | refused | 3 |
08-count-high | 3 | 7 |
11-count-huge-1e9 | refused | 3 |
35-kids-not-array | refused | 3 |
41-count-missing | refused | 3 |
Agreement with other tools shows consistency, not accuracy: the tools could all be wrong together.
| Tool | Count | Agree | Disagree | Refused | Error |
|---|---|---|---|---|---|
| pagecount | 239 | — | — | 6 | 0 |
| pdfinfo | 238 | 234 | 0 | 7 | 0 |
| mutool | 232 | 232 | 0 | 13 | 0 |
| qpdf | 241 | 232 | 5 | 3 | 1 |
| pdfcpu | 225 | 222 | 1 | 20 | 0 |
| gs | 242 | 238 | 1 | 3 | 0 |
| pdf.js | 241 | 234 | 3 | 2 | 2 |
| pdfium | 243 | 238 | 1 | 2 | 0 |
Every row ran on the same 245 files: the 200 RECAP files and the 45 damaged files
(replication/summary.txt, section 1). Count: files where the tool printed a page count.
Agree and Disagree: of the files where both the tool and pagecount printed a count, how many counts
were equal and how many differed; pdfinfo’s 0 in 234 gives an upper bound of 1.3% (3/234) at 95%
confidence. Refused: the tool declined with a documented error exit or message. Error: the tool
crashed, timed out, or exited without a count (qpdf and pdf.js on 10-count-negative, pdf.js on
21-objstm-bad-n).
Sample. 200 PDFs from RECAP, downloaded from CourtListener (replication/files.csv gives each
file’s URL) on 2026-10-25, from five named districts (selection: replication/README.md). S.D.
Tex. returned none, so the 200 are S.D.N.Y. 106, D.D.C. 43, C.D. Cal. 32, N.D. Ill. 19. The
label “Our observation:” marks a fact we recorded ourselves, with no external source to link. Our
observation: the 200 are 186 distinct byte streams (SHA-256 in files.csv); largest file 55 pages,
1,622 pages in all. 45 damaged files written by the author of pagecount
(internal/pdf/damaged_cases_test.go). Our observation: Acrobat has no command-line page count, so it
was not run.
Refusals. pagecount refused 6 of 245 files (2.4%), all of them damaged files and none of the 200 RECAP files.
| File | pdfinfo | mutool | qpdf | pdfcpu | gs | pdf.js | pdfium | Decision in refusals.tsv |
|---|---|---|---|---|---|---|---|---|
07-typed-intermediate | 3 | refused | 3 | 2 | 2 | 2 | 3 | refuse: tie |
11-count-huge-1e9 | 1 | refused | 0 | refused | 3 | 1 | 3 | refuse: tie |
28-encrypted-plain | 3 | refused | refused | 3 | refused | refused | refused | refuse: majority refuses |
35-kids-not-array | 3 | refused | 3 | refused | refused | 1 | 3 | refuse: tie |
37-root-not-catalog | refused | refused | refused | refused | 3 | refused | refused | refuse: majority refuses |
41-count-missing | refused | refused | 0 | refused | refused | 3 | 3 | refuse: majority refuses |
The wrong-refusal rate is 0 of 6: pagecount refused no file on which a majority of tools agreed on one count, which checks the code against the policy, not against ground truth.
Known consequences of the policy.
- A junk
/Kidsentry (a non-dictionary, or a reference to a free or missing object) counts as a page (rule 3 below). - A rebuilt xref can revive a deleted object: the scan keeps the last
N G objheader for each object number, including one a later update freed (rule 1 below).
Replication. replication/versions.txt gives the exact version of every tool run, pagecount
at a64b02a. replication/README.md says how to rerun it.
Performance
Our observation: the benchmark below measures the parser’s wall time and bytes allocated per page on files already in memory, so reading the file is excluded.
go test -run '^$' -bench . -benchmem ./internal/pdf
On an Intel Core i7-9750H (2.60GHz, macOS 15.7, Go 1.26.4):
| Pages | Per page | Per file | Input |
|---|---|---|---|
| 3 | 5.9 µs, 1.6 KB | 18 µs, 4.8 KB | three.pdf |
| 10,000 | 4.4 µs, 1.1 KB | 44 ms, 11.1 MB | generated, 100 /Pages nodes of 100 pages |
| 0–3 | — | 1.2–79 µs, 0.5–90 KB | the other 44 damaged files from TestDamaged |
| 1 | — | 30 ms, 12.9 MB | damaged 05-deep-tree: 10,000 nested /Pages above one page |
go test ./... fails if any of these exceeds a per-page ceiling of about 3× the measured value
(internal/pdf/perf_test.go; a refused file counts as one page):
| Wall per page | Bytes per page | Input |
|---|---|---|
| 16 µs | 4,800 | three.pdf |
| 14 µs | 3,300 | 10,000-page file |
| 60 µs | 260,000 | other damaged files |
| 120 ms | 39,000,000 | 05-deep-tree |
Damaged files: a choice
For a file whose page tree can be walked but whose structure is damaged, pagecount returns the
most common result among pdfinfo, mutool, qpdf, pdfcpu, gs, pdf.js and pdfium; a tie means refuse.
This is a choice, not a correctness result. The aim is the number of pages a reader sees;
pdfium (Chrome’s PDF viewer) is the reference for that, and 08-count-high (pdfium 7, gs
and pdf.js 3) shows viewers can disagree.
The choice is made per file in replication/refusals.tsv and implemented as one rule per kind of
damage, so no file is a special case:
- Xref. If an xref offset doesn’t hold the object header it names (no header, wrong number or
wrong generation), or
startxrefor a/Prevdoesn’t land on an xref section, the xref is rebuilt by scanning the file forN G obj; the last header for a number wins, stream data is skipped, and objects in object streams are added. The trailer is the union of every trailer and xref stream dictionary found; without a/Root, the last/Type /Catalogobject is used. stderr:xref rebuilt by scan. A file with nostartxrefat all is still refused. - Root
/Count. The walked count is printed. A/Countthat is not an integer, beyond ±2^53, or different from the walk goes to stderr as/Count N disagrees with walked tree M. A root/Pageswithout/Countis refused (41-count-missing: pdfinfo, mutool, pdfcpu and gs refuse, qpdf 0, pdf.js and pdfium 3: majority refuses, 4 of 7 refuse)./Count 1e9is not a/Countcase:1e9is not PDF number syntax (ISO 32000-1 §7.3.3, Numeric Objects), so the/Pagesobject doesn’t parse, and an object on the way to the pages that doesn’t parse is refused (11-count-huge-1e9: pdfinfo and pdf.js 1, gs and pdfium 3, a tie). - Page tree. An inner node is
/Type /Pages, or has no/Typeand has/Kids. Every other/Kidsentry is one page: a/Type /Page, a dictionary of another or no/Type, a non-dictionary, or a reference to a free or missing object (including a generation the xref doesn’t hold); a catalog/Pagesthat is such a node makes a one-page document. A/Kidsentry that leads back to its own ancestor is skipped (stderrcycle in page tree), so the count is the distinct pages reachable. An inner node whose/Kidsis not an array is refused (35-kids-not-array: pdfinfo, qpdf and pdfium 3; mutool, pdfcpu and gs refuse; pdf.js 1: a tie). A/Type /Pagenode with/Kidsis refused (07-typed-intermediate: pdfinfo and pdfium 3, gs and pdf.js 2, a tie). A/Rootthat is not a catalog, so has no/Pages, is refused (37-root-not-catalog: only gs counts). - Object streams. The header is read up to
/Npairs or the first token that isn’t a pair of integers; a/Firstoutside the decoded data is taken to be where the pairs end. - Encryption. Encrypted files are refused.
Download
Ready-to-run programs for Linux, macOS and Windows are on the releases page, with a checksums.txt. There is one for Intel or AMD processors (amd64) and one for ARM processors (arm64). They come as .tar.gz archives (Windows: .zip).
On macOS or Linux:
tar xzf pagecount_darwin_amd64.tar.gz
xattr -d com.apple.quarantine pagecount 2>/dev/null # macOS, browser download only
./pagecount FILE.pdf
macOS blocks an unsigned program downloaded through a browser with “Apple could not verify pagecount is free of malware” (Apple: Safely open apps on your Mac); the xattr line removes that flag.
Build from source
go install github.com/docs2info/pagecount@latest
Source
github.com/docs2info/pagecount
peter@docs2info.com