PyPDFForm 3.1.0__py3-none-any.whl → 3.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of PyPDFForm might be problematic. Click here for more details.

PyPDFForm/__init__.py CHANGED
@@ -20,7 +20,7 @@ The library supports various PDF form features, including:
20
20
  PyPDFForm aims to simplify PDF form manipulation, making it accessible to developers of all skill levels.
21
21
  """
22
22
 
23
- __version__ = "3.1.0"
23
+ __version__ = "3.1.1"
24
24
 
25
25
  from .middleware.text import Text # exposing for setting global font attrs
26
26
  from .wrapper import PdfWrapper
PyPDFForm/constants.py CHANGED
@@ -67,6 +67,7 @@ Opt = "/Opt"
67
67
  AS = "/AS"
68
68
  Yes = "/Yes"
69
69
  Off = "/Off"
70
+ XObject = "/XObject"
70
71
 
71
72
  # Font dict
72
73
  Length1 = "/Length1"
@@ -105,3 +106,5 @@ IMAGE_FIELD_IDENTIFIER = "event.target.buttonImportIcon();"
105
106
 
106
107
  COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO = DEFAULT_FONT_SIZE / 100
107
108
  UNIQUE_SUFFIX_LENGTH = 20
109
+
110
+ SLASH = "/"
PyPDFForm/patterns.py CHANGED
@@ -8,11 +8,14 @@ properties in the PDF's annotation dictionary. It also provides utility function
8
8
  for updating these widgets.
9
9
  """
10
10
 
11
+ from typing import Union
12
+
11
13
  from pypdf.generic import (ArrayObject, DictionaryObject, NameObject,
12
14
  NumberObject, TextStringObject)
13
15
 
14
- from .constants import (AP, AS, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, TU, A, Btn,
15
- Ch, I, N, Off, Opt, Parent, Sig, T, Tx, V, Yes)
16
+ from .constants import (AP, AS, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, SLASH, TU,
17
+ A, Btn, Ch, I, N, Off, Opt, Parent, Resources, Sig, T,
18
+ Tx, V, XObject, Yes)
16
19
  from .middleware.checkbox import Checkbox
17
20
  from .middleware.dropdown import Dropdown
18
21
  from .middleware.image import Image
@@ -21,6 +24,14 @@ from .middleware.signature import Signature
21
24
  from .middleware.text import Text
22
25
 
