Check if document contains annotations

Q: How can I check if a document contains any annotation?

A: This can be done by using [PSPDFKit.Instance.getAnnotations][] to retrieve the annotations for each page, then flattening the array and finally evaluating if the resulting array is empty or not.

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PSPDFKit.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;
    });

This has been tested with PSPDFKit for Web 2020.5.1.