Skip to content

Commit bcc8214

Browse files
authored
BUG: Highlighted Text Cannot Be Printed (#2604)
Add print flag for highlight
1 parent e92b20e commit bcc8214

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

pypdf/annotations/_markup_annotations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from abc import ABC
33
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
44

5+
from ..constants import AnnotationFlag
56
from ..generic import ArrayObject, DictionaryObject
67
from ..generic._base import (
78
BooleanObject,
@@ -233,6 +234,7 @@ def __init__(
233234
rect: Union[RectangleObject, Tuple[float, float, float, float]],
234235
quad_points: ArrayObject,
235236
highlight_color: str = "ff0000",
237+
printing: bool = False,
236238
**kwargs: Any,
237239
):
238240
super().__init__(**kwargs)
@@ -246,6 +248,8 @@ def __init__(
246248
),
247249
}
248250
)
251+
if printing:
252+
self.flags = AnnotationFlag.PRINT
249253

250254

251255
class Ellipse(MarkupAnnotation):

pypdf/generic/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def highlight(
309309
rect: Union[RectangleObject, Tuple[float, float, float, float]],
310310
quad_points: ArrayObject,
311311
highlight_color: str = "ff0000",
312+
printing: bool = False,
312313
) -> DictionaryObject:
313314
"""
314315
Add a highlight annotation to the document.
@@ -319,6 +320,8 @@ def highlight(
319320
quad_points: An ArrayObject of 8 FloatObjects. Must match a word or
320321
a group of words, otherwise no highlight will be shown.
321322
highlight_color: The color used for the highlight.
323+
printing: Whether to print out the highlight annotation when the page
324+
is printed.
322325
323326
Returns:
324327
A dictionary object representing the annotation.
@@ -329,7 +332,7 @@ def highlight(
329332
from ..annotations import Highlight
330333

331334
return Highlight(
332-
rect=rect, quad_points=quad_points, highlight_color=highlight_color
335+
rect=rect, quad_points=quad_points, highlight_color=highlight_color, printing=printing
333336
)
334337

335338
@staticmethod

tests/test_generic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,41 @@ def test_annotation_builder_highlight(pdf_file_path):
883883
FloatObject(705.4493),
884884
]
885885
),
886+
printing=False
886887
)
887888
writer.add_annotation(0, highlight_annotation)
889+
for annot in writer.pages[0]["/Annots"]:
890+
obj = annot.get_object()
891+
subtype = obj["/Subtype"]
892+
if subtype == "/Highlight":
893+
assert "/F" not in obj or obj["/F"] == NumberObject(0)
894+
895+
writer.add_page(page)
896+
# Act
897+
with pytest.warns(DeprecationWarning):
898+
highlight_annotation = AnnotationBuilder.highlight(
899+
rect=(95.79332, 704.31777, 138.55779, 724.6855),
900+
highlight_color="ff0000",
901+
quad_points=ArrayObject(
902+
[
903+
FloatObject(100.060779),
904+
FloatObject(723.55398),
905+
FloatObject(134.29033),
906+
FloatObject(723.55398),
907+
FloatObject(100.060779),
908+
FloatObject(705.4493),
909+
FloatObject(134.29033),
910+
FloatObject(705.4493),
911+
]
912+
),
913+
printing=True
914+
)
915+
writer.add_annotation(1, highlight_annotation)
916+
for annot in writer.pages[1]["/Annots"]:
917+
obj = annot.get_object()
918+
subtype = obj["/Subtype"]
919+
if subtype == "/Highlight":
920+
assert obj["/F"] == NumberObject(4)
888921

889922
# Assert: You need to inspect the file manually
890923
with open(pdf_file_path, "wb") as fp:

0 commit comments

Comments
 (0)