23
26
  WIDGET_TYPE_PATTERNS = [
27
+ # barcode? see pdf_samples/scenario/issues/1087.pdf
28
+ (
29
+ (
30
+ {FT: Tx},
31
+ {AP: {N: {Resources: {XObject: True}}}},
32
+ ),
33
+ None,
34
+ ),
24
35
  (
25
36
  ({A: {JS: IMAGE_FIELD_IDENTIFIER}},),
26
37
  Image,
@@ -42,7 +53,7 @@ WIDGET_TYPE_PATTERNS = [
42
53
  (
43
54
  {FT: Btn},
44
55
  {Parent: {FT: Btn}},
45
- {AS: (Yes, Off)},
56
+ {AS: (Yes, Off, SLASH)},
46
57
  ),
47
58
  Radio,
48
59
  ),
@@ -76,7 +87,7 @@ WIDGET_TYPE_PATTERNS = [
76
87
  (
77
88
  (
78
89
  {Parent: {FT: Btn}},
79
- {AS: (Yes, Off)},
90
+ {AS: (Yes, Off, SLASH)},
80
91
  ),
81
92
  Radio,
82
93
  ),
@@ -113,6 +124,23 @@ def update_checkbox_value(annot: DictionaryObject, check: bool = False) -> None:
113
124
  break
114
125
 
115
126
 
127
+ def get_checkbox_value(annot: DictionaryObject) -> Union[bool, None]:
128
+ """
129
+ Retrieves the boolean value of a checkbox annotation.
130
+
131
+ This function checks the value (V) of the checkbox annotation. If the value
132
+ is not 'Off', it means the checkbox is checked, and True is returned.
133
+ Otherwise, if the value is 'Off' or not present, None is returned.
134
+
135
+ Args:
136
+ annot (DictionaryObject): The checkbox annotation dictionary.
137
+
138
+ Returns:
139
+ Union[bool, None]: True if the checkbox is checked, None otherwise.
140
+ """
141
+ return True if annot.get(V, Off) != Off else None
142
+
143
+
116
144
  def update_radio_value(annot: DictionaryObject) -> None:
117
145
  """
118
146
  Updates the value of a radio button annotation, selecting it.
@@ -133,6 +161,28 @@ def update_radio_value(annot: DictionaryObject) -> None:
133
161
  break
134
162
 
135
163
 
164
+ def get_radio_value(annot: DictionaryObject) -> bool:
165
+ """
166
+ Retrieves the boolean value of a radio button annotation.
167
+
168
+ This function iterates through the appearance states (AP) of the radio button
169
+ annotation. If the value (V) of the parent dictionary matches any of these
170
+ appearance states, it means the radio button is selected, and True is returned.
171
+ Otherwise, False is returned.
172
+
173
+ Args:
174
+ annot (DictionaryObject): The radio button annotation dictionary.
175
+
176
+ Returns:
177
+ bool: True if the radio button is selected, False otherwise.
178
+ """
179
+ for each in annot.get(AP, {}).get(N, []):
180
+ if annot.get(Parent, {}).get(V) == each:
181
+ return True
182
+
183
+ return False
184
+
185
+
136
186
  def update_dropdown_value(annot: DictionaryObject, widget: Dropdown) -> None:
137
187
  """
138
188
  Updates the value of a dropdown annotation, selecting an option from the list.
@@ -157,6 +207,29 @@ def update_dropdown_value(annot: DictionaryObject, widget: Dropdown) -> None:
157
207
  annot[NameObject(I)] = ArrayObject([NumberObject(widget.value)])
158
208
 
159
209
 
210
+ def get_dropdown_value(annot: DictionaryObject, widget: Dropdown) -> None:
211
+ """
212
+ Retrieves the selected value of a dropdown annotation and updates the widget.
213
+
214
+ This function determines the current value of the dropdown, considering
215
+ whether it's a child annotation or a top-level one. It then iterates
216
+ through the widget's choices to find a match and sets the widget's
217
+ value to the index of the matched choice.
218
+
219
+ Args:
220
+ annot (DictionaryObject): The dropdown annotation dictionary.
221
+ widget (Dropdown): The Dropdown widget object to update with the retrieved value.
222
+ """
223
+ if Parent in annot and T not in annot:
224
+ to_compare = annot.get(Parent, {}).get(V)
225
+ else:
226
+ to_compare = annot.get(V)
227
+
228
+ for i, each in enumerate(widget.choices):
229
+ if each == to_compare:
230
+ widget.value = i or None # set None when 0
231
+
232
+
160
233
  def update_text_value(annot: DictionaryObject, widget: Text) -> None:
161
234
  """
162
235
  Updates the value of a text annotation, setting the text content.
@@ -176,6 +249,24 @@ def update_text_value(annot: DictionaryObject, widget: Text) -> None:
176
249
  annot[NameObject(AP)] = TextStringObject(widget.value)
177
250
 
178
251
 
252
+ def get_text_value(annot: DictionaryObject, widget: Text) -> None:
253
+ """
254
+ Retrieves the text value of a text annotation and updates the widget.
255
+
256
+ This function determines the current text value of the annotation, considering
257
+ whether it's a child annotation or a top-level one, and then sets the
258
+ widget's value accordingly.
259
+
260
+ Args:
261
+ annot (DictionaryObject): The text annotation dictionary.
262
+ widget (Text): The Text widget object to update with the retrieved value.
263
+ """
264
+ if Parent in annot and T not in annot:
265
+ widget.value = annot[Parent].get(V)
266
+ else:
267
+ widget.value = annot.get(V)
268
+
269
+
179
270
  def update_annotation_name(annot: DictionaryObject, val: str) -> None:
180
271
  """
181
272
  Updates the name of an annotation, setting the T (title) entry.
PyPDFForm/template.py CHANGED
@@ -16,12 +16,14 @@ from pypdf import PdfReader, PdfWriter
16
16
  from pypdf.generic import DictionaryObject
17
17
 
18
18
  from .constants import WIDGET_TYPES, Annots, MaxLen, Parent, T
19
+ from .middleware.checkbox import Checkbox
19
20
  from .middleware.dropdown import Dropdown
20
21
  from .middleware.radio import Radio
21
22
  from .middleware.text import Text
22
23
  from .patterns import (DROPDOWN_CHOICE_PATTERNS, WIDGET_DESCRIPTION_PATTERNS,
23
24
  WIDGET_KEY_PATTERNS, WIDGET_TYPE_PATTERNS,
24
- update_annotation_name)
25
+ get_checkbox_value, get_dropdown_value, get_radio_value,
26
+ get_text_value, update_annotation_name)
25
27
  from .utils import extract_widget_property, find_pattern_match, stream_to_io
26
28
 
27
29
 
@@ -61,11 +63,16 @@ def build_widgets(
61
63
  if isinstance(_widget, Text):
62
64
  # mostly for schema for now
63
65
  _widget.max_length = get_text_field_max_length(widget)
66
+ get_text_value(widget, _widget)
67
+
68
+ if type(_widget) is Checkbox:
69
+ _widget.value = get_checkbox_value(widget)
64
70
 
65
71
  if isinstance(_widget, Dropdown):
66
72
  # actually used for filling value
67
73
  # doesn't trigger hook
68
74
  _widget.__dict__["choices"] = get_dropdown_choices(widget)
75
+ get_dropdown_value(widget, _widget)
69
76
 
70
77
  if isinstance(_widget, Radio):
71
78
  if key not in results:
@@ -73,6 +80,9 @@ def build_widgets(
73
80
 
74
81
  # for schema
75
82
  results[key].number_of_options += 1
83
+
84
+ if get_radio_value(widget):
85
+ results[key].value = results[key].number_of_options - 1
76
86
  continue
77
87
 
78
88
  results[key] = _widget
@@ -172,7 +182,7 @@ def construct_widget(widget: dict, key: str) -> Union[WIDGET_TYPES, None]:
172
182
  for pattern in patterns:
173
183
  check = check and find_pattern_match(pattern, widget)
174
184
  if check:
175
- result = _type(key)
185
+ result = _type(key) if _type else None
176
186
  break
177
187
  return result
178
188
 
PyPDFForm/utils.py CHANGED
@@ -23,7 +23,7 @@ from typing import Any, BinaryIO, List, Union
23
23
  from pypdf import PdfReader, PdfWriter
24
24
  from pypdf.generic import ArrayObject, DictionaryObject, NameObject
25
25
 
26
- from .constants import UNIQUE_SUFFIX_LENGTH, XFA, AcroForm, Annots, Root
26
+ from .constants import SLASH, UNIQUE_SUFFIX_LENGTH, XFA, AcroForm, Annots, Root
27
27
 
28
28
 
29
29
  @lru_cache
@@ -220,6 +220,10 @@ def find_pattern_match(pattern: dict, widget: Union[dict, DictionaryObject]) ->
220
220
  else:
221
221
  if isinstance(pattern[key], tuple):
222
222
  result = value in pattern[key]
223
+ if not result and SLASH in pattern[key] and value.startswith(SLASH):
224
+ result = True
225
+ elif pattern[key] is True:
226
+ result = True
223
227
  else:
224
228
  result = pattern[key] == value
225
229
  if result:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyPDFForm
3
- Version: 3.1.0
3
+ Version: 3.1.1
4
4
  Summary: The Python library for PDF forms.
5
5
  Author: Jinge Li
6
6
  License-Expression: MIT
@@ -1,14 +1,14 @@
1
- PyPDFForm/__init__.py,sha256=R4BorFo4mdXpjdpfUiO6kvJpowukDOHSiRpfHBV2jYs,925
1
+ PyPDFForm/__init__.py,sha256=2jZGE5eQAyNUA-c-ZZWdKF5RufHjKaqaqyFfH2XhNCM,925
2
2
  PyPDFForm/adapter.py,sha256=8E_PZlXU1ALWez_pWF_U52cynzowK_NQFYzMJoH9VUk,2428
3
- PyPDFForm/constants.py,sha256=WQjBvYTryj5gxxeXECPgHplXXXmqHNeVbgmhvlLDCpA,2443
3
+ PyPDFForm/constants.py,sha256=6XTmMC-sr6c6K5hG7XZ7qhC44m8RrXvUpakA26MvW9Q,2477
4
4
  PyPDFForm/coordinate.py,sha256=VMVkqa-VAGJSGVvetZwOxeMzIgQtQdvtn_DI_qSecCE,3876
5
5
  PyPDFForm/filler.py,sha256=KwStL6YzrNBcDd919ig83MnAxopi8Vnz3QNJzN_CjNM,7272
6
6
  PyPDFForm/font.py,sha256=EZSAHnkzaBo96p9XJrQDMcEhjh5BZ8p7WwFC3SbKRMM,8828
7
7
  PyPDFForm/hooks.py,sha256=A8p67ubWvCvzRt346Q7BjOvbi4_NXBcynXmq6fJTadY,11679
8
8
  PyPDFForm/image.py,sha256=CAC69jEfSbWbyNJcjLhjWVSNJuFh7frMI70eaiFayHw,3823
9
- PyPDFForm/patterns.py,sha256=xuKAqGf07f3WFXwASnrmcS073pSEcR9lQMqLppjkQ2E,5829
10
- PyPDFForm/template.py,sha256=aGhwezcynmwCEck1SUNlQvgfupn-plV9mV2FF2RNn_M,9188
11
- PyPDFForm/utils.py,sha256=RX2mOP2uCIcN_JcTalCdYKhnYtyYc8dUIqg1wP_7MJA,10867
9
+ PyPDFForm/patterns.py,sha256=8wlOma6s9Be0QxE9Fx2nGGWtPs5RlQ2ARIMCD9qIq-4,8940
10
+ PyPDFForm/template.py,sha256=OyMBAZSAuGM7_YZ4tey7YP0UtzCNM3cUghqpudKMfE0,9692
11
+ PyPDFForm/utils.py,sha256=qKBOB-qsQBhqXP1XoqCukpvtwwMaBVX4uljWPV5i6iU,11078
12
12
  PyPDFForm/watermark.py,sha256=9p1tjaIqicXngTNai_iOEkCoXRYnR66azB4s7wNsZUw,9349
13
13
  PyPDFForm/wrapper.py,sha256=Ysd1mkvE5OfDkjUI6mNFIt16-JUKwj2ifbp1MioWPUo,25257
14
14
  PyPDFForm/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -28,8 +28,8 @@ PyPDFForm/widgets/image.py,sha256=aSD-3MEZFIRL7HYVuO6Os8irfSUOLHA_rHGkqcEIPPA,85
28
28
  PyPDFForm/widgets/radio.py,sha256=nWSQQp06kRISO7Q7FVFeB3PXYvMOSc0SMhRs1bHTxeQ,2261
29
29
  PyPDFForm/widgets/signature.py,sha256=EqIRIuKSQEg8LJZ_Mu859eEvs0dwO-mzkMNuhHG1Vsg,4034
30
30
  PyPDFForm/widgets/text.py,sha256=gtheE6_w0vQPRJJ9oj_l9FaMDEGnPtvVR6_axsrmxKI,1540
31
- pypdfform-3.1.0.dist-info/licenses/LICENSE,sha256=43awmYkI6opyTpg19me731iO1WfXZwViqb67oWtCsFY,1065
32
- pypdfform-3.1.0.dist-info/METADATA,sha256=-vHzFtOdPxAwCjPm98xvD424mC3Yc0KttyMir2HvPjE,4587
33
- pypdfform-3.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
34
- pypdfform-3.1.0.dist-info/top_level.txt,sha256=GQQKuWqPUjT9YZqwK95NlAQzxjwoQrsxQ8ureM8lWOY,10
35
- pypdfform-3.1.0.dist-info/RECORD,,
31
+ pypdfform-3.1.1.dist-info/licenses/LICENSE,sha256=43awmYkI6opyTpg19me731iO1WfXZwViqb67oWtCsFY,1065
32
+ pypdfform-3.1.1.dist-info/METADATA,sha256=AV8baYVcYIjI7kUlWQJQ0B0PTn6CBLEcV7WPpTZOVPU,4587
33
+ pypdfform-3.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
34
+ pypdfform-3.1.1.dist-info/top_level.txt,sha256=GQQKuWqPUjT9YZqwK95NlAQzxjwoQrsxQ8ureM8lWOY,10
35
+ pypdfform-3.1.1.dist-info/RECORD,,