docling icon indicating copy to clipboard operation
docling copied to clipboard

headers on left-hand-side not attached to paragraph block on right-hand-side

Open clach04 opened this issue 5 months ago • 5 comments

Bug

Default OCR works well BUT layout is wrong when headers are on the left but paragraph(s) on the right.

I think this is a bug, it is remotely possible this is a feature request in disguise (or even user error).

The test files provided are created by me, based on some real physical paperwork I was scanning and OCR'ing. EDIT also see comment https://github.com/docling-project/docling/issues/3643#issuecomment-4760454950 attachments.

The original was a 3-color scan, I've provided 3-color and mono samples.

docling_bugreport_misplaced_headers_singlepage_red.html

docling_bugreport_misplaced_headers_singlepage_black.html

Docling bug report for headers not connected to paragraph content_3-color_red.pdf

Image

Docling bug report for headers not connected to paragraph content_mono.pdf

This bug report is about the layout (although this may be an interesting one for bold/emphasized text?)

Steps to reproduce

Download attachments and run:

docling --to html_split_page INPUT_FILENAME

demo uses html_split_page for easy reviewing of problem.

End up with:


## FOR HEADERS AND PARAGRAPHS

Docling bug report for headers not connected to paragraph content

## First Header

## Second Header

## Third Header

Just a regular paragraph, note header is to the left BUT right aligned. This paragraph is justified but to the  right  of  the  header.  There  should  be  a  line below.

Term: Yet more text. Not a lot, just a few sentences. Really tempting to use Ipsum here.

AnotherTerm: I'm  getting  lazy.  I  may  just  copy  and paste  the  rest  to  save  me  some  time.  I  can't  keep typing rubbish for ever.

AnotherOne: This is a long paragraph. Yep, I'm about to copy and paste here. I'm getting lazy. I may just copy and paste the rest to save me some time. I can't keep typing rubbish for ever.I'm getting lazy. I may just copy and paste the rest to save me some time. I can't keep typing rubbish for ever.I'm getting lazy. I  may  just  copy  and  paste  the  rest  to  save  me  some time. I can't keep typing rubbish for ever.

LastOne: Yet  more  text.  Not  a  lot,  just  a  few sentences. Really tempting to use Ipsum here.

Just  a  couple  of  paragraphs  here.  A  few  sentences each,  longer  than  I  really  can  be  bothered  to  type here. I'm really getting bored typing this. No, more, please!

This  is  a  long  paragraph.  but  shortened  to  get  a single page demo.

(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Expecting

## BUG REPORT

## FOR HEADERS AND PARAGRAPHS

Docling bug report for headers not connected to paragraph content

## First Header

Just a regular paragraph, note header is to the left BUT right aligned. This paragraph is justified but to the  right  of  the  header.  There  should  be  a  line below.

## Second Header

Term: Yet more text. Not a lot, just a few sentences. Really tempting to use Ipsum here.

AnotherTerm: I'm  getting  lazy.  I  may  just  copy  and paste  the  rest  to  save  me  some  time.  I  can't  keep typing rubbish for ever.

AnotherOne: This is a long paragraph. Yep, I'm about to copy and paste here. I'm getting lazy. I may just copy and paste the rest to save me some time. I can't keep typing rubbish for ever.I'm getting lazy. I may just copy and paste the rest to save me some time. I can't keep typing rubbish for ever.I'm getting lazy. I  may  just  copy  and  paste  the  rest  to  save  me  some time. I can't keep typing rubbish for ever.

LastOne: Yet  more  text.  Not  a  lot,  just  a  few sentences. Really tempting to use Ipsum here.

## Third Header

Just  a  couple  of  paragraphs  here.  A  few  sentences each,  longer  than  I  really  can  be  bothered  to  type here. I'm really getting bored typing this. No, more, please!

This  is  a  long  paragraph.  but  shortened  to  get  a single page demo.

(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Docling version

Docling version: 2.102.1 Docling Core version: 2.82.0 Docling IBM Models version: 3.13.3 Docling Parse version: 6.2.0 Python: cpython-312 (3.12.5) Platform: Windows-11-10.0.22631-SP0

Python version

Python 3.12.5

clach04 avatar Jun 18 '26 01:06 clach04

To clarify, correctly identifies headers BUT then groups them all together rather than with the paragraph blocks associated with each header.

clach04 avatar Jun 18 '26 01:06 clach04

Hi @clach04, I dug into this and found out that: The order comes from ReadingOrderPredictor in docling-ibm-models (reading_order/reading_order_rb.py), which docling feeds via ReadingOrderModel. It builds "what's directly above me" links (up_map/ dn_map) using a single rule: link i -> j only if i is strictly above j and they overlap horizontally.

Because the left-column headers never horizontally overlap the right-column paragraphs, they form two disjoint vertical chains: heads: [Header 1, Paragraph 1] Header 1 -> Header 2 -> Header 3 Paragraph 1 -> Paragraph 2 -> Paragraph 3

The two chain-starts are sorted by PageElement.__lt__, which for non-overlapping boxes returns "leftmost first", so the header column wins. The DFS then walks the entire header chain before reaching the paragraph chain → all headers, then all paragraphs.

Two details that make this worse:

  • _init_l2r_map (the only thing that could interleave left/right neighbors) is gated behind a hardcoded if (False and ...), so it's effectively dead code

  • _do_horizontal_dilation only widens an element toward its own same-column neighbors. A header and its paragraph share the same vertical band (side-by-side, not above/below), so no horizontal widening ever creates an "above" relationship to link them

Proposed approach

The fix belongs upstream in docling-ibm-models, since that's where the algorithm lives. The most targeted option:

Re-enable a real left-to-right linking pass: when a (narrow) element is vertically aligned with the element immediately to its right and there's nothing between them, link left -> right so the two column-chains interleave (header -> its paragraph -> next header -> ...). This is essentially reviving the disabled l2r_map logic with a sane condition (vertical IoU + adjacency), and chaining it into up_map/dn_map so the DFS produces the interleaved order.

The thing to watch is not regressing genuine multi-column documents (where the left column should be read fully before the right). The distinguishing signal is row alignment: a side-heading layout has left items each aligned to a single right block, whereas a true two-column layout has a tall left column spanning many right rows.

Happy to open a PR with a fix and regression tests covering both the side-heading case and the normal two-column case, if this aligns with the maintainer's direction. Thanks!

DanielNg0729 avatar Jun 18 '26 02:06 DanielNg0729

@DanielNg0729 that was fast! It feels like you figured this out faster than it took me to create the test case (and I did use an LM to help a little 😆 ).

I can't comment on changing direction (sounds reasonable to me... knowing nothing abou the internals 😆 ), but if you do make a change, I'd be interested in testing with some of the files I have.

clach04 avatar Jun 18 '26 02:06 clach04

Classification labels added: priority:medium, pdf.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 16 '26 15:11 github-actions[bot]