Skip to content

Commit

Permalink
addLink deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Dec 24, 2023
1 parent c64b5bb commit f2fd7b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 92 deletions.
57 changes: 0 additions & 57 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@
StreamType,
_get_max_pdf_version_header,
b_,
deprecate_with_replacement,
deprecation_bookmark,
deprecation_with_replacement,
logger_warning,
)
from .annotations import Link
from .constants import AnnotationDictionaryAttributes as AA
from .constants import CatalogAttributes as CA
from .constants import (
Expand Down Expand Up @@ -119,12 +116,10 @@
from .types import (
AnnotationSubtype,
BorderArrayType,
FitType,
LayoutType,
OutlineItemType,
OutlineType,
PagemodeType,
ZoomArgType,
)

OPTIONAL_READ_WRITE_FIELD = FieldFlag(0)
Expand Down Expand Up @@ -1895,8 +1890,6 @@ def add_uri(
"""
Add an URI from a rectangular area to the specified page.
This uses the basic structure of :meth:`add_link`
Args:
page_number: index of the page on which to place the URI action.
uri: URI of resource to link to.
Expand Down Expand Up @@ -1953,56 +1946,6 @@ def add_uri(
else:
page_ref[NameObject(PG.ANNOTS)] = ArrayObject([lnk_ref])

def add_link(
self,
pagenum: int, # deprecated, but method is deprecated already
page_destination: int,
rect: RectangleObject,
border: Optional[ArrayObject] = None,
fit: FitType = "/Fit",
*args: ZoomArgType,
) -> DictionaryObject:
deprecation_with_replacement(
"add_link", "add_annotation(pypdf.annotations.Link(...))", "3.0.0"
)

if isinstance(rect, str):
rect = rect.strip()[1:-1]
rect = RectangleObject(
[float(num) for num in rect.split(" ") if len(num) > 0]
)
elif isinstance(rect, RectangleObject):
pass
else:
rect = RectangleObject(rect)

annotation = Link(
rect=rect,
border=border,
target_page_index=page_destination,
fit=Fit(fit_type=fit, fit_args=args),
)
return self.add_annotation(page_number=pagenum, annotation=annotation)

def addLink(
self,
pagenum: int, # deprecated, but method is deprecated already
page_destination: int,
rect: RectangleObject,
border: Optional[ArrayObject] = None,
fit: FitType = "/Fit",
*args: ZoomArgType,
) -> None: # deprecated
"""
Use :meth:`add_link` instead.
.. deprecated:: 1.28.0
"""
deprecate_with_replacement(
"addLink", "add_annotation(pypdf.annotations.Link(...))", "4.0.0"
)
self.add_link(pagenum, page_destination, rect, border, fit, *args)

_valid_layouts = (
"/NoLayout",
"/SinglePage",
Expand Down
80 changes: 45 additions & 35 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PdfWriter,
Transformation,
)
from pypdf.annotations import Link
from pypdf.errors import DeprecationError, PageSizeNotDefinedError, PyPdfError
from pypdf.generic import (
ArrayObject,
Expand Down Expand Up @@ -180,8 +181,10 @@ def writer_operate(writer: PdfWriter) -> None:
writer.add_blank_page()
writer.add_uri(2, "https://example.com", RectangleObject([0, 0, 100, 100]))
writer.add_uri(2, "https://example.com", RectangleObject([0, 0, 100, 100]))
with pytest.raises(DeprecationError):
writer.add_link(2, 1, RectangleObject([0, 0, 100, 100]))
writer.add_annotation(
page_number=2,
annotation=Link(target_page_index=1, rect=RectangleObject([0, 0, 100, 100])),
)
assert writer._get_page_layout() is None
writer.page_layout = "broken"
assert writer.page_layout == "broken"
Expand Down Expand Up @@ -718,48 +721,55 @@ def test_add_uri(pdf_file_path):
writer.write(output_stream)


def test_add_link(pdf_file_path):
def test_link_annotation(pdf_file_path):
reader = PdfReader(RESOURCE_ROOT / "pdflatex-outline.pdf")
writer = PdfWriter()

for page in reader.pages:
writer.add_page(page)

with pytest.raises(
DeprecationError,
match=(
re.escape(
"add_link is deprecated and was removed in pypdf 3.0.0. "
"Use add_annotation(pypdf.annotations.Link(...)) instead."
)
),
):
writer.add_link(
1,
2,
RectangleObject([0, 0, 100, 100]),
writer.add_annotation(
page_number=1,
annotation=Link(
target_page_index=2,
rect=RectangleObject(
[0, 0, 100, 100],
),
border=[1, 2, 3, [4]],
fit="/Fit",
)
writer.add_link(
2, 3, RectangleObject([20, 30, 50, 80]), [1, 2, 3], "/FitH", None
)
writer.add_link(
3,
0,
"[ 200 300 250 350 ]",
[0, 0, 0],
"/XYZ",
0,
0,
2,
)
writer.add_link(
3,
0,
[100, 200, 150, 250],
),
)
writer.add_annotation(
page_number=2,
annotation=Link(
target_page_index=3,
rect=RectangleObject(
[0, 0, 100, 100],
),
border=[1, 2, 3],
fit="/FitH",
),
)
writer.add_annotation(
page_number=3,
annotation=Link(
target_page_index=0,
rect=RectangleObject(
"[ 200 300 250 350 ]",
),
border=[0, 0, 0],
)
fit="/XYZ",
fit_args=[0, 0, 2],
),
)
writer.add_annotation(
page_number=3,
annotation=Link(
target_page_index=0,
rect=RectangleObject([100, 200, 150, 250]),
border=[0, 0, 0],
),
)

# write "output" to pypdf-output.pdf
with open(pdf_file_path, "wb") as output_stream:
Expand Down

0 comments on commit f2fd7b8

Please sign in to comment.