Check if a document contains annotations
This guide explains different approaches for determining whether a document contains any annotations, without needing to count or process them all.
Method 1: Check page by page (most efficient)
The most efficient approach is to check pages one by one until you find an annotation. This method stops searching as soon as it finds the first annotation:
async function hasAnnotations(instance, pageIndex) { const annotations = await instance.getAnnotations(pageIndex); return annotations.size > 0;}
// Usage exampleasync function checkDocument(instance) { for (let i = 0; i < instance.totalPageCount; i++) { if (await hasAnnotations(instance, i)) { console.log("Document contains annotations"); return true; } } console.log("No annotations found"); return false;}
Method 2: Process all annotations at once
To load all annotations simultaneously, use Instance#getAnnotations
with Promise.all
to retrieve annotations for each page, flatten the array, and evaluate whether it’s empty:
NutrientViewer.load({ // Your configuration.}).then(async (instance) => { const pagesAnnotations = await Promise.all( Array.from({ length: instance.totalPageCount }).map((_, pageIndex) => instance.getAnnotations(pageIndex)) );
const allAnnotations = pagesAnnotations .map((pageList) => pageList.toJS()) .flat();
const hasAnnotations = allAnnotations.length > 0; console.log(`Document has annotations: ${hasAnnotations}`); console.log(`Total annotations found: ${allAnnotations.length}`);});
Performance considerations
- Method 1 — Most efficient for checking existence, as it stops immediately upon finding any annotation. Uses separate helper functions for better code organization. Best for large documents where annotations are likely present.
- Method 2 — Processes all pages concurrently using
Promise.all
. Use this when you need the complete annotation list, not just existence checking.
For documents with 100+ pages, Method 1 can be significantly faster than Method 2 if annotations exist on early pages.