How to detect and remove repeated headers/footers from PDF pages?
Hi, thank you again for the amazing work on Docling!
I'm working with a large number of PDF documents that have consistent headers and footers on every page. These headers/footers often include logos, page numbers, or section titles — and in my case, Docling identifies them as images and saves them as separate elements.
My Question:
Is there a recommended way in Docling to detect and remove repeated headers and footers across pages?
Ideally, I would like to:
- define a fixed vertical area (e.g. top/bottom 5% of the page) to ignore during processing, or
- enable some kind of content de-duplication across pages (like comparing bounding boxes or hashes of images/text blocks), or
- configure Docling to treat those areas differently (e.g. exclude them from output, or mark them as low-priority).
I found a related issue in the tracker, but it seems there was no concrete solution offered yet. The related issue : Elimination of Document Header and Footers But my pdf's header and footer are images, so this code didn't work at all. I'm wondering if there’s any internal option or upcoming feature for this kind of layout-level filtering?
Any suggestions or best practices would be greatly appreciated!
Thanks again for all your hard work and this fantastic tool.
Docling doesn't have a built-in feature to automatically remove repeated image-based headers or footers, or to ignore a fixed vertical area during PDF processing. However, all elements—including images—are annotated with bounding boxes and content layers, so you can implement custom filtering. For example, after conversion, you can filter out images whose bounding boxes fall within the top/bottom 5% of the page height, or use the PictureItem._image_to_hexhash method to identify and remove repeated images across pages by their hash value.
You can also use content layer filtering (e.g., included_content_layers={ContentLayer.BODY}) during export to exclude items labeled as FURNITURE, but this depends on accurate labeling by the layout model and may not always catch images in headers/footers. There are community workarounds for text-based headers/footers, but for images, the best practice is to use bounding box position and image hashes to identify and remove them programmatically before export. See this discussion and code example for more details and user-shared approaches.
There are no upcoming features or roadmap items for automatic image-based header/footer removal at this time. If you need help with a specific script or workflow, let me know!
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
The second part of the answer above is indeed pointing in the right direction.
Detecting and excluding headers/footers is one of the key features of the Docling processing. These components are usually identified as furniture elements which (by default) are excluded from markdown and html exports.
The detection is purely governed by the layout model. Since you mention "complex headers", it could be this is a type of layout with low coverage in the training data. We will have new layout models being released which could address this case.
There is currently no rule-based mechanism to manipulate the detection mechanism during the processing.
Since you are proposing to use a fixed geometric rule, I'm wondering if this could be done in a simple post-processing of the DoclingDocument. @dosu please propose a code which uses the document.iterate_items() function and updates the content_layer of the item if the geometric position (in prov field) is above or below the given headers/footers margins.
@dosu How can we filter based on text? I am trying this
from copy import deepcopy
# If needed, import DocItemLabel and ContentLayer enums or use strings
doc_copy = deepcopy(doc)
# Collect the self_refs of header/footer texts via iteration including furniture
header_footer_refs = set()
header_footer_texts = set()
for item, _level in doc_copy.iterate_items(
included_content_layers={'furniture', 'body'}
):
label = getattr(item, 'label', None)
content_layer = getattr(item, 'content_layer', None)
text = getattr(item, 'text', None)
self_ref = getattr(item, 'self_ref', None)
# Detect headers/footers in furniture
if content_layer == 'furniture' and label in {'page_header', 'page_footer'}:
if self_ref:
header_footer_refs.add(self_ref)
if text:
header_footer_texts.add(text)
# Filter out texts whose self_ref is in header_footer_refs
filtered_texts = [
t for t in doc_copy.texts
if getattr(t, 'self_ref', None) not in header_footer_refs
]
# Additionally filter 'section_header' in body whose text matches header/footer text to catch tricky cases
filtered_texts = [
t for t in filtered_texts
if not (getattr(t, 'label', None) == 'section_header' and
getattr(t, 'content_layer', None) == 'body' and
getattr(t, 'text', None) in header_footer_texts)
]
doc_copy.texts = filtered_texts
return doc_copy
doc_clean = filter_headers_and_footers_via_iteration(doc)
md = doc_clean.export_to_markdown(page_break_placeholder="<--- PAGE BREAK --->")
but get error ValidationError: 1 validation error for MarkdownDocSerializer doc Value error, Document hierachy is inconsistent. [type=value_error, input_value=DoclingDocument(schema_na...mage=None, page_no=10)}), input_type=DoclingDocument] For further information visit https://errors.pydantic.dev/2.10/v/value_error
I have also tried removing reference from body, group etc but still same error.
The ValidationError happens because directly filtering doc.texts breaks the internal hierarchy and references in DoclingDocument. Instead, use the built-in delete_items method, which safely removes items and updates all parent/child and group references to keep the document valid (source).
Here's how you can do it:
from copy import deepcopy
doc_copy = deepcopy(doc)
to_remove = []
for item, _ in doc_copy.iterate_items(included_content_layers={'furniture', 'body'}):
label = getattr(item, 'label', None)
content_layer = getattr(item, 'content_layer', None)
text = getattr(item, 'text', None)
# Mark page_header/page_footer in furniture
if content_layer == 'furniture' and label in {'page_header', 'page_footer'}:
to_remove.append(item)
# Mark section_header in body if its text matches a header/footer
if content_layer == 'body' and label == 'section_header':
# You may want to build a set of header/footer texts first, as in your code
pass # see below
# If you want to match section_headers by text, build a set of header/footer texts first:
header_footer_texts = set(
getattr(item, 'text', None)
for item, _ in doc_copy.iterate_items(included_content_layers={'furniture'})
if getattr(item, 'label', None) in {'page_header', 'page_footer'}
)
for item, _ in doc_copy.iterate_items(included_content_layers={'body'}):
if getattr(item, 'label', None) == 'section_header' and getattr(item, 'text', None) in header_footer_texts:
to_remove.append(item)
# Now safely remove all marked items
doc_copy.delete_items(node_items=to_remove)
md = doc_copy.export_to_markdown(page_break_placeholder="<--- PAGE BREAK --->")
This approach keeps the document structure consistent and avoids validation errors (source).
To reply, just mention @dosu.
Share context across your team and agents. Try Dosu.
This works. Thanks for your support @dosu
@dosu Could we extend BasePageModel and remove the headers/footers there instead of having a post-processing step?
@dosu how do you obtain the bounding boxes for images? I want to filter out images whose bounding boxes fall within the top/bottom 5% of the page height, as you suggested.
Classification labels added: enhancement, priority:medium, pdf.
These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.