Skip to content

Commit ebc3411

Browse files
committed
Use the cached annotations when collecting them by types
1 parent e20ee99 commit ebc3411

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/core/document.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ import { XRef } from "./xref.js";
8080
const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
8181

8282
class Page {
83+
#areAnnotationsCached = false;
84+
8385
#resourcesPromise = null;
8486

8587
constructor({
@@ -839,6 +841,8 @@ class Page {
839841
return sortedAnnotations;
840842
});
841843

844+
this.#areAnnotationsCached = true;
845+
842846
return shadow(this, "_parsedAnnotations", promise);
843847
}
844848

@@ -858,8 +862,20 @@ class Page {
858862
promises,
859863
annotationGlobals
860864
) {
861-
const annots = await this.pdfManager.ensure(this, "annotations");
862865
const { pageIndex } = this;
866+
867+
if (this.#areAnnotationsCached) {
868+
const cachedAnnotations = await this._parsedAnnotations;
869+
for (const { data } of cachedAnnotations) {
870+
if (!types || types.has(data.annotationType)) {
871+
data.pageIndex = pageIndex;
872+
promises.push(Promise.resolve(data));
873+
}
874+
}
875+
return;
876+
}
877+
878+
const annots = await this.pdfManager.ensure(this, "annotations");
863879
for (const annotationRef of annots) {
864880
promises.push(
865881
AnnotationFactory.create(

test/unit/api_spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,42 @@ describe("api", function () {
32563256
]);
32573257
await loadingTask.destroy();
32583258
});
3259+
3260+
it("gets editable annotations after getting annotations on page 13", async function () {
3261+
const loadingTask = getDocument(
3262+
buildGetDocumentParams("tracemonkey_with_editable_annotations.pdf")
3263+
);
3264+
const pdfDoc = await loadingTask.promise;
3265+
const pdfPage = await pdfDoc.getPage(13);
3266+
await pdfPage.getAnnotations();
3267+
3268+
// Get all the editable annotations in the document.
3269+
const editableAnnotations = (
3270+
await pdfDoc.getAnnotationsByType(
3271+
new Set([
3272+
AnnotationType.FREETEXT,
3273+
AnnotationType.STAMP,
3274+
AnnotationType.INK,
3275+
AnnotationType.HIGHLIGHT,
3276+
]),
3277+
null
3278+
)
3279+
).map(annotation => ({
3280+
id: annotation.id,
3281+
subtype: annotation.subtype,
3282+
pageIndex: annotation.pageIndex,
3283+
}));
3284+
editableAnnotations.sort((a, b) => a.id.localeCompare(b.id));
3285+
expect(editableAnnotations).toEqual([
3286+
{ id: "1000R", subtype: "FreeText", pageIndex: 12 },
3287+
{ id: "1001R", subtype: "Stamp", pageIndex: 12 },
3288+
{ id: "1011R", subtype: "Stamp", pageIndex: 13 },
3289+
{ id: "997R", subtype: "Ink", pageIndex: 13 },
3290+
{ id: "998R", subtype: "Highlight", pageIndex: 13 },
3291+
]);
3292+
3293+
await loadingTask.destroy();
3294+
});
32593295
});
32603296
});
32613297

0 commit comments

Comments
 (0)