Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Highlighted Text Cannot Be Printed #2604

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pypdf/annotations/_markup_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union

from ..constants import AnnotationFlag
from ..generic import ArrayObject, DictionaryObject
from ..generic._base import (
BooleanObject,
Expand Down Expand Up @@ -233,6 +234,7 @@ def __init__(
rect: Union[RectangleObject, Tuple[float, float, float, float]],
quad_points: ArrayObject,
highlight_color: str = "ff0000",
printing: bool = False,
**kwargs: Any,
):
super().__init__(**kwargs)
Expand All @@ -246,6 +248,8 @@ def __init__(
),
}
)
if printing:
self.flags = AnnotationFlag.PRINT


class Ellipse(MarkupAnnotation):
Expand Down
5 changes: 4 additions & 1 deletion pypdf/generic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def highlight(
rect: Union[RectangleObject, Tuple[float, float, float, float]],
quad_points: ArrayObject,
highlight_color: str = "ff0000",
printing: bool = False,
) -> DictionaryObject:
"""
Add a highlight annotation to the document.
Expand All @@ -319,6 +320,8 @@ def highlight(
quad_points: An ArrayObject of 8 FloatObjects. Must match a word or
a group of words, otherwise no highlight will be shown.
highlight_color: The color used for the highlight.
printing: Whether to print out the highlight annotation when the page
is printed.

Returns:
A dictionary object representing the annotation.
Expand All @@ -329,7 +332,7 @@ def highlight(
from ..annotations import Highlight

return Highlight(
rect=rect, quad_points=quad_points, highlight_color=highlight_color
rect=rect, quad_points=quad_points, highlight_color=highlight_color, printing=printing
)

@staticmethod
Expand Down
33 changes: 33 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,41 @@ def test_annotation_builder_highlight(pdf_file_path):
FloatObject(705.4493),
]
),
printing=False
)
writer.add_annotation(0, highlight_annotation)
for annot in writer.pages[0]["/Annots"]:
obj = annot.get_object()
subtype = obj["/Subtype"]
if subtype == "/Highlight":
assert "/F" not in obj or obj["/F"] == NumberObject(0)

writer.add_page(page)
# Act
with pytest.warns(DeprecationWarning):
highlight_annotation = AnnotationBuilder.highlight(
rect=(95.79332, 704.31777, 138.55779, 724.6855),
highlight_color="ff0000",
quad_points=ArrayObject(
[
FloatObject(100.060779),
FloatObject(723.55398),
FloatObject(134.29033),
FloatObject(723.55398),
FloatObject(100.060779),
FloatObject(705.4493),
FloatObject(134.29033),
FloatObject(705.4493),
]
),
printing=True
)
writer.add_annotation(1, highlight_annotation)
for annot in writer.pages[1]["/Annots"]:
obj = annot.get_object()
subtype = obj["/Subtype"]
if subtype == "/Highlight":
assert obj["/F"] == NumberObject(4)

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