-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAINT: Cleanup of annotations (#1745)
The goal of this PR is to create a more intuitive interface for creating annotations. The AnnotationBuild gets deprecated in favor of several annotation classes, e.g. ```python # old from pypdf.generic import AnnotationBuilder annotation = AnnotationBuilder.free_text( "Hello World\nThis is the second line!", rect=(50, 550, 200, 650), font="Arial", bold=True, italic=True, font_size="20pt", font_color="00ff00", border_color="0000ff", background_color="cdcdcd", ) # new from pypdf.annotations import FreeText annotation = FreeText( text="Hello World\nThis is the second line!", rect=(50, 550, 200, 650), font="Arial", bold=True, italic=True, font_size="20pt", font_color="00ff00", border_color="0000ff", background_color="cdcdcd", ) ``` * `pypdf/generic/_annotations.py` ➔ `pypdf/annotations/` * Create abstract base class AnnotationDictionary * Create abstract base class MarkupAnnotation which inherits from AnnotationDictionary. Most annotations are MarkupAnnotations. * Deprecated AnnotationBuilder * Ensure the AnnotationBuilder is not used in the docs Closes #107
- Loading branch information
1 parent
8abd34a
commit abd2673
Showing
15 changed files
with
1,036 additions
and
618 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
The annotations module | ||
---------------------- | ||
|
||
.. automodule:: pypdf.annotations | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
PDF specifies several annotation types which pypdf makes available here. | ||
The names of the annotations and their attributes do not reflect the names in | ||
the specification in all cases. For example, the PDF standard defines a | ||
'Square' annotation that does not actually need to be square. For this reason, | ||
pypdf calls it 'Rectangle'. | ||
At their core, all annotation types are DictionaryObjects. That means if pypdf | ||
does not implement a feature, users can easily extend the given functionality. | ||
""" | ||
|
||
|
||
from ._base import NO_FLAGS, AnnotationDictionary | ||
from ._markup_annotations import ( | ||
Ellipse, | ||
FreeText, | ||
Highlight, | ||
Line, | ||
Link, | ||
MarkupAnnotation, | ||
Polygon, | ||
PolyLine, | ||
Rectangle, | ||
Text, | ||
) | ||
from ._non_markup_annotations import Popup | ||
|
||
__all__ = [ | ||
"NO_FLAGS", | ||
# Export abstract base classes so that they are shown in the docs | ||
"AnnotationDictionary", | ||
"MarkupAnnotation", | ||
# markup annotations | ||
"Ellipse", | ||
"FreeText", | ||
"Highlight", | ||
"Line", | ||
"Link", | ||
"Polygon", | ||
"PolyLine", | ||
"Rectangle", | ||
"Text", | ||
# Non-markup annotations | ||
"Popup", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from abc import ABC | ||
|
||
from ..constants import AnnotationFlag | ||
from ..generic import NameObject, NumberObject | ||
from ..generic._data_structures import DictionaryObject | ||
|
||
|
||
class AnnotationDictionary(DictionaryObject, ABC): | ||
def __init__(self) -> None: | ||
from ..generic._base import NameObject | ||
|
||
# "rect" should not be added here as PolyLine can automatically set it | ||
self[NameObject("/Type")] = NameObject("/Annot") | ||
# The flags was NOT added to the constructor on purpose: We expect that | ||
# most users don't want to change the default. If they want, they | ||
# can use the property. The default is 0. | ||
|
||
@property | ||
def flags(self) -> AnnotationFlag: | ||
return self.get(NameObject("/F"), AnnotationFlag(0)) | ||
|
||
@flags.setter | ||
def flags(self, value: AnnotationFlag) -> None: | ||
self[NameObject("/F")] = NumberObject(value) | ||
|
||
|
||
NO_FLAGS = AnnotationFlag(0) |
Oops, something went wrong.