Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] JS - Add a function in api to get the fields ids in AcroForm::CO #12485

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
Dict,
isDict,
isName,
isRef,
isStream,
Ref,
} from "./primitives.js";
Expand Down Expand Up @@ -1012,6 +1013,21 @@ class PDFDocument {
Promise.all(allPromises).then(() => allFields)
);
}

get calculationOrderIds() {
calixteman marked this conversation as resolved.
Show resolved Hide resolved
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 };
4 changes: 4 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,15 @@ class PDFDocumentProxy {
getFieldObjects() {
return this._transport.getFieldObjects();
}

/**
calixteman marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<Array<string> | null>} A promise that is resolved with an
* {Array<string>} 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();
}
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/document_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});