Skip to content

Commit 52e4a6e

Browse files
committed
MAINT: _appearance_stream: Delay escaping parentheses
Delay escaping parentheses until after selected text for a choice field is compared to a line of text.
1 parent 48d1236 commit 52e4a6e

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

pypdf/generic/_appearance_stream.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def _appearance_stream_data(
4848

4949
ap_stream = f"q\n/Tx BMC \nq\n1 1 {rect.width - 1} {rect.height - 1} re\nW\nBT\n{default_appearance}\n".encode()
5050
for line_number, line in enumerate(text.replace("\n", "\r").split("\r")):
51+
# Escape parentheses (PDF 1.7 reference, table 3.2, Literal Strings)
52+
line = line.replace("\\", "\\\\").replace("(", r"\(").replace(")", r"\)")
5153
if selection and line in selection:
5254
# may be improved but cannot find how to get fill working => replaced with lined box
5355
ap_stream += (
@@ -196,9 +198,6 @@ def from_text_annotation(
196198
text = field.get("/V", "")
197199
selection = []
198200

199-
# Escape parentheses (PDF 1.7 reference, table 3.2, Literal Strings)
200-
text = text.replace("\\", "\\\\").replace("(", r"\(").replace(")", r"\)")
201-
202201
# Create the TextStreamAppearance instance
203202
new_appearance_stream = cls(
204203
text, selection, font_glyph_byte_map, rect, font_name, font_size, font_color, multiline

0 commit comments

Comments
 (0)