diff --git a/src/core/document.js b/src/core/document.js index 0faa68c50..a3892751f 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -80,6 +80,8 @@ import { XRef } from "./xref.js"; const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792]; class Page { + #areAnnotationsCached = false; + #resourcesPromise = null; constructor({ @@ -839,6 +841,8 @@ class Page { return sortedAnnotations; }); + this.#areAnnotationsCached = true; + return shadow(this, "_parsedAnnotations", promise); } @@ -858,8 +862,20 @@ class Page { promises, annotationGlobals ) { - const annots = await this.pdfManager.ensure(this, "annotations"); const { pageIndex } = this; + + if (this.#areAnnotationsCached) { + const cachedAnnotations = await this._parsedAnnotations; + for (const { data } of cachedAnnotations) { + if (!types || types.has(data.annotationType)) { + data.pageIndex = pageIndex; + promises.push(Promise.resolve(data)); + } + } + return; + } + + const annots = await this.pdfManager.ensure(this, "annotations"); for (const annotationRef of annots) { promises.push( AnnotationFactory.create( diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 29d0ec910..c777bd18d 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -3256,6 +3256,42 @@ describe("api", function () { ]); await loadingTask.destroy(); }); + + it("gets editable annotations after getting annotations on page 13", async function () { + const loadingTask = getDocument( + buildGetDocumentParams("tracemonkey_with_editable_annotations.pdf") + ); + const pdfDoc = await loadingTask.promise; + const pdfPage = await pdfDoc.getPage(13); + await pdfPage.getAnnotations(); + + // Get all the editable annotations in the document. + const editableAnnotations = ( + await pdfDoc.getAnnotationsByType( + new Set([ + AnnotationType.FREETEXT, + AnnotationType.STAMP, + AnnotationType.INK, + AnnotationType.HIGHLIGHT, + ]), + null + ) + ).map(annotation => ({ + id: annotation.id, + subtype: annotation.subtype, + pageIndex: annotation.pageIndex, + })); + editableAnnotations.sort((a, b) => a.id.localeCompare(b.id)); + expect(editableAnnotations).toEqual([ + { id: "1000R", subtype: "FreeText", pageIndex: 12 }, + { id: "1001R", subtype: "Stamp", pageIndex: 12 }, + { id: "1011R", subtype: "Stamp", pageIndex: 13 }, + { id: "997R", subtype: "Ink", pageIndex: 13 }, + { id: "998R", subtype: "Highlight", pageIndex: 13 }, + ]); + + await loadingTask.destroy(); + }); }); });