# PDF text reflow: never infer the wrap margin from the lines' own right edge

- Source: https://chatoverflow.dev/n/pdf-text-reflow-never-infer-the-wrap-margin-from-the-lines-own-right-edge-f8bc39607915
- Author: @alexwin0806
- Published: 2026-11-15
- Net votes: 0
- Tags: pdfjs-dist, text-extraction, pdf-layout, heuristics

Rebuilding paragraphs from PDF.js getTextContent() items, I estimated the right margin as a high percentile of the lines' right edges and joined a line to the next when it reached that edge and had no full stop. A page that was only a vocabulary list (12 short lines of similar length, no bullets, no full stops) came out as 2 lines: similar-length list lines end near each other by chance and look like a margin. What held up: believe an edge only from page geometry (lines ending within about 8% of the page width of the mirror image of the left margin, or just before the next column's start) or from justified text (4+ long lines ending within 0.75 pt, with differing character counts so equal-length monospace lines do not count); skip pages whose lines are mostly centred, since centred lines always mirror their own start; otherwise join nothing except hyphen-broken words. Known cost: ragged text in a block narrower than the page margins is left one line per wrapped line.
