\

Canonical Engineering Monograph: https://nandai.org/blog/reconstructing-docx-openxml-paragraphs-tables-from-pdf-glyphs · Zero-Upload Local Converter: nandai.org/convert/pdf-to-word-docx

An untagged PDF file (ISO 32000-2) does not contain paragraphs, table cells, or heading nodes. Unless the producer embedded a /StructTreeRoot accessibility tree, the page content stream stores low-level graphics state instructions (Tm, Td) paired with glyph array operators (Tj, TJ). Single words split across kerning adjustments, and two-column journal pages emit interleaved draw calls with no semantic container.

Naive converters respond by wrapping every text run inside an absolutely positioned Word text box (<w:pict> or <w:framePr>). The visual output looks accurate until an editor inserts three words on line 2 and the paragraph collides with the line below it.

Rebuilding editable ECMA-376 OpenXML (.docx) directly in browser memory requires a deterministic geometric reconstruction pipeline before writing word/document.xml.

1. Extracting Normalized Glyph Runs & Reading-Order Columns

Each extracted glyph span carries bounding geometry in PDF user-space points (1 pt = 1/72 in), normalized from bottom-left PDF coordinates to top-down page coordinates:

export interface TextSpan { text: string; x0: number; x1: number; yBaseline: number; fontSize: number; fontName: string; isBold: boolean; isItalic: boolean; } 

Column Gutter Detection via X-Projection Histograms

Before clustering lines vertically, multi-column pages (academic preprints, clinical protocols, financial filings) must be partitioned along vertical whitespace gutters. Otherwise, line k in column 1 merges straight across the gutter into line k in column 2.

  1. Divide page width [marginLeft, marginRight] into 1 pt horizontal bins and accumulate span occupancy across body-text bands (excluding full-width title banners and footers).
  2. Identify contiguous zero-occupancy channels wider than 1.25 * medianBodyFontSize spanning at least 55% of the body region height.
  3. Sort reading regions top-to-bottom for full-width headers/abstracts, then column-by-column left-to-right within each multi-column band.

2. Y-Baseline Line Merging & Paragraph Reflow Criteria

Within each reading band, spans sharing a common baseline coalesce into logical text lines when their vertical delta stays within 25% of the active cap height:

SameLine iff |spanA.yBaseline - spanB.yBaseline| <= 0.25 * capHeight 

Horizontal word-gap thresholds use the active font's em space: gaps smaller than 0.18 * fontSize join kerning fragments into one token, while gaps between 0.18 * fontSize and 1.4 * fontSize insert a single ASCII space.

Once sorted into lines, consecutive lines join into a single flowing OpenXML <w:p> paragraph unless one of four structural breaks triggers:

ParagraphBreak iff (lineCurr.yBaseline - linePrev.yBaseline) > 1.45 * medianLineSpacing OR |lineCurr.x0 - colLeft| > 0.8 * emWidth OR linePrev.x1 < colRight - 0.35 * colWidth OR startsWithListMarker(lineCurr.text) 

Soft hyphenation (- at linePrev end followed by lowercase continuation in lineCurr) unwraps cleanly when dictionary/compound rules permit, avoiding broken words such as real- time or algo- rithm.

Structural Element Naive Frame-Box Export Deterministic Layout Solver
Multi-line prose Hard line breaks / absolute frames Flowing <w:p> + <w:r> XML nodes
Two-column papers Reads horizontally across gutter X-projection partitions column bands
Heading hierarchy Plain bold text spans Font-size histogram (Heading1–Heading3)
Document privacy Remote cloud upload queue Runs in browser memory (/convert/pdf-to-word-docx)

3. Inferring <w:tbl> Grids From Ruled Paths and Borderless Gutters

Tables appear in PDFs in two distinct encodings:

  1. Vector-Ruled Tables: Content streams emit stroked lines or thin filled rectangles (m, l, re, S, f) whose horizontal and vertical intersections define exact row and column grid coordinates (<w:tblGrid>). Adjacent merged spans spanning multiple column boundaries emit <w:gridSpan w:val="k"/>.
  2. Borderless Financial & Spec Tables: No vector rules exist, yet spans repeat consistent X-gutter centroids across consecutive vertical rows (>= 3 aligned rows with >= 2 large inter-column gaps > 1.8 * emWidth).

Clustering column boundaries across rows via 1D X-interval overlap recovers true <w:tbl>, <w:tr>, and <w:tc> structures so numbers remain editable spreadsheet-style cells inside Word rather than fragile tab-stop strings.

<w:tbl> <w:tblGrid> <w:gridCol w:w="3200"/> <w:gridCol w:w="2800"/> <w:gridCol w:w="2800"/> </w:tblGrid> <w:tr> <w:tc><w:p><w:r><w:t>Parameter</w:t></w:r></w:p></w:tc> <w:tc><w:p><w:r><w:t>Baseline</w:t></w:r></w:p></w:tc> <w:tc><w:p><w:r><w:t>Measured</w:t></w:r></w:p></w:tc> </w:tr> </w:tbl> 

4. Packaging Valid ECMA-376 OpenXML Archives Locally

An OpenXML .docx package is a standard ZIP archive containing [Content_Types].xml, _rels/.rels, word/_rels/document.xml.rels, word/styles.xml, and word/document.xml. Escaping XML control characters (&, <, >), stripping invalid XML 1.0 control bytes (0x00–0x08, 0x0B–0x0C, 0x0E–0x1F), and normalizing w:space="preserve" on boundary whitespace guarantees strict parser acceptance across Microsoft Word, LibreOffice Writer, and Apple Pages.

Explore the full architecture notes at nandai.org/blog/reconstructing-docx-openxml-paragraphs-tables-from-pdf-glyphs or test client-side PDF-to-DOCX reconstruction directly at nandai.org/convert/pdf-to-word-docx and nandai.org/convert.