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

Method addLinkAnnotation

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

* Add a link annotation. * * @param options - Link options (uri or destination) * @returns The created annotation * * @example * ```typescript * // External link * page.addLinkAnnotation({ * rect: { x: 100, y: 600, width: 200, height: 20 }, * uri: "https://example.c

(options: LinkAnnotationOptions)

Source from the content-addressed store, hash-verified

2062 * ```
2063 */
2064 addLinkAnnotation(options: LinkAnnotationOptions): PDFLinkAnnotation {
2065 const destination = options.destination;
2066
2067 if (destination) {
2068 const destinationPage = destination.page;
2069
2070 const destinationPageRef =
2071 destinationPage instanceof PDFPage ? destinationPage.ref : destinationPage;
2072
2073 if (!(destinationPageRef instanceof PdfRef)) {
2074 throw new Error("Link destination page must be a PDFPage or PdfRef");
2075 }
2076
2077 const pageRefs = this.ctx.pages.getPages();
2078 const matchesPage = pageRefs.some(
2079 ref =>
2080 ref.objectNumber === destinationPageRef.objectNumber &&
2081 ref.generation === destinationPageRef.generation,
2082 );
2083
2084 if (!matchesPage) {
2085 throw new Error("Link destination page ref not found in document");
2086 }
2087 }
2088
2089 const annotDict = PDFLinkAnnotation.create(options);
2090
2091 // Register and add to page
2092 const annotRef = this.ctx.register(annotDict);
2093 this.addAnnotationRef(annotRef);
2094
2095 // oxlint-disable-next-line typescript/no-unsafe-type-assertion
2096 return createAnnotation(annotDict, annotRef, this.ctx.registry) as PDFLinkAnnotation;
2097 }
2098
2099 /**
2100 * Add a text annotation (sticky note).

Callers 2

Calls 5

addAnnotationRefMethod · 0.95
createAnnotationFunction · 0.90
getPagesMethod · 0.65
createMethod · 0.65
registerMethod · 0.45

Tested by

no test coverage detected