Skip to content

Commit 44bc1fc

Browse files
authored
ENH: Allow free-text annotation to have transparent border/background (#1664)
1 parent b14dabb commit 44bc1fc

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

pypdf/generic/_annotations.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def free_text(
6868
italic: bool = False,
6969
font_size: str = "14pt",
7070
font_color: str = "000000",
71-
border_color: str = "000000",
72-
background_color: str = "ffffff",
71+
border_color: Optional[str] = "000000",
72+
background_color: Optional[str] = "ffffff",
7373
) -> DictionaryObject:
7474
"""
7575
Add text in a rectangle to a page.
@@ -83,9 +83,10 @@ def free_text(
8383
italic: Print the text in italic
8484
font_size: How big the text will be, e.g. '14pt'
8585
font_color: Hex-string for the color, e.g. cdcdcd
86-
border_color: Hex-string for the border color, e.g. cdcdcd
86+
border_color: Hex-string for the border color, e.g. cdcdcd.
87+
Use `None` for no border.
8788
background_color: Hex-string for the background of the annotation,
88-
e.g. cdcdcd
89+
e.g. cdcdcd. Use `None` for transparent background.
8990
9091
Returns:
9192
A dictionary object representing the annotation.
@@ -98,10 +99,11 @@ def free_text(
9899
font_str = f"{font_str}{font} {font_size}"
99100
font_str = f"{font_str};text-align:left;color:#{font_color}"
100101

101-
bg_color_str = ""
102-
for st in hex_to_rgb(border_color):
103-
bg_color_str = f"{bg_color_str}{st} "
104-
bg_color_str = f"{bg_color_str}rg"
102+
default_appearance_string = ""
103+
if border_color:
104+
for st in hex_to_rgb(border_color):
105+
default_appearance_string = f"{default_appearance_string}{st} "
106+
default_appearance_string = f"{default_appearance_string}rg"
105107

106108
free_text = DictionaryObject()
107109
free_text.update(
@@ -112,14 +114,21 @@ def free_text(
112114
NameObject("/Contents"): TextStringObject(text),
113115
# font size color
114116
NameObject("/DS"): TextStringObject(font_str),
115-
# border color
116-
NameObject("/DA"): TextStringObject(bg_color_str),
117-
# background color
118-
NameObject("/C"): ArrayObject(
119-
[FloatObject(n) for n in hex_to_rgb(background_color)]
120-
),
117+
NameObject("/DA"): TextStringObject(default_appearance_string),
121118
}
122119
)
120+
if border_color is None:
121+
# Border Style
122+
free_text[NameObject("/BS")] = DictionaryObject(
123+
{
124+
# width of 0 means no border
125+
NameObject("/W"): NumberObject(0)
126+
}
127+
)
128+
if background_color is not None:
129+
free_text[NameObject("/C")] = ArrayObject(
130+
[FloatObject(n) for n in hex_to_rgb(background_color)]
131+
)
123132
return free_text
124133

125134
@staticmethod

tests/test_generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ def test_annotation_builder_free_text():
720720
italic=True,
721721
font_size="20pt",
722722
font_color="00ff00",
723-
border_color="0000ff",
724-
background_color="cdcdcd",
723+
border_color=None,
724+
background_color=None,
725725
)
726726
writer.add_annotation(0, free_text_annotation)
727727

0 commit comments

Comments
 (0)