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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
"""Test the pypdf.annotations submodule."""
from io import BytesIO
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 PdfReadError
from pypdf.generic import ArrayObject, FloatObject, NumberObject
from . import RESOURCE_ROOT, get_data_from_url
def test_ellipse(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
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_text(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(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__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_annotation_dictionary():
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,
match=r"A polyline needs at least 1 vertex with two coordinates",
):
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_rectangle(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
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
def test_markup_annotation_in_reply_to():
"""Test that a reply annotation gets /IRT, /RT, and /NM after a write/read cycle."""
writer = PdfWriter(clone_from=RESOURCE_ROOT / "crazyones.pdf")
parent = Text(
text="Parent comment",
rect=(50, 550, 200, 650),
open=True,
)
parent_ref = writer.add_annotation(0, parent)
reply = Text(
text="Reply to parent",
rect=(50, 550, 200, 650),
in_reply_to=parent_ref,
)
writer.add_annotation(0, reply)
assert "/IRT" in reply
assert reply["/IRT"].get_object() is parent_ref
assert reply["/RT"] == "/R"
assert "/NM" in reply
assert "/NM" not in parent_ref
buf = BytesIO()
writer.write(buf)
reader2 = PdfReader(buf)
annots = reader2.pages[0]["/Annots"]
assert len(annots) == 2
reply_obj = annots[1].get_object()
assert reply_obj["/IRT"].get_object()["/Contents"] == "Parent comment"
assert reply_obj["/NM"] == reply["/NM"]
def test_markup_annotation_in_reply_to_group_type():
"""Test that a grouped annotation sets /RT to /Group."""
writer = PdfWriter(clone_from=RESOURCE_ROOT / "crazyones.pdf")
parent = Text(
text="Parent",
rect=(50, 550, 200, 650),
)
parent_ref = writer.add_annotation(0, parent)
grouped = Text(
text="Grouped with parent",
rect=(50, 550, 200, 650),
in_reply_to=parent_ref,
reply_type="Group",
)
writer.add_annotation(0, grouped)
assert grouped["/RT"] == "/Group"
assert "/IRT" in grouped
assert "/NM" in grouped
def test_markup_annotation_name_without_reply():
"""Test that annotation_name without in_reply_to raises ValueError."""
with pytest.raises(ValueError, match="annotation_name is only supported when in_reply_to is set"):
Text(
text="Named but not a reply",
rect=(50, 550, 200, 650),
annotation_name="my-unique-name",
)
def test_markup_annotation_reply_type_without_reply():
"""Test that non-default reply_type without in_reply_to raises ValueError."""
with pytest.raises(ValueError, match="reply_type is only meaningful when in_reply_to is set"):
Text(
text="Grouped but not a reply",
rect=(50, 550, 200, 650),
reply_type="Group",
)
def test_markup_annotation_in_reply_to_custom_name():
"""Test explicit annotation_name with in_reply_to."""
writer = PdfWriter()
writer.add_blank_page(width=200, height=200)
parent = Text(text="Parent", rect=(0, 0, 100, 100))
parent_ref = writer.add_annotation(0, parent)
reply = Text(
text="Reply",
rect=(0, 0, 100, 100),
in_reply_to=parent_ref,
annotation_name="custom-reply-name",
)
writer.add_annotation(0, reply)
assert reply["/NM"] == "custom-reply-name"
assert "/IRT" in reply
def test_markup_annotation_in_reply_to_unregistered():
"""Test that an unregistered parent raises ValueError."""
unregistered = Text(text="Not added to writer", rect=(0, 0, 100, 100))
with pytest.raises(ValueError, match="in_reply_to must be a registered annotation"):
Text(
text="Reply",
rect=(0, 0, 100, 100),
in_reply_to=unregistered,
)
def test_markup_annotation_in_reply_to_indirect_object():
"""Test passing an IndirectObject directly as in_reply_to."""
writer = PdfWriter()
writer.add_blank_page(width=200, height=200)
parent = Text(text="Parent", rect=(0, 0, 100, 100))
parent_ref = writer.add_annotation(0, parent)
indirect_ref = parent_ref.indirect_reference
reply = Text(
text="Reply via IndirectObject",
rect=(0, 0, 100, 100),
in_reply_to=indirect_ref,
)
writer.add_annotation(0, reply)
assert "/IRT" in reply
assert reply["/RT"] == "/R"
assert "/NM" in reply
buf = BytesIO()
writer.write(buf)
reader = PdfReader(buf)
annots = reader.pages[0]["/Annots"]
assert len(annots) == 2
reply_obj = annots[1].get_object()
assert reply_obj["/IRT"].get_object()["/Contents"] == "Parent"
assert reply_obj["/NM"] == reply["/NM"]
@pytest.mark.enable_socket
def test_outline_action_without_d_lenient():
reader = PdfReader(BytesIO(get_data_from_url(name="iss3268.pdf")))
assert len(reader.outline) == 2
@pytest.mark.enable_socket
def test_outline_action_without_d_strict(pdf_file_path):
reader = PdfReader(BytesIO(get_data_from_url(name="iss3268.pdf")))
reader.strict = True
with pytest.raises(PdfReadError) as e:
assert len(reader.outline) == 2
assert "Outline Action Missing /D" in str(e)
|