Basic operations ​
This page shows how to read, create, modify, delete, and flatten annotations with PDFPage and Annot. Annotation types are listed in Overview.
Related APIs include:
PDFPage.getAnnots(),PDFPage.getAnnotCountAsync(),PDFPage.getAnnotsByRect(): Read page annotations.PDFPage.addAnnot(): Create annotations on a page.PDFPage.removeAnnotById(),PDFPage.removeAnnotByObjectNumber(),PDFDoc.removeAllAnnots(): Delete annotations.PDFPage.flatten(),PDFPage.flattenAnnot(): Flatten annotations or form widgets.Annot.getType(),Annot.getRect(),Annot.setRect(),Annot.getContent(),Annot.setContent(),Annot.getFlags(),Annot.setFlags(): Read or modify common annotation properties.PDFPage.getAnnotIdAtDevicePoint(): Hit-test annotations at device coordinates.
Read page annotations ​
javascript
const page = await pdfDoc.getPageByIndex(0);
const annots = await page.getAnnots();
annots.forEach((annot) => {
console.log(annot.getType(), annot.getRect());
});For count only:
javascript
const count = await page.getAnnotCountAsync();Get annotations by rectangle ​
PDFPage.getAnnotsByRect(rect, tolerance?) returns annotations that intersect the rectangle. rect uses PDF coordinates; tolerance defaults to 8 (about 0–30).
javascript
const page = await pdfDoc.getPageByIndex(0);
const annots = await page.getAnnotsByRect(
{ left: 0, bottom: 0, right: 200, top: 200 },
8
);Create annotations ​
PDFPage.addAnnot(annotJson) creates annotations. Required properties vary by type; common fields include type, rect, contents, color, and coords.
javascript
const annots = await page.addAnnot({
type: 'note',
rect: {
left: 100,
right: 130,
top: 700,
bottom: 670
},
contents: 'Please confirm this content'
});
const note = annots[0];Text markup usually needs coords from a text selection or search results. With the full UI, prefer built-in text selection tools.
Use the current user as author when pasting ​
After pdfViewer.setCurrentUserAsAnnotationAuthor(true), pasted annotations use the current user as author. Set the current user identity first.
Modify annotation properties ​
javascript
const annot = annots[0];
await annot.setContent('Updated comment text');
await annot.setRect({
left: 120,
right: 180,
top: 720,
bottom: 680
});Export or save the PDF after changes to persist them.
Batch resize selected annotations ​
Constructor option enableBatchAnnotResize defaults to false. When true, dragging the resize box scales multiple selected annotations proportionally and keeps relative positions. Supported types include ink, square, circle, FreeText, area highlight, and screen; note, stamp, and pencil may not apply.
javascript
const pdfViewer = new PDFViewCtrl.PDFViewer({
enableBatchAnnotResize: true
});For non-selectable permission, see Annotation authority manager · selectable.
Delete and flatten annotations ​
javascript
const annotId = annot.getUniqueID();
await page.removeAnnotById(annotId);Remove all annotations ​
PDFDoc.removeAllAnnots() removes every annotation from the document and returns a map from page index to arrays of removed annotation object numbers. Annotation edit permission is required.
javascript
const removed = await pdfDoc.removeAllAnnots();Flattening writes annotation appearance into page content; the annotation is no longer editable as an annotation object.
javascript
await page.flattenAnnot(annot);Notes ​
- Annotation coordinates usually use PDF coordinates, not Viewer device pixels.
- Creating, modifying, deleting, or flattening annotations may require appropriate document permissions.
- Export or save the PDF after annotation changes to persist them.