-
-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy path66-fields.ts
43 lines (40 loc) · 1.51 KB
/
66-fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Use fields to include dynamic text
import * as fs from "fs";
import { Bookmark, Document, Packer, Paragraph, SimpleField, TextRun } from "docx";
const doc = new Document({
creator: "Me",
sections: [
{
properties: {},
children: [
new Paragraph({
children: [
new TextRun("This document is called "),
new SimpleField("FILENAME", "My Document.docx"),
new TextRun(", was created on "),
new SimpleField('CREATEDATE \\@ "d MMMM yyyy"'),
new TextRun(" by "),
new SimpleField("AUTHOR"),
],
}),
new Paragraph({
children: [
new TextRun("The document has "),
new SimpleField("NUMWORDS", "34"),
new TextRun(" words and if you'd print it "),
new Bookmark({
id: "TimesPrinted",
children: [new TextRun("42")],
}),
new TextRun(" times two-sided, you would need "),
new SimpleField("=INT((TimesPrinted+1)/2)"),
new TextRun(" sheets of paper."),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});