Confirm correct working of interfield calculations. We are going to create three pages with a computed result field each. Tests the fix for https://github.com/pymupdf/PyMuPDF/issues/3402.
()
| 254 | |
| 255 | |
| 256 | def test_interfield_calculation(): |
| 257 | """Confirm correct working of interfield calculations. |
| 258 | |
| 259 | We are going to create three pages with a computed result field each. |
| 260 | |
| 261 | Tests the fix for https://github.com/pymupdf/PyMuPDF/issues/3402. |
| 262 | """ |
| 263 | # Field bboxes (same on each page) |
| 264 | r1 = pymupdf.Rect(100, 100, 300, 120) |
| 265 | r2 = pymupdf.Rect(100, 130, 300, 150) |
| 266 | r3 = pymupdf.Rect(100, 180, 300, 200) |
| 267 | |
| 268 | doc = pymupdf.open() |
| 269 | pdf = pymupdf._as_pdf_document(doc) # we need underlying PDF document |
| 270 | |
| 271 | # Make PDF name object for "CO" because it is not defined in MuPDF. |
| 272 | CO_name = pymupdf.mupdf.pdf_new_name("CO") # = PDF_NAME(CO) |
| 273 | for i in range(3): |
| 274 | page = doc.new_page() |
| 275 | w = pymupdf.Widget() |
| 276 | w.field_name = f"NUM1{page.number}" |
| 277 | w.rect = r1 |
| 278 | w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT |
| 279 | w.field_value = f"{i*100+1}" |
| 280 | w.field_flags = 2 |
| 281 | page.add_widget(w) |
| 282 | |
| 283 | w = pymupdf.Widget() |
| 284 | w.field_name = f"NUM2{page.number}" |
| 285 | w.rect = r2 |
| 286 | w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT |
| 287 | w.field_value = "200" |
| 288 | w.field_flags = 2 |
| 289 | page.add_widget(w) |
| 290 | |
| 291 | w = pymupdf.Widget() |
| 292 | w.field_name = f"RESULT{page.number}" |
| 293 | w.rect = r3 |
| 294 | w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT |
| 295 | w.field_value = "Result?" |
| 296 | # Script that adds previous two fields. |
| 297 | w.script_calc = f"""AFSimple_Calculate("SUM", |
| 298 | new Array("NUM1{page.number}", "NUM2{page.number}"));""" |
| 299 | page.add_widget(w) |
| 300 | |
| 301 | # Access the inter-field calculation array. It contains a reference to |
| 302 | # all fields which have a JavaScript stored in their "script_calc" |
| 303 | # property, i.e. an "AA/C" entry. |
| 304 | # Every iteration adds another such field, so this array's length must |
| 305 | # always equal the loop index. |
| 306 | if i == 0: # only need to execute this on first time through |
| 307 | CO = pymupdf.mupdf.pdf_dict_getl( |
| 308 | pymupdf.mupdf.pdf_trailer(pdf), |
| 309 | pymupdf.PDF_NAME("Root"), |
| 310 | pymupdf.PDF_NAME("AcroForm"), |
| 311 | CO_name, |
| 312 | ) |
| 313 | # we confirm CO is an array of foreseeable length |
nothing calls this directly
no test coverage detected
searching dependent graphs…