Skip to main content

Guides

Cleaning text copied from a PDF

· 6 min read

Text copied out of a PDF arrives broken in a consistent set of ways, and the reason is structural. A PDF does not store paragraphs. It stores glyphs at coordinates. When you copy, software reconstructs a reading order from positions, and the paragraph structure was never there to recover.

What you get and why

SymptomCause
A line break after every visual lineEach rendered line was a separate text object
Words split with a hyphenThe original was hyphenated to justify the line
Page numbers and headers mixed into the textThey are text objects like any other
Odd spacing inside wordsLetter spacing applied for justification
Missing or merged lettersLigatures, where fi or fl is one glyph
Columns interleavedReading order guessed wrongly across a multi-column layout

Fix it in this order

Order matters, because some steps destroy the information later steps need.

Removing line breaks before rejoining hyphenated words is the mistake that cannot be undone. Once the break is gone, a hyphen in the middle of a sentence is indistinguishable from a real one.

The hyphen problem in detail

A hyphen at the end of a line is usually a split word, but not always: a genuine compound can fall there too. Automatic rejoining will occasionally merge a real compound into one word. On a long document this is a reasonable trade, and it is worth a read-through afterwards rather than trusting it blindly.

Columns and tables

Multi-column layouts are where copying fails worst, because the reading order has to be inferred from geometry. Tables are the same problem: what you get is a sequence of cells with no structure. If the table matters, it is usually faster to retype it than to repair the paste.

Invisible characters come along too

PDF text carries non-breaking spaces, soft hyphens and unusual whitespace, all of which survive the paste and none of which are visible. If a comparison or a search behaves oddly on cleaned text, that is usually the reason.

The tools here run in the browser, which matters because the PDF is often a contract, an invoice or a report rather than something public.

Frequently asked questions

Why does every line break when I copy from a PDF?

A PDF stores glyphs at positions, not paragraphs. Each visual line is a separate text object, so the copy has a break wherever the line ended on the page.

What should I fix first?

Headers and page numbers, then hyphenated words, then line breaks. Removing the breaks first destroys the signal that tells you a word was split.

Why do some words lose letters?

Ligatures. Pairs such as fi and fl can be stored as a single glyph, and extraction sometimes fails to map it back to two letters.