Fix demos

This commit is contained in:
Dolan
2019-10-10 01:08:01 +01:00
parent f16126e948
commit 721de30587
4 changed files with 244 additions and 126 deletions

View File

@ -1,82 +1,153 @@
// Setting styles with JavaScript configuration // Setting styles with JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { AlignmentType, Document, Footer, HeadingLevel, Media, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; import {
AlignmentType,
Document,
Footer,
HeadingLevel,
Media,
Packer,
Paragraph,
Table,
TableCell,
TableRow,
TabStopPosition,
UnderlineType,
} from "../build";
const doc = new Document(); const doc = new Document({
styles: {
doc.Styles.createParagraphStyle("Heading1", "Heading 1") paragraphStyles: [
.basedOn("Normal") {
.next("Normal") id: "Heading1",
.quickFormat() name: "Heading 1",
.font("Calibri") basedOn: "Normal",
.size(52) next: "Normal",
.center() quickFormat: true,
.bold() run: {
.color("000000") font: "Calibri",
.spacing({ line: 340 }) size: 52,
.underline("single", "000000"); bold: true,
color: "000000",
doc.Styles.createParagraphStyle("Heading2", "Heading 2") underline: {
.basedOn("Normal") type: UnderlineType.SINGLE,
.next("Normal") color: "000000",
.font("Calibri") },
.quickFormat() },
.size(26) paragraph: {
.bold() alignment: AlignmentType.CENTER,
.spacing({ line: 340 }); spacing: { line: 340 },
},
doc.Styles.createParagraphStyle("Heading3", "Heading 3") },
.basedOn("Normal") {
.next("Normal") id: "Heading2",
.font("Calibri") name: "Heading 2",
.quickFormat() basedOn: "Normal",
.size(26) next: "Normal",
.bold() quickFormat: true,
.spacing({ line: 276 }); run: {
font: "Calibri",
doc.Styles.createParagraphStyle("Heading4", "Heading 4") size: 26,
.basedOn("Normal") bold: true,
.next("Normal") },
.justified() paragraph: {
.font("Calibri") spacing: { line: 340 },
.size(26) },
.bold(); },
{
doc.Styles.createParagraphStyle("normalPara", "Normal Para") id: "Heading3",
.basedOn("Normal") name: "Heading 3",
.next("Normal") basedOn: "Normal",
.font("Calibri") next: "Normal",
.quickFormat() quickFormat: true,
.leftTabStop(453.543307087) run: {
.maxRightTabStop() font: "Calibri",
.size(26) size: 26,
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); bold: true,
},
doc.Styles.createParagraphStyle("normalPara2", "Normal Para2") paragraph: {
.basedOn("Normal") spacing: { line: 276 },
.next("Normal") },
.quickFormat() },
.font("Calibri") {
.size(26) id: "Heading4",
.justified() name: "Heading 4",
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); basedOn: "Normal",
next: "Normal",
doc.Styles.createParagraphStyle("aside", "Aside") quickFormat: true,
.basedOn("Normal") run: {
.next("Normal") font: "Calibri",
.color("999999") size: 26,
.italics() bold: true,
.indent({ left: 720 }) },
.spacing({ line: 276 }); paragraph: {
alignment: AlignmentType.JUSTIFIED,
doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") },
.basedOn("Normal") },
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); {
id: "normalPara",
doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") name: "Normal Para",
.quickFormat() basedOn: "Normal",
.basedOn("Normal"); next: "Normal",
quickFormat: true,
run: {
font: "Calibri",
size: 26,
bold: true,
},
paragraph: {
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
rightTabStop: TabStopPosition.MAX,
leftTabStop: 453.543307087,
},
},
{
id: "normalPara2",
name: "Normal Para2",
basedOn: "Normal",
next: "Normal",
quickFormat: true,
run: {
font: "Calibri",
size: 26,
},
paragraph: {
alignment: AlignmentType.JUSTIFIED,
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
{
id: "aside",
name: "Aside",
basedOn: "Normal",
next: "Normal",
run: {
color: "999999",
italics: true,
},
paragraph: {
spacing: { line: 276 },
indent: { left: 720 },
},
},
{
id: "wellSpaced",
name: "Well Spaced",
basedOn: "Normal",
paragraph: {
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
{
id: "ListParagraph",
name: "List Paragraph",
basedOn: "Normal",
quickFormat: true,
},
],
},
});
const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));

View File

