How Do I Limit the Number of Annotations in a PDF?

To limit the number of annotations in a PDF, it’s necessary to first add an event listener to the annotations.create event, so each time an annotation is created, you can check how many there are in the document. This can be achieved by using getAnnotations, which gives you the annotations for a given pageIndex. You then need to iterate over every page in the document and count them all together.

Once you reach the limit, set the view state to readOnly to disable annotation creation and editing:

  async (createdAnnotations) => {
    const annotations = await Promise.all(
        length: instance.totalPageCount
      }).map((_, pageIndex) => instance.getAnnotations(pageIndex))

    const annotationCount = annotations.flatMap((annotations) =>

    if (annotationCount > 9) {
      instance.setViewState((v) => v.set("readOnly", true));