diff --git a/src/core/document.js b/src/core/document.js index ca5030402f086..105ea1a71214f 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -37,6 +37,7 @@ import { Dict, isDict, isName, + isRef, isStream, Ref, } from "./primitives.js"; @@ -1012,6 +1013,21 @@ class PDFDocument { Promise.all(allPromises).then(() => allFields) ); } + + get calculationOrderIds() { + const acroForm = this.catalog.acroForm; + if (!acroForm || !acroForm.has("CO")) { + return shadow(this, "calculationOrderIds", null); + } + + const calculationOrder = acroForm.get("CO"); + if (!Array.isArray(calculationOrder) || calculationOrder.length === 0) { + return shadow(this, "calculationOrderIds", null); + } + + const ids = calculationOrder.filter(isRef).map(ref => ref.toString()); + return shadow(this, "calculationOrderIds", ids); + } } export { Page, PDFDocument }; diff --git a/src/core/worker.js b/src/core/worker.js index 407afb6d23f21..eae578ad604d1 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -520,6 +520,10 @@ class WorkerMessageHandler { return pdfManager.ensureDoc("fieldObjects"); }); + handler.on("GetCalculationOrderIds", function (data) { + return pdfManager.ensureDoc("calculationOrderIds"); + }); + handler.on("SaveDocument", function ({ numPages, annotationStorage, diff --git a/src/display/api.js b/src/display/api.js index d8ab4ec649b89..c6e7befb81780 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -885,6 +885,15 @@ class PDFDocumentProxy { getFieldObjects() { return this._transport.getFieldObjects(); } + + /** + * @returns {Promise | null>} A promise that is resolved with an + * {Array} containing IDs of annotations that have a calculation + * action, or `null` when no such annotations are present in the PDF file. + */ + getCalculationOrderIds() { + return this._transport.getCalculationOrderIds(); + } } /** @@ -2562,6 +2571,10 @@ class WorkerTransport { return this.messageHandler.sendWithPromise("GetFieldObjects", null); } + getCalculationOrderIds() { + return this.messageHandler.sendWithPromise("GetCalculationOrderIds", null); + } + getDestinations() { return this.messageHandler.sendWithPromise("GetDestinations", null); } diff --git a/test/unit/document_spec.js b/test/unit/document_spec.js index be58d85c9294b..fb18029a0fa14 100644 --- a/test/unit/document_spec.js +++ b/test/unit/document_spec.js @@ -159,5 +159,20 @@ describe("document", function () { hasFields: true, }); }); + + it("should get calculation order array or null", function () { + const acroForm = new Dict(); + + let pdfDocument = getDocument(acroForm); + expect(pdfDocument.calculationOrderIds).toEqual(null); + + acroForm.set("CO", [Ref.get(1, 0), Ref.get(2, 0), Ref.get(3, 0)]); + pdfDocument = getDocument(acroForm); + expect(pdfDocument.calculationOrderIds).toEqual(["1R", "2R", "3R"]); + + acroForm.set("CO", []); + pdfDocument = getDocument(acroForm); + expect(pdfDocument.calculationOrderIds).toEqual(null); + }); }); });