diff --git a/PyPDFForm/patterns.py b/PyPDFForm/patterns.py index 0e1be6be..87b8b40f 100644 --- a/PyPDFForm/patterns.py +++ b/PyPDFForm/patterns.py @@ -4,7 +4,7 @@ from pypdf.generic import (DictionaryObject, NameObject, NumberObject, TextStringObject) -from .constants import (AP, AS, CA, DA, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, MK, +from .constants import (AP, AS, CA, DA, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, MK, MULTILINE, READ_ONLY, A, Btn, Ch, Ff, N, Off, Opt, Parent, Q, Sig, T, Tx, V, Yes) from .middleware.checkbox import Checkbox @@ -166,4 +166,16 @@ def update_created_text_field_alignment(annot: DictionaryObject, val: int) -> No annot[NameObject(Q)] = NumberObject(val) -NON_ACRO_FORM_PARAM_TO_FUNC = {"alignment": update_created_text_field_alignment} +def update_created_text_field_multiline(annot: DictionaryObject, val: bool) -> None: + """Patterns to update to multiline for text annotations created by the library.""" + + if val: + annot[NameObject(Ff)] = NumberObject( + int(annot[NameObject(Ff)]) | MULTILINE # noqa + ) + + +NON_ACRO_FORM_PARAM_TO_FUNC = { + ("TextWidget", "alignment"): update_created_text_field_alignment, + ("TextWidget", "multiline"): update_created_text_field_multiline, +} diff --git a/PyPDFForm/widgets/base.py b/PyPDFForm/widgets/base.py index ba1f2c81..a641b6e6 100644 --- a/PyPDFForm/widgets/base.py +++ b/PyPDFForm/widgets/base.py @@ -59,7 +59,7 @@ def __init__( for each in self.ALLOWED_NON_ACRO_FORM_PARAMS: if each in kwargs: - self.non_acro_form_params.append((each, kwargs.get(each))) + self.non_acro_form_params.append(((type(self).__name__, each), kwargs.get(each))) def watermarks(self, stream: bytes) -> List[bytes]: """Returns a list of watermarks after creating the widget.""" diff --git a/PyPDFForm/widgets/text.py b/PyPDFForm/widgets/text.py index a4d74a81..d18b7cf9 100644 --- a/PyPDFForm/widgets/text.py +++ b/PyPDFForm/widgets/text.py @@ -19,6 +19,6 @@ class TextWidget(Widget): ("max_length", "maxlen"), ] COLOR_PARAMS = ["font_color", "bg_color", "border_color"] - ALLOWED_NON_ACRO_FORM_PARAMS = ["alignment"] + ALLOWED_NON_ACRO_FORM_PARAMS = ["alignment", "multiline"] NONE_DEFAULTS = ["max_length"] ACRO_FORM_FUNC = "textfield" diff --git a/docs/prepare.md b/docs/prepare.md index 0ecd1a7d..6df5b94f 100644 --- a/docs/prepare.md +++ b/docs/prepare.md @@ -41,7 +41,8 @@ new_form = PdfWrapper("dummy.pdf").create_widget( bg_color=(0, 0, 1), # optional border_color=(1, 0, 0), # optional border_width=5, # optional - alignment=0 # optional, 0=left, 1=center, 2=right + alignment=0, # optional, 0=left, 1=center, 2=right + multiline=True # optional ) with open("output.pdf", "wb+") as output: diff --git a/pdf_samples/widget/create_text_align_multiline.pdf b/pdf_samples/widget/create_text_align_multiline.pdf new file mode 100644 index 00000000..b7f4e97b Binary files /dev/null and b/pdf_samples/widget/create_text_align_multiline.pdf differ diff --git a/tests/test_create_widget.py b/tests/test_create_widget.py index 16d6f9c1..cfa01fbe 100644 --- a/tests/test_create_widget.py +++ b/tests/test_create_widget.py @@ -242,6 +242,28 @@ def test_create_text_align_right(template_stream, pdf_samples, request): assert obj.stream == expected +def test_create_text_align_multiline(template_stream, pdf_samples, request): + expected_path = os.path.join(pdf_samples, "widget", "create_text_align_multiline.pdf") + with open(expected_path, "rb+") as f: + obj = PdfWrapper(template_stream).create_widget( + "text", + "foo", + 1, + 100, + 100, + multiline=True, + ) + assert obj.schema["properties"]["foo"]["type"] == "string" + + request.config.results["expected_path"] = expected_path + request.config.results["stream"] = obj.read() + + expected = f.read() + + assert len(obj.stream) == len(expected) + assert obj.stream == expected + + def test_create_text_default_filled(template_stream, pdf_samples, request): expected_path = os.path.join( pdf_samples, "widget", "create_text_default_filled.pdf"