By Alex, who builds Cuelark. This article mentions our own free PDF tools.
Someone sends a 40-page bank statement and the form only wants pages 2 and 5. Or a scanned admission packet where the university asks for the mark sheet alone. You need a smaller PDF with a few pages pulled out, and you want those pages to look exactly like they did before.
That last part is where a lot of people get burned. The quick fix most of us reach for is "Print to PDF" or a screenshot pasted into a document. Both work in the sense that you get a file. But they redraw the page. Text can stop being selectable, scans get recompressed, and sometimes the file gets bigger instead of smaller.
This post covers the approach we use: copy the page objects into a new file and leave their contents alone. I'll show it with two command-line tools, then explain a few results that surprised me when I measured them.
Copying pages vs redrawing them
A PDF page is a set of drawing instructions plus the resources it points to: fonts, images, sometimes form fields and links. An extraction tool can do one of two things.
- Copy. Take the page object and everything it references, and write it into a new PDF. The JPEG inside a scanned page goes across byte for byte. Text stays text.
- Redraw. Render the page to pixels (or to a new set of drawing commands) and build a new PDF from that. This is what printing and screenshotting do.
For "give me pages 2 and 5", you almost always want the first one. Redrawing only makes sense when you are deliberately flattening something, like burning in annotations.
Option 1: qpdf
qpdf is a small, mature tool for structural PDF changes. On Debian or Ubuntu:
sudo apt install qpdf
On macOS, brew install qpdf works. I tested everything below with qpdf 12.2.0.
To keep pages 2, 4, 5 and 6 from scan.pdf:
qpdf scan.pdf --pages . 2,4-6 -- out.pdf
The . means "the input file I already named". The page list accepts a few handy shortcuts:
# page 8 to the last page ("z" is the last page)
qpdf --empty --pages scan.pdf 8-z -- tail.pdf
# odd pages only
qpdf scan.pdf --pages . 1-z:odd -- odd.pdf
# the last six pages, in reverse order ("r1" is the last page)
qpdf scan.pdf --pages . r6-r1 -- reversed.pdf
# page 2 of one file followed by page 1 of another
qpdf --empty --pages scan.pdf 2 letter.pdf 1 -- combined.pdf
--empty starts from a blank document instead of using the first file as the base. That difference affects file size more than you'd think, as the measurements below show.
Password-protected files
If the PDF has a user password, qpdf refuses with invalid password. If you know the password, write an unlocked copy first and extract from that:
qpdf --password='your-password' --decrypt locked.pdf unlocked.pdf
qpdf unlocked.pdf --pages . 1 -- page1.pdf
Only do this with files you are allowed to open, obviously. The point is that extraction tools generally need an unlocked copy, and qpdf will tell you clearly when that is the problem.
Option 2: poppler's pdfseparate and pdfunite
If you already have poppler-utils installed (it provides pdftotext, pdfinfo and friends), you can extract without anything new:
# one file per page, for pages 2 to 6
pdfseparate -f 2 -l 6 scan.pdf page-%d.pdf
# stitch the pages you want back together
pdfunite page-2.pdf page-4.pdf page-5.pdf page-6.pdf out.pdf
It is two steps instead of one and leaves temporary files around, but it's useful on machines where you can't install packages. I used poppler 25.03.0 for these tests.
What I measured
I didn't want to hand-wave about sizes, so I generated two test files on a Linux box and ran the commands above.
A "scanned" PDF. Twelve A4 pages, each one a 1654 × 2339 pixel JPEG at 200 ppi, built with Python's Pillow. The file was 5,525,089 bytes.
Extracting pages 2, 4, 5 and 6:
| Method | Output size | Pages |
|---|---|---|
qpdf --pages . 2,4-6 |
1,846,776 bytes | 4 |
pdfseparate + pdfunite
|
1,849,205 bytes | 4 |
Both outputs come out at almost exactly a third of the original, which is what you'd expect for 4 of 12 roughly equal pages. The images were not touched. You can check that yourself:
pdfimages -list out.pdf
The listing still showed 1654 × 2339 JPEGs at 200 ppi, each around 450 KB, the same as in the source.
A text PDF. Eleven pages of plain text exported from LibreOffice, 29,419 bytes, with one embedded font. I extracted pages 3 and 4 two ways:
| Method | Output size |
|---|---|
qpdf doc.pdf --pages . 3-4 -- out.pdf |
22,416 bytes |
qpdf --empty --pages doc.pdf 3-4 -- out.pdf |
11,183 bytes |
Two pages out of eleven, and the first command kept about three quarters of the original size. That looked wrong, so I dumped the objects with qpdf --json.
Two things were going on. First, when you use ., qpdf keeps document-level objects from the input file. LibreOffice had written an XMP metadata stream (4,960 bytes) and a tagged-PDF structure tree, and both came along. With --empty, they didn't, and the size halved. The catch: pdfinfo reports the first file as Tagged: yes and the second as Tagged: no. Tags help screen readers, so if accessibility matters for the document, keep the bigger file.
Second, both pages use the same embedded font subset, about 8 KB compressed. Copying a page means copying everything it references, so that font comes along whichever command you use. Even the smaller file is still 38% of the original for 18% of the pages.
So here is the practical rule: with scans, extracting pages saves space roughly in proportion to the pages you drop. With text-heavy PDFs, shared fonts and document-level data set a floor, and the saving can be small. If you need the file under a size limit, extraction alone may not get you there. You may need compression as a separate step.
What redrawing costs. For comparison, I rendered the same two text pages to PNG at 150 dpi with pdftoppm and rebuilt a PDF from the images. That file was 418,755 bytes, about 37 times the size of the --empty output. And pdftotext found zero words in it, because the text was now a picture. That's the "Print to PDF from a screenshot" path, and it's a bad trade for documents that are mostly text.
Check the output before you send it
A few quick commands I run before attaching an extracted PDF to anything that matters:
pdfinfo out.pdf | grep Pages # right page count?
pdftotext out.pdf - | head # text still selectable?
pdfimages -list out.pdf # scans kept their resolution?
qpdf --check out.pdf # structure OK?
Then open it and look at every page. Page numbers printed on the paper won't match the PDF's page indexes if the document has a cover or roman-numeral front matter, and that's the most common reason someone ends up with the wrong page.
Extract, remove, or split?
These three get mixed up a lot, including in search results:
- Extract keeps only the pages you pick. "I need pages 2 and 5."
- Remove keeps everything except the pages you pick. "Drop the blank page and the ad at the end."
- Split turns one PDF into several files. "One file per chapter", or "parts that each fit under an email attachment limit".
With qpdf, all three are just different page lists. If you'd rather not use a terminal, or you're on a phone, we built browser versions that do the same copy-not-redraw thing. Our extract PDF pages tool takes ranges like 2, 4-6 or an open end like 8-, and copies the selected pages into a new file in their original order. Text stays selectable and scans aren't re-encoded. If you want the opposite, Remove PDF pages keeps the rest of the document. And Split PDF handles the multi-file cases: every N pages, odd and even pages, or parts capped at a size such as 24 MB for email.
All three run inside the browser tab, so the file isn't uploaded anywhere. You can confirm that in your browser's network panel while a file is processing. That matters for the kind of documents people usually extract pages from: statements, ID scans, mark sheets.
Summary
- Use a tool that copies pages (qpdf, pdfseparate/pdfunite, or a browser tool built the same way). Avoid print-to-PDF and screenshots for this job.
-
qpdf in.pdf --pages . 2,4-6 -- out.pdfcovers most cases.zmeans the last page,r1counts from the end,:oddand:evenfilter. - Expect scans to shrink in proportion to the pages removed. Expect text PDFs to shrink less, because shared fonts get copied with every page that uses them.
-
--emptydrops document-level data such as metadata and accessibility tags. Smaller file, but check whether you need the tags. - Check page count, text, and image resolution before you send the file.
If you have a PDF where extraction behaves strangely, like links breaking or form fields vanishing, I'd like to hear about it in the comments. Those edge cases are where most of our bug fixes come from.
Top comments (0)