Files
docx-js/demo/28-table-of-contents.ts

65 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2019-02-26 22:20:20 +00:00
// Table of contents
// 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";
2019-10-10 01:08:01 +01:00
const doc = new File({
styles: {
paragraphStyles: [
{
id: "MySpectacularStyle",
name: "My Spectacular Style",
basedOn: "Heading1",
next: "Heading1",
quickFormat: true,
run: {
italics: true,
color: "990000",
},
},
],
},
});
2018-09-25 01:33:44 -03:00
// WordprocessingML docs for TableOfContents can be found here:
// http://officeopenxml.com/WPtableOfContents.php
2018-09-25 01:33:44 -03:00
// Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle,
// making the entries be hyperlinks for the paragraph
2019-07-31 08:48:02 +01:00
doc.addSection({
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,
}),
],
});
2018-09-25 01:33:44 -03:00
2019-08-07 22:12:14 +01:00
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});