2019-02-26 22:20:20 +00:00
|
|
|
// Table of contents
|
2018-09-21 07:40:58 -03:00
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2019-06-17 01:51:57 +01:00
|
|
|
import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build";
|
2018-09-21 07:40:58 -03:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
// WordprocessingML docs for TableOfContents can be found here:
|
|
|
|
// http://officeopenxml.com/WPtableOfContents.php
|
|
|
|
|
|
|
|
// Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle,
|
|
|
|
// making the entries be hyperlinks for the paragraph
|
2019-10-10 01:08:01 +01:00
|
|
|
const doc = new File({
|
2021-05-26 08:36:05 +03:00
|
|
|
features: {
|
|
|
|
updateFields: true,
|
|
|
|
},
|
2019-10-10 01:08:01 +01:00
|
|
|
styles: {
|
|
|
|
paragraphStyles: [
|
|
|
|
{
|
|
|
|
id: "MySpectacularStyle",
|
|
|
|
name: "My Spectacular Style",
|
|
|
|
basedOn: "Heading1",
|
|
|
|
next: "Heading1",
|
|
|
|
quickFormat: true,
|
|
|
|
run: {
|
|
|
|
italics: true,
|
|
|
|
color: "990000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
new TableOfContents("Summary", {
|
|
|
|
hyperlink: true,
|
|
|
|
headingStyleRange: "1-5",
|
|
|
|
stylesWithLevels: [new StyleLevel("MySpectacularStyle", 1)],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
text: "Header #1",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
pageBreakBefore: true,
|
|
|
|
}),
|
|
|
|
new Paragraph("I'm a little text very nicely written.'"),
|
|
|
|
new Paragraph({
|
|
|
|
text: "Header #2",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
pageBreakBefore: true,
|
|
|
|
}),
|
|
|
|
new Paragraph("I'm a other text very nicely written.'"),
|
|
|
|
new Paragraph({
|
|
|
|
text: "Header #2.1",
|
|
|
|
heading: HeadingLevel.HEADING_2,
|
|
|
|
}),
|
|
|
|
new Paragraph("I'm a another text very nicely written.'"),
|
|
|
|
new Paragraph({
|
|
|
|
text: "My Spectacular Style #1",
|
|
|
|
style: "MySpectacularStyle",
|
|
|
|
pageBreakBefore: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
});
|
2018-09-25 01:33:44 -03:00
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-09-25 19:49:44 +01:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
2018-09-21 07:40:58 -03:00
|
|
|
});
|