QuickieTool

How to get clean text out of a PDF instead of a jumbled mess

By Muhammad Zaheer, QuickieTool6 min read

You copy three paragraphs out of a PDF and paste them somewhere useful. What arrives has a line break after every line rather than every paragraph, words run together where spaces should be, and if the page had two columns the sentences alternate between them into nonsense.

This is not a bug in your reader. It is a direct consequence of how PDFs store text, and understanding it tells you which documents will copy cleanly and which never will.

A PDF has no idea what a paragraph is

When a PDF is created, the text is broken into fragments and each is placed at a coordinate. The file records that certain characters are drawn at a certain position in a certain font. It does not record that these two lines belong to the same paragraph, or that this column should be read before that one.

So when software extracts text, it is reconstructing reading order by looking at positions. It sorts fragments by where they sit and guesses where lines, paragraphs and columns begin. On a simple single-column page that guess is nearly always right. On anything more complex it is a genuine inference, and inferences fail.

The four failures and why they happen

  • A line break after every line: the extractor cannot tell a wrapped line from a deliberate one, so it preserves both
  • Words run together: the space between fragments was drawn as positioning rather than an actual space character
  • Columns interleaved: fragments sorted top to bottom across the whole page rather than down one column first
  • Headers, footers and page numbers appearing mid-sentence: they sit between the last line of one page and the first of the next
  • Ligatures arriving as odd characters: fi and fl are frequently stored as single glyphs that map back imperfectly

The missing-space problem surprises people most. In many PDFs there is no space character between words at all — the next word is simply drawn slightly further along. Extraction has to infer a space from the gap, and if the gap is narrow it does not.

The scanned case

If the document is a scan, there is no text to extract, only a picture of text. Selecting nothing when you try to highlight a word is the giveaway.

Run recognition first to generate a text layer, then extract from that. Bear in mind that you are then reading a transcription rather than the document, and transcriptions contain errors, particularly in numbers, proper nouns and anything set in an unusual typeface.

Pull the text out of a PDF in one step. Runs in your browser, so your document never leaves your device.

Extract text from a PDF

Getting a better result

  1. Extract the whole document rather than copying a selection, since extraction tools handle reading order better than a drag-select does.
  2. Confirm there is real text by trying to highlight a single word, and run OCR first if there is not.
  3. Expect to fix line endings. Replacing single line breaks with spaces while preserving double ones restores paragraphs in most editors.
  4. Read the joins between pages, where headers and footers intrude.
  5. Check numbers against the original rather than trusting them, especially after OCR.

That line-ending repair is worth learning once. Most text editors and word processors can do it with a find and replace, and it turns an unusable extraction into clean prose in about ten seconds.

When to use a different route

If you need the layout as well as the words — tables, headings, structure — extracting plain text throws all of that away. Converting to Word preserves considerably more, at the cost of a reconstruction that will need tidying.

And if the document originated somewhere else, ask for that original. A Word file, a spreadsheet or even an email carries structure that no amount of extraction can rebuild, and asking takes less time than repairing.