@ -1,28 +1,53 @@
// Custom styles using JavaScript configuration // Custom styles using JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, HeadingLevel, Packer, Paragraph } from "../build"; import { Document, HeadingLevel, Packer, Paragraph, UnderlineType } from "../build";
const doc = new Document(); const doc = new Document({
styles: {
// The first argument is an ID you use to apply the style to paragraphs paragraphStyles: [
// The second argument is a human-friendly name to show in the UI {
doc.Styles.createParagraphStyle("myWonkyStyle", "My Wonky Style") id: "myWonkyStyle",
.basedOn("Normal") name: "My Wonky Style",
.next("Normal") basedOn: "Normal",
.color("990000") next: "Normal",
.italics() run: {
.indent({ left: 720 }) // 720 TWIP === 720 / 20 pt === .5 in color: "990000",
.spacing({ line: 276 }); // 276 / 240 = 1.15x line spacing italics: true,
},
doc.Styles.createParagraphStyle("Heading2", "Heading 2") paragraph: {
.basedOn("Normal") indent: {
.next("Normal") left: 720,
.quickFormat() },
.size(26) // 26 half-points === 13pt font spacing: {
.bold() line: 276,
.underline("double", "FF0000") },
.spacing({ before: 240, after: 120 }); // TWIP for both },
},
{
id: "Heading2",
name: "Heading 2",
basedOn: "Normal",
next: "Normal",
quickFormat: true,
run: {
bold: true,
size: 26,
underline: {
type: UnderlineType.DOUBLE,
color: "FF0000",
},
},
paragraph: {
spacing: {
before: 240,
after: 120,
},
},
},
],
},
});
doc.addSection({ doc.addSection({
children: [ children: [

View File

@ -3,15 +3,23 @@
import * as fs from "fs"; import * as fs from "fs";
import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build";
const doc = new File(); const doc = new File({
styles: {
// The first argument is an ID you use to apply the style to paragraphs paragraphStyles: [
// The second argument is a human-friendly name to show in the UI {
doc.Styles.createParagraphStyle("MySpectacularStyle", "My Spectacular Style") id: "MySpectacularStyle",
.basedOn("Heading1") name: "My Spectacular Style",
.next("Heading1") basedOn: "Heading1",
.color("990000") next: "Heading1",
.italics(); quickFormat: true,
run: {
italics: true,
color: "990000",
},
},
],
},
});
// WordprocessingML docs for TableOfContents can be found here: // WordprocessingML docs for TableOfContents can be found here:
// http://officeopenxml.com/WPtableOfContents.php // http://officeopenxml.com/WPtableOfContents.php

View File

@ -1,18 +1,26 @@
// Multiple sections with total number of pages in each section // Multiple sections with total number of pages in each section
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, PageNumberFormat, TextRun } from "../build"; import { AlignmentType, Document, Packer, PageNumberFormat, TextRun, Header, Paragraph, Footer, PageBreak } from "../build";
const doc = new Document(); const doc = new Document();
const header = new Header({
children: [
new Paragraph({
children: [
new TextRun("Header on another page"),
new TextRun("Page Number: ").pageNumber(),
new TextRun(" to ").numberOfTotalPagesSection(),
],
alignment: AlignmentType.CENTER,
}),
],
});
const header = doc.createHeader(); const footer = new Footer({
header.createParagraph("Header on another page"); children: [new Paragraph("Foo Bar corp. ")],
const footer = doc.createFooter(); });
footer.createParagraph("Foo Bar corp. ")
.center()
.addRun(new TextRun("Page Number: ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPagesSection());
doc.addSection({ doc.addSection({
headers: { headers: {
@ -21,13 +29,17 @@ doc.addSection({
footers: { footers: {
default: footer, default: footer,
}, },
pageNumberStart: 1, properties: {
pageNumberFormatType: PageNumberFormat.DECIMAL, pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()],
}),
],
}); });
doc.createParagraph("Section 1").pageBreak();
doc.createParagraph("Section 1").pageBreak();
doc.addSection({ doc.addSection({
headers: { headers: {
default: header, default: header,
@ -35,15 +47,17 @@ doc.addSection({
footers: { footers: {
default: footer, default: footer,
}, },
pageNumberStart: 1, properties: {
pageNumberFormatType: PageNumberFormat.DECIMAL, pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()],
}),
],
}); });
doc.createParagraph("Section 2").pageBreak(); Packer.toBuffer(doc).then((buffer) => {
doc.createParagraph("Section 2").pageBreak();
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);
}); });