I am currently working on incorporating hyperlinks into a PDF file using the pdf-lib JavaScript library. However, I've encountered an issue where when I click on a link in the PDF while viewing it in a browser, it opens the corresponding webpage in the same tab instead of a new tab. Is there a way to modify this behavior so that the link opens in a new tab?
export const drawLink = (
x: number,
y: number,
item: TransformedEvent,
haufefontSans: PDFFont,
currentPage: PDFPage,
pdfDoc: PDFDocument,
linkImg: PDFImage
) => {
if (getTexts.link(item).trim() === "nothing_to_link") {
return y;
} else {
const linkAnnotation: PDFDict = pdfDoc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [x, y - 10, x + 150, y + 10],
Border: [0, 0, 0],
C: [0, 0, 1],
A: {
Type: "Action",
S: "URI",
URI: PDFString.of(getTexts.link(item).trim()),
// NewWindow: true, <-- this does not work.
},
});
const linkAnnotationRef = pdfDoc.context.register(linkAnnotation);
currentPage.drawText("mehr erfahren", {
x: x + 18,
y: y,
size: 13,
color: red,
font: haufefontSans,
});
currentPage.drawImage(linkImg, {
x: x + 2,
y: y - 2,
width: 11,
height: 11,
});
y -= 13;
let existingAnnots: PDFObject[] = [];
const existingAnnotsObj = currentPage.node.get(PDFName.of("Annots"));
if (existingAnnotsObj instanceof PDFArray) {
existingAnnots = existingAnnotsObj.asArray().slice();
} else if (existingAnnotsObj) {
existingAnnots.push(existingAnnotsObj);
}
existingAnnots.push(linkAnnotationRef);
currentPage.node.set(
PDFName.of("Annots"),
pdfDoc.context.obj(existingAnnots)
);
return y;
}
};