Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added basic support for Free Text annotations #1039

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fpdf/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
file_spec: str = None,
field_type: str = None,
value=None,
default_appearance: str = None, # for free text annotations
):
self.type = Name("Annot")
self.subtype = Name(subtype)
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(
else None
)
self.f_s = file_spec
self.d_a = "(0 1 0 rg /Helv 12 Tf)" if default_appearance else None


class PDFAnnotation(AnnotationMixin, PDFObject):
Expand Down Expand Up @@ -100,6 +102,7 @@ class AnnotationDict(AnnotationMixin):
"name",
"ink_list",
"f_s",
"d_a",
)

def serialize(self, _security_handler=None, _obj_id=None):
Expand Down
38 changes: 38 additions & 0 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,44 @@ def text_annotation(
self.pages[self.page].annots.append(annotation)
return annotation

@check_page
def free_text_annotation(
self,
x,
y,
text,
w=1,
h=1,
name=None,
flags=DEFAULT_ANNOT_FLAGS,
default_appearance=False,
):
"""
Puts a free text annotation on a rectangular area of the page.

Args:
x (float): horizontal position (from the left) to the left side of the link rectangle
y (float): vertical position (from the top) to the bottom side of the link rectangle
text (str): text to display
w (float): optional width of the link rectangle
h (float): optional height of the link rectangle
name (fpdf.enums.AnnotationName, str): optional icon that shall be used in displaying the annotation
flags (Tuple[fpdf.enums.AnnotationFlag], Tuple[str]): optional list of flags defining annotation properties
"""
annotation = AnnotationDict(
"FreeText",
x * self.k,
self.h_pt - y * self.k,
w * self.k,
h * self.k,
contents=text,
name=AnnotationName.coerce(name) if name else None,
flags=tuple(AnnotationFlag.coerce(flag) for flag in flags),
default_appearance=default_appearance,
)
self.pages[self.page].annots.append(annotation)
return annotation

@check_page
def add_action(self, action, x, y, w, h):
"""
Expand Down