← All posts

Get the text of just pages 10 to 20

You need one section out of a 400-page report. Twenty pages, somewhere in the middle. What you actually want back is the words — something you can paste into an email, search, quote from, or hand to something else.

So you searched for how to extract the text from specific pages, and got two kinds of answer, neither of them yours. Half the results are code: a C# snippet setting a start page and an end page, a Python loop, a Power Automate connector with a screenshot. The other half answer a question that sounds identical and is not — how to pull out the pages that contain a particular word, which is a search, not a range.

The plain answer is shorter than either, and the only thing in its way is that two different jobs are wearing the same sentence.

Two jobs, one phrase

"Extract pages 10 to 20" describes two entirely different files coming back.

A document. Twenty pages of PDF, looking exactly as they do now, that you can send, print, sign or upload. That is Extract PDF pages.

The words. A plain .txt file containing the text those twenty pages carry, with no pages, no layout and no formatting. That is PDF to text.

Decide by what happens next. Reading, searching, pasting, feeding a script or a language model: words. Sending, printing, filing, attaching: document. If the answer is both, you do both, and the order matters less than you would think — see below.

Why every result is either code or the whole file

This is the part nobody says, and it explains the shape of the search results.

Extracting text is a walk. The tool opens a page, reads its content stream — the instructions saying put this glyph at this coordinate — and reconstructs a reading order from where the glyphs landed. Do that for one page and you have one page of text. Do it for every page and you have the document.

So a page range is not a feature that has to be built. It is simply which pages the loop visits. In any library it is two lines: setStartPage and setEndPage in PDFBox, -f and -l on the command line. That is why every developer-facing page on the subject has it, and why those pages are what you found.

A web converter is a different problem. It has one upload box and one button, and adding a range means a text field, a parser that understands 10-20 and 3,7,9, and a sensible message when you type a page the document does not have. Most of them never bothered, so they hand you the text of the whole file and let you scroll. Nothing technical is stopping them. The all-or-nothing behaviour is a user interface decision, not a limit of the format.

One consequence worth having: a range costs what it says. Our text tool reads one page at a time rather than extracting everything and slicing afterwards, so pages 10–20 of a 400-page document is eleven pages of work, not four hundred.

The other consequence is less welcome. A page number is the only selector the format offers. A PDF has no chapters, no sections and no headings — if you want "the methods chapter" you have to turn it into page numbers first. The document's bookmarks are the shortcut there, because each one records the page its section starts on, which is also the whole trick behind splitting a manual into chapters.

Getting the text of a range in one step

  1. Work out the position numbers, not the printed ones. Page 10 in the file is rarely the sheet with "10" on it — front matter, an added cover and merged-in exhibits all push the two apart, and the three different page numbers in every PDF is the whole story if the file has been through any of that. Our PDF inspector gives you the page count without uploading anything, which is the other number you need before writing a range with an open end in it.
  2. Set the range on PDF to text. 10-20 for a run, 3,7,9 for scattered pages, 12- for page twelve to the end. Pages come out in the document's order however you write them, and an en dash pasted out of Word is accepted rather than rejected.
  3. Leave "keep the layout" off for ordinary prose. Turn it on only for columns and tables, where reading by position on the page beats reading in the order the file happens to store — that switch does more than it looks like it does, including where the running header lands in your text.
  4. Read the note before you read the text. It gives a character count and says how many pages had no text at all. If your twenty-page range reports four empty pages, four of those sheets are pictures, and no setting will get words out of them.

When to take the pages out first

The two-step route — Extract PDF pages to make a twenty-page PDF, then text from that — produces exactly the same words. Extracting copies the page objects with their content streams untouched, and text extraction works a page at a time regardless, so there is no version of the file in which the words come out differently. The choice is about what else you want.

Take the pages out first when:

One thing to know if you keep the intermediate file: the extract is a new document. Bookmarks, form fields and the original's metadata do not come along, because none of them belongs to a page. Irrelevant if you are only after text. Not irrelevant if you were about to send that file to someone.

"The pages that mention X" is a different question

Half the results you found answer this one, so it is worth separating. You do not know the range. You know a phrase, and you want the pages carrying it.

There are two honest routes and no button:

Find the numbers, then use them. Run the whole document through PDF to text with page-break marks on, split the result on the form feed (\f), and search the blocks. With the range set to all, block n is page n, so a hit tells you the page number to put into Extract PDF pages. Crude, and it takes two minutes.

Split at the phrase. Split PDF by text starts a new file at every page containing words you choose, which is how a 300-page batch print becomes one file per invoice. Be clear about what it does, though: it cuts at the matches, it does not select them. The pages between one match and the next travel with the match above them. That is right for a batch of statements and wrong if you wanted the six pages that say "indemnity" and nothing else.

The same range, every month

If this is a recurring job — the same section of the same monthly report — chain it once. Workflows runs two to eight tools as a single pass: extract pages 10-20, then convert to text, one upload and a .txt at the end. Only the final output is kept, so the intermediate PDF is discarded when the run finishes; building and running is anonymous, and saving a workflow for next month needs an account.

The fan-out is the useful trick here. Put a splitter into runs of one page in front of the text step and the step runs once per file, so you get one text file per page, each with its own download — which is the shape you want if the text is going into a database with a page number beside each row.

On the command line, this is one line

If you have poppler-utils, or can install it, this is the tool the developer results were circling:

pdftotext -f 10 -l 20 -layout report.pdf section.txt

-f is the first page, -l the last, both counting sheets from 1, and -layout is the read-by- position mode. It is free, it is fast on enormous files, and it takes only a contiguous range — for 3,7,9 you run it three times and concatenate.

For the document half of the job, qpdf does it without rewriting anything:

qpdf --empty --pages in.pdf 10-20 -- out.pdf

Both are worth having on the machine if you do this more than twice.

What this will not do

A scanned range comes back as a refusal, not an empty file. If the pages are pictures of words there is nothing to extract, and the job stops and says so. Our OCR PDF tool will name the pages responsible, but it cannot mend them: Tesseract is absent from this deployment, so the report is the whole of what comes back — which pages need recognition is the fuller account. For the recognition itself you need the free ocrmypdf, your scanner's software, or Acrobat.

You get one text file for the range, not one per page. The workflow above is the way round that.

Formatting is not text and does not survive. Bold, headings, footnote markers, tables and the difference between a caption and a paragraph are all gone. If figures are what you are after, do not extract prose at all: PDF to Excel takes the same page range and reads the pages as rows and cells instead, and the full route into a spreadsheet covers which of the four ways to do it suits your table.

No page labels. The range box reads digits, dashes, commas, 12-, odd and even. Type iv and it is refused. Whatever your reader prints in its page box, the tools count sheets.

Twenty pages of text out of four hundred takes about a minute once you know which twenty. The minute you should spend is the one before that, working out whether the pages you are naming are the pages you mean.