diff --git a/src/core/editor/pdf_editor.js b/src/core/editor/pdf_editor.js index a5d16b310..1b64907ef 100644 --- a/src/core/editor/pdf_editor.js +++ b/src/core/editor/pdf_editor.js @@ -1476,6 +1476,7 @@ class PDFEditor { this.currentDocument = null; } } + this.#setAcroFormCalculationOrder(allDocumentData); } #setAcroFormQ(allDocumentData) { @@ -1511,7 +1512,6 @@ class PDFEditor { #setAcroFormDefaultBasicValues(allDocumentData) { let sigFlags = 0; let needAppearances = false; - const calculationOrder = []; for (const documentData of allDocumentData) { if (!documentData.acroForm) { continue; @@ -1523,7 +1523,15 @@ class PDFEditor { if (documentData.acroForm.get("NeedAppearances") === true) { needAppearances = true; } - const co = documentData.acroForm.get("CO") || null; + } + this.acroFormSigFlags = sigFlags; + this.acroFormNeedAppearances = needAppearances; + } + + #setAcroFormCalculationOrder(allDocumentData) { + const calculationOrder = []; + for (const documentData of allDocumentData) { + const co = documentData.acroForm?.get("CO") || null; if (!Array.isArray(co)) { continue; } @@ -1535,8 +1543,6 @@ class PDFEditor { } } } - this.acroFormSigFlags = sigFlags; - this.acroFormNeedAppearances = needAppearances; this.acroFormCalculationOrder = calculationOrder.length > 0 ? calculationOrder : null; } diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 5f8d1439d..13e26879f 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -887,3 +887,4 @@ !radial_gradients.pdf !outlines_for_editor.pdf !mesh_shading_empty.pdf +!acroform_calculation_order.pdf diff --git a/test/pdfs/acroform_calculation_order.pdf b/test/pdfs/acroform_calculation_order.pdf new file mode 100755 index 000000000..f8de65d63 --- /dev/null +++ b/test/pdfs/acroform_calculation_order.pdf @@ -0,0 +1,48 @@ +%PDF-1.7 +1 0 obj +<< /Type /Catalog /Pages 2 0 R /AcroForm 5 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R] /Count 1 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Resources << /Font << /F1 9 0 R >> >> /Contents 4 0 R /Annots [7 0 R 8 0 R] >> +endobj +4 0 obj +<< /Length 36 >> +stream +BT /F1 12 Tf 10 100 Td (Hello) Tj ET +endstream +endobj +5 0 obj +<< /Fields [6 0 R] /CO [6 0 R] /DA (/F1 12 Tf 0 g) >> +endobj +6 0 obj +<< /FT /Tx /T (group) /Kids [7 0 R 8 0 R] /DA (/F1 12 Tf 0 g) >> +endobj +7 0 obj +<< /Type /Annot /Subtype /Widget /Parent 6 0 R /Rect [10 150 90 170] /P 3 0 R >> +endobj +8 0 obj +<< /Type /Annot /Subtype /Widget /Parent 6 0 R /Rect [10 120 90 140] /P 3 0 R >> +endobj +9 0 obj +<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> +endobj +xref +0 10 +0000000000 65535 f +0000000009 00000 n +0000000074 00000 n +0000000131 00000 n +0000000279 00000 n +0000000365 00000 n +0000000434 00000 n +0000000514 00000 n +0000000610 00000 n +0000000706 00000 n +trailer +<< /Size 10 /Root 1 0 R >> +startxref +776 +%%EOF diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 866558b64..7ce81129f 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -6398,6 +6398,34 @@ small scripts as well as for`); await loadingTask.destroy(); }); + + it("preserves calculation order when it points to parent fields", async function () { + let loadingTask = getDocument( + buildGetDocumentParams("acroform_calculation_order.pdf") + ); + let pdfDoc = await loadingTask.promise; + + expect(await pdfDoc.getCalculationOrderIds()).toEqual(["6R"]); + expect(Object.keys((await pdfDoc.getFieldObjects()) || {})).toEqual([ + "group", + ]); + + const data = await pdfDoc.extractPages([{ document: null }]); + await loadingTask.destroy(); + + loadingTask = getDocument(data); + pdfDoc = await loadingTask.promise; + + const calculationOrder = await pdfDoc.getCalculationOrderIds(); + expect(Array.isArray(calculationOrder)).toEqual(true); + expect(calculationOrder.length).toEqual(1); + expect(calculationOrder[0]).not.toEqual("6R"); + expect(Object.keys((await pdfDoc.getFieldObjects()) || {})).toEqual([ + "group", + ]); + + await loadingTask.destroy(); + }); }); describe("Outlines", function () {