From ec38a0bc55fbaa312a83df1cc1a0016cf523ba1b Mon Sep 17 00:00:00 2001 From: Sheng Date: Fri, 19 Apr 2024 11:02:09 -0700 Subject: [PATCH 1/3] Add print flag for highlight --- pypdf/annotations/_markup_annotations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pypdf/annotations/_markup_annotations.py b/pypdf/annotations/_markup_annotations.py index 23c641e1e..28956e8a5 100644 --- a/pypdf/annotations/_markup_annotations.py +++ b/pypdf/annotations/_markup_annotations.py @@ -239,6 +239,7 @@ def __init__( self.update( { NameObject("/Subtype"): NameObject("/Highlight"), + NameObject("/F"): NumberObject(4), NameObject("/Rect"): RectangleObject(rect), NameObject("/QuadPoints"): quad_points, NameObject("/C"): ArrayObject( From b3ae07cb4a5461b9deb90eab53067806c008acbd Mon Sep 17 00:00:00 2001 From: Nifury Date: Tue, 30 Apr 2024 07:47:25 -0700 Subject: [PATCH 2/3] Add test and make printing optional --- pypdf/annotations/_markup_annotations.py | 5 ++++- pypdf/generic/__init__.py | 5 ++++- tests/test_generic.py | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pypdf/annotations/_markup_annotations.py b/pypdf/annotations/_markup_annotations.py index 28956e8a5..b953900c7 100644 --- a/pypdf/annotations/_markup_annotations.py +++ b/pypdf/annotations/_markup_annotations.py @@ -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, @@ -233,13 +234,13 @@ 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) self.update( { NameObject("/Subtype"): NameObject("/Highlight"), - NameObject("/F"): NumberObject(4), NameObject("/Rect"): RectangleObject(rect), NameObject("/QuadPoints"): quad_points, NameObject("/C"): ArrayObject( @@ -247,6 +248,8 @@ def __init__( ), } ) + if printing: + self.flags = AnnotationFlag.PRINT class Ellipse(MarkupAnnotation): diff --git a/pypdf/generic/__init__.py b/pypdf/generic/__init__.py index 878a17e49..e8fe10944 100644 --- a/pypdf/generic/__init__.py +++ b/pypdf/generic/__init__.py @@ -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. @@ -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. @@ -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 diff --git a/tests/test_generic.py b/tests/test_generic.py index e772618e6..d704c883f 100644 --- a/tests/test_generic.py +++ b/tests/test_generic.py @@ -883,8 +883,14 @@ def test_annotation_builder_highlight(pdf_file_path): FloatObject(705.4493), ] ), + printing=True, ) writer.add_annotation(0, highlight_annotation) + for annot in writer.pages[0]["/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: From a5f4dfdd135dc02456fe79f4f49f4082b7b7237a Mon Sep 17 00:00:00 2001 From: Nifury Date: Tue, 30 Apr 2024 08:13:38 -0700 Subject: [PATCH 3/3] Update test to cover both cases --- tests/test_generic.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_generic.py b/tests/test_generic.py index d704c883f..24da063a2 100644 --- a/tests/test_generic.py +++ b/tests/test_generic.py @@ -883,10 +883,37 @@ def test_annotation_builder_highlight(pdf_file_path): FloatObject(705.4493), ] ), - printing=True, + 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":