Skip to content

Commit 02410ef

Browse files
authored
Merge pull request #119 from anatolec/fix/annotations-with-render-text
feat: add allow_clickable_annotations_with_text_rendering option
2 parents 8eca42f + 7f75e4e commit 02410ef

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

streamlit_pdf_viewer/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def pdf_viewer(
3939
scroll_to_page: Optional[int] = None,
4040
scroll_to_annotation: Optional[int] = None,
4141
on_annotation_click: Optional[Callable[[dict], None]] = None,
42+
allow_clickable_annotations_with_text_rendering: bool = False,
4243
):
4344
"""
4445
pdf_viewer function to display a PDF file in a Streamlit app.
@@ -59,6 +60,7 @@ def pdf_viewer(
5960
:param scroll_to_page: Scroll to a specific page in the PDF. The parameter is an integer, which represent the positional value of the page. E.g. 1, will be the first page. Defaults to None.
6061
:param scroll_to_annotation: Scroll to a specific annotation in the PDF. The parameter is an integer, which represent the positional value of the annotation. E.g. 1, will be the first annotation. Defaults to None.
6162
:param on_annotation_click: A callback function that will be called when an annotation is clicked. The function should accept a single argument, which is the annotation that was clicked. Defaults to None.
63+
:param allow_clickable_annotations_with_text_rendering: When True, annotations remain clickable even when render_text is enabled. Note that text selection will not work through annotation areas. Defaults to False.
6264
6365
The function reads the PDF file (from a file path, URL, or binary data), encodes it in base64,
6466
and uses a Streamlit component to render it in the app. It supports optional annotations and adjustable margins.
@@ -132,7 +134,8 @@ def pdf_viewer(
132134
viewer_align=viewer_align,
133135
show_page_separator=show_page_separator,
134136
scroll_to_page=scroll_to_page,
135-
scroll_to_annotation=scroll_to_annotation
137+
scroll_to_annotation=scroll_to_annotation,
138+
allow_clickable_annotations_with_text_rendering=allow_clickable_annotations_with_text_rendering
136139
)
137140

138141
# Execute the custom callback function

streamlit_pdf_viewer/frontend/src/PdfViewer.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default {
8383
const isRendering = ref(false);
8484
8585
const renderText = props.args.render_text === true;
86+
const allowClickableAnnotationsWithTextRendering = props.args.allow_clickable_annotations_with_text_rendering === true;
8687
8788
const parseWidthValue = (widthValue, fallbackValue = window.innerWidth) => {
8889
if (typeof widthValue === "string" && widthValue.endsWith("%")) {
@@ -158,11 +159,13 @@ export default {
158159
border = "solid"
159160
}
160161
annotationDiv.style.outline = `${props.args.annotation_outline_size * scale}px ${border} ${annotation.color}`;
161-
annotationDiv.style.cursor = renderText ? 'text' : 'pointer';
162-
annotationDiv.style.pointerEvents = renderText ? 'none' : 'auto';
163-
annotationDiv.style.zIndex = 10;
162+
const annotationsClickable = !renderText || allowClickableAnnotationsWithTextRendering;
163+
annotationDiv.style.cursor = annotationsClickable ? 'pointer' : 'text';
164+
annotationDiv.style.pointerEvents = annotationsClickable ? 'auto' : 'none';
165+
// Use higher z-index when annotations need to be above text layer (z-index 11)
166+
annotationDiv.style.zIndex = (renderText && allowClickableAnnotationsWithTextRendering) ? 12 : 10;
164167
165-
if (!renderText) {
168+
if (annotationsClickable) {
166169
annotationDiv.addEventListener('click', () => {
167170
Streamlit.setComponentValue({
168171
clicked_annotation: {index: annotation.id, ...annotation},

0 commit comments

Comments
 (0)