2019-02-26 22:20:20 +00:00
|
|
|
// Example on how to add hyperlinks to websites
|
2018-12-24 16:50:53 +00:00
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2021-03-01 23:35:52 +00:00
|
|
|
import { Document, ExternalHyperlink, Footer, FootnoteReferenceRun, Media, Packer, Paragraph, TextRun } from "../build";
|
2019-01-03 20:47:00 +00:00
|
|
|
|
2021-03-01 23:35:52 +00:00
|
|
|
const doc = new Document({
|
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({
|
|
|
|
child: new TextRun({
|
|
|
|
text: "Footnotes external hyperlink",
|
|
|
|
style: "Hyperlink",
|
|
|
|
}),
|
|
|
|
link: "http://www.example.com",
|
|
|
|
}),
|
|
|
|
],
|
2021-03-01 23:35:52 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-11 01:06:55 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-01 23:35:52 +00:00
|
|
|
});
|
2018-12-24 16:50:53 +00:00
|
|
|
|
2021-03-15 02:41:37 +00:00
|
|
|
const image1 = Media.addImage({
|
|
|
|
document: doc,
|
|
|
|
data: fs.readFileSync("./demo/images/image1.jpeg"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
});
|
2020-02-27 10:44:07 +00:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
doc.addSection({
|
2021-02-28 16:04:21 +00:00
|
|
|
footers: {
|
|
|
|
default: new Footer({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Click here for the "),
|
|
|
|
new ExternalHyperlink({
|
|
|
|
child: new TextRun({
|
|
|
|
text: "Footer external hyperlink",
|
|
|
|
style: "Hyperlink",
|
|
|
|
}),
|
|
|
|
link: "http://www.example.com",
|
|
|
|
}),
|
|
|
|
],
|
2021-03-01 03:28:35 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
default: new Footer({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Click here for the "),
|
|
|
|
new ExternalHyperlink({
|
|
|
|
child: new TextRun({
|
|
|
|
text: "Header external hyperlink",
|
|
|
|
style: "Hyperlink",
|
|
|
|
}),
|
|
|
|
link: "http://www.google.com",
|
|
|
|
}),
|
|
|
|
],
|
2021-02-28 16:04:21 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
},
|
2019-09-30 22:56:21 +01:00
|
|
|
children: [
|
|
|
|
new Paragraph({
|
2021-02-27 19:23:29 +00:00
|
|
|
children: [
|
|
|
|
new ExternalHyperlink({
|
|
|
|
child: new TextRun({
|
|
|
|
text: "Anchor Text",
|
|
|
|
style: "Hyperlink",
|
|
|
|
}),
|
|
|
|
link: "http://www.example.com",
|
|
|
|
}),
|
2021-03-11 01:06:55 +00:00
|
|
|
new FootnoteReferenceRun(1),
|
2021-02-27 19:23:29 +00:00
|
|
|
],
|
2019-09-30 22:56:21 +01:00
|
|
|
}),
|
2020-02-27 10:44:07 +00:00
|
|
|
new Paragraph({
|
2021-02-27 19:23:29 +00:00
|
|
|
children: [
|
|
|
|
new ExternalHyperlink({
|
|
|
|
child: image1,
|
|
|
|
link: "http://www.google.com",
|
|
|
|
}),
|
|
|
|
new ExternalHyperlink({
|
|
|
|
child: new TextRun({
|
|
|
|
text: "BBC News Link",
|
|
|
|
style: "Hyperlink",
|
|
|
|
}),
|
|
|
|
link: "https://www.bbc.co.uk/news",
|
|
|
|
}),
|
|
|
|
],
|
2020-02-27 10:44:07 +00:00
|
|
|
}),
|
2019-09-30 22:56:21 +01: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);
|
|
|
|
});
|