Skip to main content

Annotations

Read, create, and remove PDF annotations. Requires the full tier.

import { PDFDocument, AnnotationType } from "pdfnova";

Read Annotations

const page = doc.getPage(0);
const annotations = await page.getAnnotations();

for (const annot of annotations) {
console.log(`Type: ${AnnotationType[annot.type]}`);
console.log(`Rect: ${JSON.stringify(annot.rect)}`);
console.log(`Contents: ${annot.contents}`);
console.log(`Author: ${annot.author}`);
}

AnnotationData

PropertyTypeDescription
indexnumberAnnotation index on the page
typeAnnotationTypeAnnotation type (Highlight, Text, etc.)
rectAnnotationRectBounding rectangle { left, top, right, bottom }
colorAnnotationColor?RGBA color { r, g, b, a }
contentsstring?Text content
authorstring?Author name
modificationDatestring?Last modified date
attachmentPointsAttachmentPoint[]?Quadrilateral points for text markup

Add Annotations

Highlight

await page.addAnnotation({
type: AnnotationType.Highlight,
rect: { left: 72, top: 720, right: 300, bottom: 700 },
color: { r: 255, g: 235, b: 59, a: 128 },
contents: "Important section",
});

Text Note

await page.addAnnotation({
type: AnnotationType.Text,
rect: { left: 500, top: 750, right: 520, bottom: 730 },
contents: "Review this paragraph",
});

Underline

await page.addAnnotation({
type: AnnotationType.Underline,
rect: { left: 72, top: 680, right: 400, bottom: 668 },
color: { r: 0, g: 0, b: 255, a: 200 },
});

Remove Annotations

// Remove the first annotation
await page.removeAnnotation(0);

Annotation Types

enum AnnotationType {
Text = 1,
Link = 2,
FreeText = 3,
Line = 4,
Square = 5,
Circle = 6,
Polygon = 7,
Polyline = 8,
Highlight = 9,
Underline = 10,
Squiggly = 11,
StrikeOut = 12,
Stamp = 13,
Ink = 15,
Popup = 16,
FileAttachment = 17,
Widget = 20,
Watermark = 24,
Redact = 28,
}

Save After Modification

// Add annotations...
await page.addAnnotation({ /* ... */ });

// Save the modified PDF
const bytes = await doc.save();
const blob = new Blob([bytes], { type: "application/pdf" });
const url = URL.createObjectURL(blob);