How to create an external hyperlink
I can't find any documents around it, I could see an example from https://github.com/dolanmiu/docx/blob/master/demo/35-hyperlinks.ts but it seems out of date.
hi @crespowang I was able to get the hyperlinks working by following:
const { Hyperlink } = docx;
new Paragraph({
children: [
new Hyperlink(anchor_text, "", "http://google.com")
],
})
demo 35 is up to date with master, but not in 5.5.0
demo 35 way will be the way going forward in 6.0.0
I have a similar question. I can create the link:

but when I do Link -> Edit in word, the address doesn't show up:

here's the code:
const { Document, Paragraph, Hyperlink } = require('docx');
const doc = new Document();
doc.addSection({
children: [
new Paragraph({
children: [
new Hyperlink('This Is A Link', '', 'https://google.com/')
],
})
],
});
Also, the links doesn't work, when I click open.
The feature isn't released yet
The feature isn't released yet
Could you please explain how to create an external hyperlink with release 5.5.0?
there is a big discrepancy between what is released, what is available in the API and what is documented.... I have a hard time to figure out how stuff works.....
Sorry guys, 6.0.0 will be released soon, if you guys want 5.5.0 related examples, then just go to the 5.5.0 tag:
https://github.com/dolanmiu/docx/tree/5.5.0
Guys I've found out how to do this
import * as fs from "fs";
import { Document, HyperlinkRef, HyperlinkType, Packer, Paragraph, Media } from "../build";
const doc = new Document({
hyperlinks: {
myCoolLink: {
link: "http://www.example.com",
text: "Hyperlink",
type: HyperlinkType.EXTERNAL,
},
myOtherLink: {
link: "http://www.google.com",
text: "Google Link",
type: HyperlinkType.EXTERNAL,
},
},
});
const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
doc.addSection({
children: [
new Paragraph({
children: [new HyperlinkRef("myCoolLink")],
}),
new Paragraph({
children: [image1, new HyperlinkRef("myOtherLink")],
}),
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
The problem is that you can't add hyperlink on the fly. You need to know what links will be there when you create the document.
It would be nice to have something like this, so that you can create links with the Paragraph rather than the Document
const { Hyperlink } = docx;
new Paragraph({
children: [
new Hyperlink(anchor_text, "", "http://google.com")
],
})
Hopefully it's in 6.0.0 release.
I'm using version 6.0.3, and the following doesn't seem to work - I get empty text where the hyperlink should be.
The TextRuns before and after the hyperlink appear fine. Any idea?
new Paragraph({
children: [
new TextRun({ text: 'A', size: 22, font: 'Montserrat Light' }),
new ExternalHyperlink({
children: [
new TextRun({
text: 'link to google',
style: "Hyperlink",
size: 22, font: 'Montserrat Light'
}),
],
link: 'http://www.google.com'
}),
new TextRun({ text: `B`, size: 22, font: 'Montserrat Light' })
]
})
At the moment, ExternalHyperlink's take a single child in 6.0, in v7 it will allow multiple children
My code includes only a single child , but it doesn't seem to work. Is this the correct syntax?
Try similar to this:
const hyperlink = new ExternalHyperlink({
child: new TextRun({
text: 'link to google',
style: "Hyperlink",
size: 22, font: 'Montserrat Light'
}),
link: 'http://www.google.com'
});
The example from this point in time should likely work: https://github.com/dolanmiu/docx/blob/3299c557a0ff77dd07d7f14506b717b045fd6bba/demo/35-hyperlinks.ts