Skip to content

Commit 8245fbc

Browse files
authored
BUG: Fix font specificier for FreeText annotation (#2893)
Closes #1435.
1 parent 96b46ad commit 8245fbc

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pypdf/annotations/_markup_annotations.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,17 @@ def __init__(
103103
self[NameObject("/Subtype")] = NameObject("/FreeText")
104104
self[NameObject("/Rect")] = RectangleObject(rect)
105105

106+
# Table 225 of the 1.7 reference ("CSS2 style attributes used in rich text strings")
106107
font_str = "font: "
107-
if bold:
108-
font_str = f"{font_str}bold "
109108
if italic:
110109
font_str = f"{font_str}italic "
111-
font_str = f"{font_str}{font} {font_size}"
110+
else:
111+
font_str = f"{font_str}normal "
112+
if bold:
113+
font_str = f"{font_str}bold "
114+
else:
115+
font_str = f"{font_str}normal "
116+
font_str = f"{font_str}{font_size} {font}"
112117
font_str = f"{font_str};text-align:left;color:#{font_color}"
113118

114119
default_appearance_string = ""

tests/test_annotations.py

+20
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ def test_free_text_annotation(pdf_file_path):
132132
writer.write(fp)
133133

134134

135+
def test_free_text_annotation__font_specifier():
136+
free_text_annotation = FreeText(
137+
text="Hello World",
138+
rect=(0, 0, 0, 0),
139+
)
140+
assert free_text_annotation["/DS"] == "font: normal normal 14pt Helvetica;text-align:left;color:#000000"
141+
free_text_annotation = FreeText(
142+
text="Hello World",
143+
rect=(50, 550, 200, 650),
144+
font="Arial",
145+
bold=True,
146+
italic=True,
147+
font_size="20pt",
148+
font_color="00ff00",
149+
border_color=None,
150+
background_color=None,
151+
)
152+
assert free_text_annotation["/DS"] == "font: italic bold 20pt Arial;text-align:left;color:#00ff00"
153+
154+
135155
def test_annotationdictionary():
136156
a = AnnotationDictionary()
137157
a.flags = 123

0 commit comments

Comments
 (0)