unstructured icon indicating copy to clipboard operation
unstructured copied to clipboard

feat/Infer the hierarchical heading/title levels such as H1, H2, H3, H4 for PDFs

Open codebymikey opened this issue 10 months ago • 4 comments

Is your feature request related to a problem? Please describe. This was supposedly raised and addressed as part of #1354

But I can't seem to get any metadata regarding the inferred heading of any content I test with.

All Title elements all have the same level at the moment which isn't an accurate representation of the parsed document.

Describe the solution you'd like The parser should be able to infer PDF hierarchy in a couple ways:

  • Through the document outline, e.g. example PDF linked on https://pdfa.org/techniques-for-accessible-pdf/headings-with-different-levels-correctly-tagged/UA1_Tpdf-H_03/
  • Calculate and infer the heading size relative to the size of the page (and other headings within the same page/document).

Describe alternatives you've considered N/A

Additional context

codebymikey avatar Jan 22 '26 12:01 codebymikey

@PastelStorm Can I work on this? I can

statxc avatar Mar 31 '26 13:03 statxc

Hi @statxc, I'm sure MRs are more than welcome.

The reason the previous one didn't get very far was because it was written by AI and didn't address the actual issue/feedback well enough.

codebymikey avatar Apr 02 '26 07:04 codebymikey

Hi, @codebymikey I opened a PR for this issue. Please let me know if anything else is needed to update. Thanks.

statxc avatar Apr 07 '26 17:04 statxc

Until one of the open PRs is merged, one workaround is to compute the levels outside Unstructured and then match them to the Title elements by text and page number. When the PDF has an outline, pypdf (reader.outline) or PyMuPDF (doc.get_toc()) already gives each entry a level and a page. When it does not, ranking the distinct heading font sizes, combined with section numbering such as 2.1 or 3.2.4, is usually a reasonable fallback.

For transparency, I built Section Anything (open source, Apache-2.0), which does this matching: outline first, then the printed table of contents, then font size and numbering. It only handles the heading structure and needs a text layer, so Unstructured would still do the element extraction.

Omniwot avatar Oct 29 '26 01:10 Omniwot