Search code examples

How to use annotation layer in PDF.js?

Some of the PDF.js code mentions an "annotation layer", for example AnnotationLayerBuilder here:

There is also pdfDocument.annotationStorage and pdfjsLib.AnnotationLayer, which - on all the documents I've tried - are empty, even in documents which do have text annotations.

I couldn't find any examples or documentation on the annotation layer and how it is supposed to be used, but it sure sounds interesting :)

  1. What is the annotation layer? Is this talking about standard PDF annotations, as described in or section 8.4 Annotations? Or, is it something internal to PDF.js?

  2. How do I list the existing annotations from javascript code in PDF.js, and how do I add one? (just for display; not expecting to be able to save it in the pdf, of course) Can anyone provide a working code example?



  • Annotations are standard for the PDF file format as described in the links you provided. They are not a new concept to PDF.js.

    How you get the annotations will depend on your situation. I'm building a view layer to replace the viewer the PDF.js team built. The basic idea there is you:

    1. Get a reference to the PDFDocumentProxy object via const doc = getDocument(url)
    2. Get a reference to a PDFPageProxy object via const page = await doc.getPage(num)
    3. Get the annotations via await page.getAnnotations()

    If you're already using the viewer they built, it doesn't appear to be exposed anywhere.