Skip to content

Commit 574c31c

Browse files
Merge pull request #297 from chinapandaman/PPF-296
PPF-296: fix x coordinate math for checkbox and radio button
2 parents 3b56663 + 3d55b4e commit 574c31c

24 files changed

+27
-39
lines changed

PyPDFForm/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
PyPDFForm = Wrapper
88
PyPDFForm2 = WrapperV2
99

10-
__version__ = "1.1.2"
10+
__version__ = "1.1.3"

PyPDFForm/core/filler.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,17 @@ def fill_v2(
233233
if elements[key].type == ElementType.checkbox:
234234
if elements[key].value:
235235
font_size = Utils().checkbox_radio_font_size(_element)
236+
_to_draw = Utils().checkbox_radio_to_draw(
237+
elements[key], font_size
238+
)
239+
x, y = TemplateCore().get_draw_checkbox_radio_coordinates_v2(
240+
_element, _to_draw
241+
)
236242
texts_to_draw[page].append(
237243
[
238-
Utils().checkbox_radio_to_draw(
239-
elements[key], font_size
240-
),
241-
TemplateCore().get_draw_checkbox_radio_coordinates_v2(
242-
_element, font_size
243-
)[0],
244-
TemplateCore().get_draw_checkbox_radio_coordinates_v2(
245-
_element, font_size
246-
)[1],
244+
_to_draw,
245+
x,
246+
y,
247247
]
248248
)
249249
elif elements[key].type == ElementType.radio:
@@ -253,17 +253,17 @@ def fill_v2(
253253

254254
if elements[key].value == radio_button_tracker[key] - 1:
255255
font_size = Utils().checkbox_radio_font_size(_element)
256+
_to_draw = Utils().checkbox_radio_to_draw(
257+
elements[key], font_size
258+
)
259+
x, y = TemplateCore().get_draw_checkbox_radio_coordinates_v2(
260+
_element, _to_draw
261+
)
256262
texts_to_draw[page].append(
257263
[
258-
Utils().checkbox_radio_to_draw(
259-
elements[key], font_size
260-
),
261-
TemplateCore().get_draw_checkbox_radio_coordinates_v2(
262-
_element, font_size
263-
)[0],
264-
TemplateCore().get_draw_checkbox_radio_coordinates_v2(
265-
_element, font_size
266-
)[1],
264+
_to_draw,
265+
x,
266+
y,
267267
]
268268
)
269269
else:

PyPDFForm/core/template.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,25 @@ def get_draw_checkbox_radio_coordinates(
275275
@staticmethod
276276
def get_draw_checkbox_radio_coordinates_v2(
277277
element: "pdfrw.PdfDict",
278-
font_size: Union[float, int],
278+
element_middleware: "ElementMiddleware",
279279
) -> Tuple[Union[float, int], Union[float, int]]:
280280
"""Returns coordinates to draw at given a PDF form checkbox/radio element."""
281281

282-
side = font_size * 96 / 72
283-
c_half_width = (
282+
string_height = element_middleware.font_size * 96 / 72
283+
width_mid_point = (
284284
float(element[TemplateCoreConstants().annotation_rectangle_key][0])
285285
+ float(element[TemplateCoreConstants().annotation_rectangle_key][2])
286286
) / 2
287-
c_half_height = (
287+
height_mid_point = (
288288
float(element[TemplateCoreConstants().annotation_rectangle_key][1])
289289
+ float(element[TemplateCoreConstants().annotation_rectangle_key][3])
290290
) / 2
291291

292292
return (
293-
(c_half_width - side / 2 + c_half_width) / 2,
294-
(c_half_height - side / 2 + c_half_height) / 2,
293+
width_mid_point - stringWidth(
294+
element_middleware.value, element_middleware.font, element_middleware.font_size
295+
) / 2,
296+
(height_mid_point - string_height / 2 + height_mid_point) / 2,
295297
)
296298

297299
@staticmethod
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-1 Bytes
Binary file not shown.
Binary file not shown.
3 Bytes
Binary file not shown.
9 Bytes
Binary file not shown.

pdf_samples/v2/sample_filled.pdf

1 Byte
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
3 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

tests/unit/test_template.py

-14
Original file line numberDiff line numberDiff line change
@@ -692,20 +692,6 @@ def test_find_pattern_match_sejda(sejda_template, sejda_data):
692692
assert check
693693

694694

695-
def test_get_draw_checkbox_radio_coordinates_v2():
696-
element = pdfrw.PdfDict(
697-
**{
698-
TemplateCoreConstants().annotation_rectangle_key.replace("/", ""): [
699-
0,
700-
0,
701-
16,
702-
16,
703-
]
704-
}
705-
)
706-
assert TemplateCore().get_draw_checkbox_radio_coordinates_v2(element, 12) == (4, 4)
707-
708-
709695
def test_get_text_field_max_length(sample_template_with_max_length_text_field):
710696
for _page, elements in (
711697
TemplateCore()

0 commit comments

Comments
 (0)