-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtest_annotations.py
400 lines (336 loc) · 10.4 KB
/
test_annotations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""Test the pypdf.annotations submodule."""
from pathlib import Path
import pytest
from pypdf import PdfReader, PdfWriter
from pypdf.annotations import (
AnnotationDictionary,
Ellipse,
FreeText,
Highlight,
Line,
Link,
Polygon,
PolyLine,
Popup,
Rectangle,
Text,
)
from pypdf.errors import DeprecationError
from pypdf.generic import ArrayObject, FloatObject, NumberObject
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
def test_ellipse_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
with pytest.raises(DeprecationError):
ellipse_annotation = Ellipse(
rect=(50, 550, 500, 650),
interiour_color="ff0000",
)
ellipse_annotation = Ellipse(
rect=(50, 550, 500, 650),
interior_color="ff0000",
)
writer.add_annotation(0, ellipse_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_rectangle_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
rectangle_annotation = Rectangle(
rect=(50, 550, 500, 650),
interior_color="ff0000",
)
writer.add_annotation(0, rectangle_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_text_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
text_annotation = Text(
text="Hello World\nThis is the second line!",
rect=(50, 550, 500, 650),
open=True,
)
writer.add_annotation(0, text_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_free_text_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
free_text_annotation = FreeText(
text="Hello World - bold and italic\nThis is the second line!",
rect=(50, 550, 200, 650),
font="Arial",
bold=True,
italic=True,
font_size="20pt",
font_color="00ff00",
border_color=None,
background_color=None,
)
writer.add_annotation(0, free_text_annotation)
free_text_annotation = FreeText(
text="Another free text annotation (not bold, not italic)",
rect=(500, 550, 200, 650),
font="Arial",
bold=False,
italic=False,
font_size="20pt",
font_color="00ff00",
border_color="0000ff",
background_color="cdcdcd",
)
writer.add_annotation(0, free_text_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_free_text_annotation__font_specifier():
free_text_annotation = FreeText(
text="Hello World",
rect=(0, 0, 0, 0),
)
assert free_text_annotation["/DS"] == "font: normal normal 14pt Helvetica;text-align:left;color:#000000"
free_text_annotation = FreeText(
text="Hello World",
rect=(50, 550, 200, 650),
font="Arial",
bold=True,
italic=True,
font_size="20pt",
font_color="00ff00",
border_color=None,
background_color=None,
)
assert free_text_annotation["/DS"] == "font: italic bold 20pt Arial;text-align:left;color:#00ff00"
def test_annotationdictionary():
a = AnnotationDictionary()
a.flags = 123
assert a.flags == 123
def test_polygon(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
with pytest.raises(ValueError):
Polygon(
vertices=[],
)
annotation = Polygon(
vertices=[(50, 550), (200, 650), (70, 750), (50, 700)],
)
writer.add_annotation(0, annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_polyline(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
with pytest.raises(ValueError):
PolyLine(
vertices=[],
)
annotation = PolyLine(
vertices=[(50, 550), (200, 650), (70, 750), (50, 700)],
)
writer.add_annotation(0, annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_line(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
line_annotation = Line(
text="Hello World\nLine2",
rect=(50, 550, 200, 650),
p1=(50, 550),
p2=(200, 650),
)
writer.add_annotation(0, line_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_square(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
with pytest.raises(DeprecationError):
square_annotation = Rectangle(
rect=(50, 550, 200, 650), interiour_color="ff0000"
)
square_annotation = Rectangle(rect=(50, 550, 200, 650), interior_color="ff0000")
writer.add_annotation(0, square_annotation)
square_annotation = Rectangle(
rect=(40, 400, 150, 450),
)
writer.add_annotation(0, square_annotation)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_highlight(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
highlight_annotation = 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=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
highlight_annotation = 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:
writer.write(fp)
def test_link(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
# Part 1: Too many args
with pytest.raises(ValueError):
Link(
rect=(50, 550, 200, 650),
url="https://martin-thoma.com/",
target_page_index=3,
)
# Part 2: Too few args
with pytest.raises(ValueError):
Link(
rect=(50, 550, 200, 650),
)
# Part 3: External Link
link_annotation = Link(
rect=(50, 50, 100, 100),
url="https://martin-thoma.com/",
border=[1, 0, 6, [3, 2]],
)
writer.add_annotation(0, link_annotation)
# Part 4: Internal Link
link_annotation = Link(
rect=(100, 100, 300, 200),
target_page_index=1,
border=[50, 10, 4],
)
writer.add_annotation(0, link_annotation)
for page in reader.pages[1:]:
writer.add_page(page)
# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)
def test_popup(caplog):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)
# Act
text_annotation = Text(
title_bar="hello world",
text="Hello World\nThis is the second line!",
rect=(50, 550, 200, 650),
open=True,
)
ta = writer.add_annotation(0, text_annotation)
popup_annotation = Popup(
rect=(50, 550, 200, 650),
open=True,
parent=ta, # prefer to use for evolutivity
)
writer.add_annotation(writer.pages[0], popup_annotation)
Popup(
rect=(50, 550, 200, 650),
open=True,
parent=True, # broken parameter # type: ignore
)
assert "Unregistered Parent object : No Parent field set" in caplog.text
target = "annotated-pdf-popup.pdf"
writer.write(target)
Path(target).unlink() # comment this out for manual inspection