Files
docx-js/demo/35-hyperlinks.ts

164 lines
6.1 KiB
TypeScript
Raw Permalink Normal View History

2019-02-26 22:20:20 +00:00
// Example on how to add hyperlinks to websites
2023-06-05 00:33:43 +01:00
2018-12-24 16:50:53 +00:00
import * as fs from "fs";
2023-06-05 00:33:43 +01:00
import { Document, ExternalHyperlink, Footer, FootnoteReferenceRun, ImageRun, Packer, Paragraph, TextRun } from "docx";
2019-01-03 20:47:00 +00:00
2021-03-01 23:35:52 +00:00
const doc = new Document({
2023-06-28 21:40:20 +01:00
styles: {
default: {
hyperlink: {
run: {
color: "FF0000",
underline: {
color: "0000FF",
},
},
},
},
},
2021-03-11 01:06:55 +00:00
footnotes: {
1: {
2021-03-01 23:35:52 +00:00
children: [
2021-03-11 01:06:55 +00:00
new Paragraph({
children: [
new TextRun("Click here for the "),
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new TextRun({
text: "Footnotes external hyperlink",
style: "Hyperlink",
}),
],
2021-03-11 01:06:55 +00:00
link: "http://www.example.com",
}),
],
2021-03-01 23:35:52 +00:00
}),
],
2021-03-11 01:06:55 +00:00
},
},
2021-03-19 20:53:56 +00:00
sections: [
{
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun("Click here for the "),
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new TextRun({
text: "Footer external hyperlink",
style: "Hyperlink",
}),
],
2021-03-19 20:53:56 +00:00
link: "http://www.example.com",
}),
],
}),
],
}),
},
headers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun("Click here for the "),
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new TextRun({
text: "Header external hyperlink",
style: "Hyperlink",
}),
],
2021-03-19 20:53:56 +00:00
link: "http://www.google.com",
}),
],
}),
],
}),
},
children: [
new Paragraph({
children: [
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new TextRun({
text: "Anchor Text",
style: "Hyperlink",
}),
],
link: "http://www.example.com",
}),
2021-03-19 20:53:56 +00:00
new FootnoteReferenceRun(1),
],
}),
new Paragraph({
children: [
2021-03-19 20:53:56 +00:00
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new ImageRun({
data: fs.readFileSync("./demo/images/image1.jpeg"),
transformation: {
width: 100,
height: 100,
},
}),
],
2021-03-19 20:53:56 +00:00
link: "http://www.google.com",
}),
new ExternalHyperlink({
2021-08-31 18:55:38 -06:00
children: [
new TextRun({
text: "BBC News Link",
style: "Hyperlink",
}),
],
2021-03-19 20:53:56 +00:00
link: "https://www.bbc.co.uk/news",
}),
],
}),
2021-08-31 18:55:38 -06:00
new Paragraph({
children: [
new TextRun({
text: "This is a hyperlink with formatting: ",
}),
new ExternalHyperlink({
children: [
new TextRun({
text: "A ",
style: "Hyperlink",
}),
new TextRun({
text: "single ",
bold: true,
style: "Hyperlink",
}),
new TextRun({
text: "link",
doubleStrike: true,
style: "Hyperlink",
}),
new TextRun({
text: "1",
superScript: true,
style: "Hyperlink",
}),
new TextRun({
text: "!",
style: "Hyperlink",
}),
],
link: "http://www.example.com",
}),
],
}),
],
2021-03-19 20:53:56 +00:00
},
],
2019-07-31 08:48:02 +01:00
});
2018-12-24 16:50:53 +00:00
2019-08-07 22:12:14 +01:00
Packer.toBuffer(doc).then((buffer) => {
2018-12-24 16:50:53 +00:00
fs.writeFileSync("My Document.docx", buffer);
});