Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use annotation layer in PDF.js?

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

https://github.com/mozilla/pdf.js/blob/95e102c07bc257c2120fd7fd9141762b2c813a1c/web/annotation_layer_builder.js#L118

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 https://pspdfkit.com/blog/2018/what-are-annotations/ or https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf 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?

Thanks!

like image 626
Alex I Avatar asked Aug 06 '20 07:08

Alex I


People also ask

How does PDF annotation work?

PDF annotation is adding suggestions in the form of comments or digital sticky notes to PDF documents in the proofing stage. Basically, PDF annotation refers to providing contextual feedback right inside deliverables in PDF format. Think eBooks, white papers, reports, etc.

How do I view annotations in PDF?

Show annotations and drawing markup toolsChoose Tools > Comment to open the Comment toolbar. The comments that you add to the document are displayed in the right pane.

Where is annotate in PDF?

Go to View > Show Markup Toolbar or click the (sketch pen) icon on the top right. Use the selection and the markup tools annotate the PDF. You can use type, sketch, draw, use shapes, add notes, sign, and change the format for all.


1 Answers

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.

like image 180
Kevin Beal Avatar answered Oct 31 '22 08:10

Kevin Beal