MCPcopy Create free account
hub / github.com/LibPDF-js/core / removeAnnotation

Method removeAnnotation

src/api/pdf-page.ts:2218–2266  ·  view source on GitHub ↗

* Remove a specific annotation from the page. * * Also removes any linked Popup annotation. * * @param annotation - The annotation to remove * * @example * ```typescript * const highlights = page.getHighlightAnnotations(); * page.removeAnnotation(highlights[0]); * ```

(annotation: PDFAnnotation)

Source from the content-addressed store, hash-verified

2216 * ```
2217 */
2218 removeAnnotation(annotation: PDFAnnotation): void {
2219 const annots = this.dict.getArray("Annots");
2220
2221 if (!annots) {
2222 return;
2223 }
2224
2225 const removeMatchingEntry = (predicate: (entry: unknown) => boolean): void => {
2226 for (let i = 0; i < annots.length; i++) {
2227 const entry = annots.at(i);
2228
2229 if (predicate(entry)) {
2230 annots.remove(i);
2231 return;
2232 }
2233 }
2234 };
2235
2236 const annotRef = annotation.ref;
2237
2238 if (annotRef) {
2239 // Find and remove the annotation reference
2240 removeMatchingEntry(
2241 entry =>
2242 entry instanceof PdfRef &&
2243 entry.objectNumber === annotRef.objectNumber &&
2244 entry.generation === annotRef.generation,
2245 );
2246 } else {
2247 // Direct annotation dict entry
2248 removeMatchingEntry(entry => entry instanceof PdfDict && entry === annotation.dict);
2249 }
2250
2251 // Check if the annotation has an associated Popup to remove
2252 const popup = annotation.dict.get("Popup");
2253
2254 if (popup instanceof PdfRef) {
2255 removeMatchingEntry(
2256 entry =>
2257 entry instanceof PdfRef &&
2258 entry.objectNumber === popup.objectNumber &&
2259 entry.generation === popup.generation,
2260 );
2261 } else if (popup instanceof PdfDict) {
2262 removeMatchingEntry(entry => entry instanceof PdfDict && entry === popup);
2263 }
2264
2265 this.invalidateAnnotationCache();
2266 }
2267
2268 /**
2269 * Remove annotations from the page.

Callers 2

removeAnnotationsMethod · 0.95

Calls 3

getArrayMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected