Make .addSection fully declarative

This commit is contained in:
Dolan
2021-03-19 20:53:56 +00:00
parent 4783812044
commit 3299c557a0
86 changed files with 3341 additions and 3278 deletions

8
.nycrc
View File

@ -1,9 +1,9 @@
{
"check-coverage": true,
"lines": 98.79,
"functions": 97.54,
"branches": 95.64,
"statements": 98.8,
"lines": 98.86,
"functions": 97.86,
"branches": 95.86,
"statements": 98.86,
"include": [
"src/**/*.ts"
],

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -22,6 +22,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -129,9 +129,9 @@ const achievements = [
class DocumentCreator {
// tslint:disable-next-line: typedef
public create([experiences, educations, skills, achivements]): Document {
const document = new Document();
document.addSection({
const document = new Document({
sections: [
{
children: [
new Paragraph({
text: "Dolan Miu",
@ -143,7 +143,10 @@ class DocumentCreator {
.map((education) => {
const arr: Paragraph[] = [];
arr.push(
this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`),
this.createInstitutionHeader(
education.schoolName,
`${education.startDate.year} - ${education.endDate.year}`,
),
);
arr.push(this.createRoleText(`${education.fieldOfStudy} - ${education.degree}`));
@ -190,10 +193,13 @@ class DocumentCreator {
),
new Paragraph("More references upon request"),
new Paragraph({
text: "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.",
text:
"This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.",
alignment: AlignmentType.CENTER,
}),
],
},
],
});
return document;

View File

@ -17,6 +17,39 @@ import {
UnderlineType,
} from "../build";
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 1.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 2.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 3.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 4.")],
}),
],
}),
],
});
const doc = new Document({
styles: {
default: {
@ -124,42 +157,8 @@ const doc = new Document({
},
],
},
});
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 1.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 2.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 3.")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("Test cell 4.")],
}),
],
}),
],
});
doc.addSection({
sections: [
{
properties: {
top: 700,
right: 700,
@ -258,6 +257,8 @@ doc.addSection({
style: "normalPara2",
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, ImageRun, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph("Hello World"),
new Paragraph({
@ -53,6 +53,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -7,9 +7,8 @@ const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8");
const doc = new Document({
title: "Title",
externalStyles: styles,
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "Cool Heading Text",
@ -25,6 +24,8 @@ doc.addSection({
style: "MyFancyStyle",
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
titlePage: true,
},
@ -71,6 +71,8 @@ doc.addSection({
}),
new Paragraph("Second Page"),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph("Hello World"),
new Paragraph({
@ -13,6 +13,8 @@ doc.addSection({
pageBreakBefore: true,
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,13 +3,12 @@
import * as fs from "fs";
import { Document, Footer, Header, Packer, PageNumber, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [new Paragraph("Hello World")],
});
doc.addSection({
},
{
headers: {
default: new Header({
children: [new Paragraph("First Default Header on another page")],
@ -25,9 +24,8 @@ doc.addSection({
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [new Paragraph("hello")],
});
doc.addSection({
},
{
headers: {
default: new Header({
children: [new Paragraph("Second Default Header on another page")],
@ -46,9 +44,8 @@ doc.addSection({
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [new Paragraph("hello in landscape")],
});
doc.addSection({
},
{
headers: {
default: new Header({
children: [
@ -66,9 +63,8 @@ doc.addSection({
orientation: PageOrientation.PORTRAIT,
},
children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")],
});
doc.addSection({
},
{
headers: {
default: new Header({
children: [
@ -91,9 +87,8 @@ doc.addSection({
"Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.",
),
],
});
doc.addSection({
},
{
headers: {
default: new Header({
children: [
@ -115,7 +110,11 @@ doc.addSection({
pageNumberStart: 25,
},
children: [
new Paragraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section."),
new Paragraph(
"Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section.",
),
],
},
],
});

View File

@ -12,9 +12,8 @@ const doc = new Document({
5: { children: [new Paragraph("Test1")] },
6: { children: [new Paragraph("My amazing reference1")] },
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
children: [
@ -30,9 +29,8 @@ doc.addSection({
children: [new TextRun("Hello World"), new FootnoteReferenceRun(3)],
}),
],
});
doc.addSection({
},
{
children: [
new Paragraph({
children: [
@ -48,6 +46,8 @@ doc.addSection({
children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,11 +3,11 @@
import * as fs from "fs";
import { Document, ImageRun, Packer, Paragraph } from "../build";
const doc = new Document();
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAhHh4iHx8iHx8jHx8jHyAjHSAjHSAiHh4iHh4jHx8jHx8jHyAjHyAhHSAhHSAiHh4iHh4jHx8jHx8jHyAhHyAhHSAiHSAiHh4jHh4jHx8jHx8jHyAhHyAhHSAiHSAjHR4jHh4jHx8jHx8hHyAhHyAiHSAjHS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JAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
@ -21,6 +21,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
@ -21,6 +21,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBase64String(doc).then((str) => {

View File

@ -99,9 +99,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "Test heading1, bold and italicized",
@ -184,6 +183,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,11 @@
import * as fs from "fs";
import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build";
const doc = new Document();
const table = new Table({
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [
new TableRow({
children: [
@ -94,10 +96,12 @@ const table = new Table({
],
}),
],
}),
],
},
],
});
doc.addSection({ children: [table] });
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -10,9 +10,8 @@ const doc = new Document({
creator: "Clippy",
title: "Sample Document",
description: "A brief example of using docx with bookmarks and internal hyperlinks",
});
doc.addSection({
sections: [
{
footers: {
default: new Footer({
children: [
@ -57,6 +56,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, Table, TableCell, TableRow, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
bidirectional: true,
@ -62,6 +62,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,11 +3,11 @@
import * as fs from "fs";
import { Document, ImageRun, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAh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M8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
@ -77,6 +77,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,11 @@
import * as fs from "fs";
import { Document, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "../build";
const doc = new Document();
const table = new Table({
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [
new TableRow({
children: [
@ -84,10 +86,10 @@ const table = new Table({
],
}),
],
});
doc.addSection({
children: [table],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -4,18 +4,13 @@ import * as fs from "fs";
import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build";
const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8");
const doc = new Document({
title: "Title",
externalStyles: styles
});
// Create a table and pass the XML Style
const table = new Table({
style: 'MyCustomTableStyle',
style: "MyCustomTableStyle",
width: {
size: 9070,
type: WidthType.DXA
type: WidthType.DXA,
},
rows: [
new TableRow({
@ -41,8 +36,14 @@ const table = new Table({
],
});
doc.addSection({
const doc = new Document({
title: "Title",
externalStyles: styles,
sections: [
{
children: [table],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph("No border!"),
new Paragraph({
@ -26,6 +26,8 @@ doc.addSection({
},
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -47,9 +47,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "Hello",
@ -60,6 +59,8 @@ doc.addSection({
heading: HeadingLevel.HEADING_2,
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,6 +3,11 @@
import * as fs from "fs";
import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build";
// 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
const doc = new File({
styles: {
paragraphStyles: [
@ -19,15 +24,8 @@ const doc = new File({
},
],
},
});
// 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
doc.addSection({
sections: [
{
children: [
new TableOfContents("Summary", {
hyperlink: true,
@ -57,6 +55,8 @@ doc.addSection({
pageBreakBefore: true,
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -57,9 +57,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "line with contextual spacing",
@ -277,6 +276,8 @@ doc.addSection({
},
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -117,9 +117,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "Hey you",
@ -248,6 +247,8 @@ doc.addSection({
},
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -12,16 +12,21 @@ fs.readFile(filePath, (err, data) => {
}
importDotx.extract(data).then((templateDocument) => {
const doc = new Document(undefined, {
template: templateDocument,
});
doc.addSection({
const doc = new Document(
{
sections: [
{
properties: {
titlePage: templateDocument.titlePageIsDefined,
},
children: [new Paragraph("Hello World")],
});
},
],
},
{
template: templateDocument,
},
);
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);

View File

@ -3,9 +3,11 @@
import * as fs from "fs";
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "../build";
const doc = new Document();
const table = new Table({
const doc = new Document({
sections: [
{
children: [
new Table({
rows: [
new TableRow({
children: [
@ -65,10 +67,10 @@ const table = new Table({
],
}),
],
});
doc.addSection({
children: [table],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -17,8 +17,6 @@ import {
WidthType,
} from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -376,8 +374,9 @@ const table8 = new Table({
type: WidthType.PERCENTAGE,
},
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [
table,
new Paragraph({
@ -401,6 +400,8 @@ doc.addSection({
new Paragraph("Merging columns 5"),
table8,
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, SequentialIdentifier, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
@ -40,6 +40,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -16,8 +16,6 @@ import {
WidthType,
} from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -57,8 +55,12 @@ const table = new Table({
layout: TableLayoutType.FIXED,
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [table],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -22,9 +22,8 @@ const doc = new Document({
],
},
},
});
doc.addSection({
sections: [
{
footers: {
default: new Footer({
children: [
@ -96,6 +95,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { Document, Header, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -69,13 +67,17 @@ const table = new Table({
});
// Adding same table in the body and in the header
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [table],
}),
},
children: [table],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Header, ImageRun, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [
@ -46,6 +46,8 @@ doc.addSection({
}),
},
children: [new Paragraph("Hello World")],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -4,9 +4,9 @@ import * as fs from "fs";
// import { Document, Packer, Paragraph } from "../build";
import { Document, ImageRun, Packer, Paragraph, TextWrappingSide, TextWrappingType } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vehicula nec nulla vitae efficitur. Ut interdum mauris eu ipsum rhoncus, nec pharetra velit placerat. Sed vehicula libero ac urna molestie, id pharetra est pellentesque. Praesent iaculis vehicula fringilla. Duis pretium gravida orci eu vestibulum. Mauris tincidunt ipsum dolor, ut ornare dolor pellentesque id. Integer in nulla gravida, lacinia ante non, commodo ex. Vivamus vulputate nisl id lectus finibus vulputate. Ut et nisl mi. Cras fermentum augue arcu, ac accumsan elit euismod id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed ac posuere nisi. Pellentesque tincidunt vehicula bibendum. Phasellus eleifend viverra nisl.",
@ -17,7 +17,8 @@ doc.addSection({
new Paragraph(
"Ut eget diam cursus quam accumsan interdum at id ante. Ut mollis mollis arcu, eu scelerisque dui tempus in. Quisque aliquam, augue quis ornare aliquam, ex purus ultrices mauris, ut porta dolor dolor nec justo. Nunc a tempus odio, eu viverra arcu. Suspendisse vitae nibh nec mi pharetra tempus. Mauris ut ullamcorper sapien, et sagittis sapien. Vestibulum in urna metus. In scelerisque, massa id bibendum tempus, quam orci rutrum turpis, a feugiat nisi ligula id metus. Praesent id dictum purus. Proin interdum ipsum nulla.",
),
new Paragraph(
new Paragraph({
children: [
new ImageRun({
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
@ -41,7 +42,10 @@ doc.addSection({
},
},
}),
),
],
}),
],
},
],
});

View File

@ -3,9 +3,17 @@
import * as fs from "fs";
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
const doc = new Document({});
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
page: {
pageNumbers: {
start: 1,
formatType: PageNumberFormat.DECIMAL,
},
},
},
headers: {
default: new Header({
children: [
@ -41,10 +49,6 @@ doc.addSection({
],
}),
},
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Hello World 1"), new PageBreak()],
@ -62,6 +66,8 @@ doc.addSection({
children: [new TextRun("Hello World 5"), new PageBreak()],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build";
const doc = new Document();
const table = new Table({
columnWidths: [3505, 5505],
rows: [
@ -114,7 +112,9 @@ const table3 = new Table({
],
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({ text: "Table with skewed widths" }),
table,
@ -123,6 +123,8 @@ doc.addSection({
new Paragraph({ text: "Table without setting widths" }),
table3,
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,12 +3,14 @@
import * as fs from "fs";
import { Document, HeadingLevel, LineNumberRestartFormat, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
lineNumberCountBy: 1,
lineNumberRestart: LineNumberRestartFormat.CONTINUOUS,
lineNumbers: {
countBy: 1,
restart: LineNumberRestartFormat.CONTINUOUS,
},
},
children: [
new Paragraph({
@ -22,6 +24,8 @@ doc.addSection({
"Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse.",
),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -252,8 +250,12 @@ const table2 = new Table({
],
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [table, new Paragraph(""), table2],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { AlignmentType, Document, Header, Packer, PageBreak, PageNumber, PageNumberSeparator, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [
@ -40,12 +40,15 @@ doc.addSection({
}),
new Paragraph("Second Page"),
],
});
doc.addSection({
},
{
properties: {
pageNumberStart: 1,
pageNumberSeparator: PageNumberSeparator.EM_DASH
page: {
pageNumbers: {
start: 1,
separator: PageNumberSeparator.EM_DASH,
},
},
},
headers: {
default: new Header({
@ -81,6 +84,8 @@ doc.addSection({
}),
new Paragraph("Fourth Page"),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -75,8 +73,12 @@ const table = new Table({
],
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [table],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
column: {
space: 708,
@ -18,9 +18,8 @@ doc.addSection({
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
),
],
});
doc.addSection({
},
{
properties: {
column: {
space: 708,
@ -33,9 +32,8 @@ doc.addSection({
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
),
],
});
doc.addSection({
},
{
properties: {
column: {
space: 708,
@ -49,6 +47,8 @@ doc.addSection({
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { AlignmentType, Document, Header, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [
@ -28,6 +28,8 @@ doc.addSection({
}),
},
children: [],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { AlignmentType, Document, Header, Packer, Paragraph, ShadingType, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [
@ -57,6 +57,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
const doc = new Document();
const header = new Header({
children: [
new Paragraph({
@ -26,40 +24,51 @@ const footer = new Footer({
children: [new Paragraph("Foo Bar corp. ")],
});
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
page: {
pageNumbers: {
start: 1,
formatType: PageNumberFormat.DECIMAL,
},
},
},
headers: {
default: header,
},
footers: {
default: footer,
},
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()],
}),
],
});
doc.addSection({
},
{
properties: {
page: {
pageNumbers: {
start: 1,
formatType: PageNumberFormat.DECIMAL,
},
},
},
headers: {
default: header,
},
footers: {
default: footer,
},
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, SectionVerticalAlignValue, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
verticalAlign: SectionVerticalAlignValue.CENTER,
},
@ -24,6 +24,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -15,8 +15,6 @@ import {
VerticalAlign,
} from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -160,7 +158,9 @@ const noBorderTable = new Table({
],
});
doc.addSection({ children: [table, new Paragraph("Hello"), noBorderTable] });
const doc = new Document({
sections: [{ children: [table, new Paragraph("Hello"), noBorderTable] }],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);

View File

@ -12,9 +12,9 @@ import {
VerticalPositionRelativeFrom,
} from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph("Hello World"),
new Paragraph({
@ -127,6 +127,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,8 +3,6 @@
import * as fs from "fs";
import { Document, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build";
const doc = new Document();
const table = new Table({
rows: [
new TableRow({
@ -66,7 +64,9 @@ const table = new Table({
],
});
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new Paragraph({
text: "Hello World",
@ -85,6 +85,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -25,9 +25,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
children: [
@ -50,6 +49,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -18,9 +18,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "KFCを食べるのが好き",
@ -30,6 +29,8 @@ doc.addSection({
text: "こんにちは",
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -41,9 +41,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "中文和英文 Chinese and English",
@ -83,6 +82,8 @@ doc.addSection({
発闘美慎健育旅布容広互無秋認天歌媛芸。転器合読県増認会賞倒点系。食気母服絵知去祝芸車報熱勝。能貿月更障文的欠賞現覇声敏施会。懲病写昼法族業律記聡生開緊楽暮護。東地二員者説盟治害討面提。第北乗査庭年近的禁疑報方店記必迷都流通。聞有力前愛院梨野関業前訳本清滋補。蒲読火死勝広保会婚際気二由保国。用君込村需起相点選紙拡氏訃不。`,
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -22,9 +22,9 @@ import {
TextRun,
} from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -287,6 +287,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -7,9 +7,8 @@ const doc = new Document({
background: {
color: "C45911",
},
});
doc.addSection({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -26,6 +25,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -40,9 +40,8 @@ const doc = new Document({
},
],
},
});
doc.addSection({
sections: [
{
children: [
new Paragraph({
text: "How to make cake",
@ -81,6 +80,8 @@ doc.addSection({
heading: HeadingLevel.HEADING_1,
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun, SectionType } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -18,9 +18,8 @@ doc.addSection({
],
}),
],
});
doc.addSection({
},
{
properties: {
type: SectionType.CONTINUOUS,
},
@ -35,9 +34,8 @@ doc.addSection({
],
}),
],
});
doc.addSection({
},
{
properties: {
type: SectionType.ODD_PAGE,
},
@ -52,9 +50,8 @@ doc.addSection({
],
}),
],
});
doc.addSection({
},
{
properties: {
type: SectionType.EVEN_PAGE,
},
@ -69,9 +66,8 @@ doc.addSection({
],
}),
],
});
doc.addSection({
},
{
properties: {
type: SectionType.NEXT_PAGE,
},
@ -86,6 +82,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Footer, Header, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
header: 100,
footer: 50,
@ -41,6 +41,8 @@ doc.addSection({
}),
},
children: [new Paragraph("Hello World")],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,15 +3,19 @@
import * as fs from "fs";
import { Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
margins: {
const doc = new Document({
sections: [
{
properties: {
page: {
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0,
},
},
},
children: [
new Paragraph({
children: [
@ -33,6 +37,8 @@ doc.addSection({
new Paragraph("Foo bar"),
new Paragraph("Github is the best"),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -26,6 +26,27 @@ import {
Note that this setting enables to track *new changes* after teh file is generated, so this example will still show inserted and deleted text runs when you remove it.
*/
const paragraph = new Paragraph({
children: [
new TextRun("This is a simple demo "),
new TextRun({
text: "on how to ",
}),
new InsertedTextRun({
text: "mark a text as an insertion ",
id: 0,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
new DeletedTextRun({
text: "or a deletion.",
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
],
});
const doc = new Document({
footnotes: {
1: {
@ -53,30 +74,8 @@ const doc = new Document({
features: {
trackRevisions: true,
},
});
const paragraph = new Paragraph({
children: [
new TextRun("This is a simple demo "),
new TextRun({
text: "on how to ",
}),
new InsertedTextRun({
text: "mark a text as an insertion ",
id: 0,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
new DeletedTextRun({
text: "or a deletion.",
id: 1,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
}),
],
});
doc.addSection({
sections: [
{
properties: {},
children: [
paragraph,
@ -143,6 +142,8 @@ doc.addSection({
],
}),
},
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, FrameAnchorType, HorizontalPositionAlign, Packer, Paragraph, TextRun, VerticalPositionAlign } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -64,6 +64,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, LineRuleType, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
@ -27,6 +27,8 @@ doc.addSection({
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -5,9 +5,8 @@ import { Document, Footer, Header, Packer, PageBreak, Paragraph, TextRun } from
const doc = new Document({
evenAndOddHeaderAndFooters: true,
});
doc.addSection({
sections: [
{
headers: {
default: new Header({
children: [
@ -63,6 +62,8 @@ doc.addSection({
children: [new TextRun("Hello World 5"), new PageBreak()],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,13 +3,19 @@
import * as fs from "fs";
import { Document, Packer, PageOrientation, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
properties: {
page: {
size: {
orientation: PageOrientation.LANDSCAPE,
},
},
},
children: [new Paragraph("Hello World")],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Footer, Header, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [new Paragraph("Header text")],
@ -17,6 +17,8 @@ doc.addSection({
}),
},
children: [new Paragraph("Hello World")],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -3,9 +3,9 @@
import * as fs from "fs";
import { Document, Footer, Header, ImageRun, Packer, Paragraph } from "../build";
const doc = new Document();
doc.addSection({
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [
@ -41,6 +41,8 @@ doc.addSection({
}),
},
children: [new Paragraph("Hello World")],
},
],
});
Packer.toBuffer(doc).then((buffer) => {

View File

@ -14,7 +14,9 @@
function generate() {
const doc = new docx.Document();
doc.addSection({
const doc = new Document({
sections: [
{
children: [
new docx.Paragraph({
children: [
@ -30,10 +32,10 @@
],
}),
],
},
],
});
docx.Packer.toBlob(doc).then((blob) => {
console.log(blob);
saveAs(blob, "example.docx");

View File

@ -24,12 +24,10 @@ import { ... } from "docx";
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
// Create document
const doc = new Document();
// Documents contain sections, you can have multiple sections per document, go here to learn more about sections
// This simple example will only contain one section
doc.addSection({
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
@ -46,6 +44,7 @@ doc.addSection({
],
}),
],
}];
});
// Used to export the file into a .docx file

View File

@ -57,12 +57,14 @@ text.contents = "Hello World";
**Do**
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
}];
});
```

View File

@ -6,7 +6,8 @@
To make a bullet point, simply make a paragraph into a bullet point:
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [
new Paragraph({
text: "Bullet points",
@ -21,6 +22,7 @@ doc.addSection({
}
})
],
}];
});
```

View File

@ -5,7 +5,8 @@
Every Section has a sections which you can define its Headers and Footers:
```ts
doc.addSection({
const doc = new Document({
sections: [{
headers: {
default: new Header({ // The standard default header
children: [],
@ -29,6 +30,7 @@ doc.addSection({
}),
},
children: [],
}];
});
```

View File

@ -37,20 +37,14 @@ const image = new ImageRun({
Add it into the document by adding the image into a paragraph:
```ts
doc.addSection({
children: [new Paragraph(image)],
});
```
Or:
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [image],
}),
],
}];
});
```
@ -59,16 +53,22 @@ doc.addSection({
Adding images can be easily done by creating an instance of `ImageRun`. This can be added in a `Paragraph` or `Hyperlink`:
```ts
const image = new ImageRun({
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [
new ImageRun({
data: [IMAGE_BUFFER],
transformation: {
width: [IMAGE_SIZE],
height: [IMAGE_SIZE],
},
});
doc.addSection({
children: [new Paragraph(image)],
}),
],
}),
],
}];
});
```

View File

@ -35,20 +35,24 @@ const paragraph = new Paragraph({
After you create the paragraph, you must add the paragraph into a `section`:
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [paragraph],
}];
});
```
Or the preferred convension, define the paragraph inside the section and remove the usage of variables:
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")],
}),
],
}];
});
```

View File

@ -11,12 +11,14 @@ For example, you could have one section which is portrait with a header and foot
This creates a simple section in a document with one paragraph inside:
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
}];
});
```
@ -35,7 +37,8 @@ Setting the section type determines how the contents of the section will be plac
- `ODD_PAGE`
```ts
doc.addSection({
const doc = new Document({
sections: [{
properties: {
type: SectionType.CONTINUOUS,
}
@ -44,5 +47,6 @@ doc.addSection({
children: [new TextRun("Hello World")],
}),
],
}];
});
```

View File

@ -19,8 +19,10 @@ const table = new Table({
Then add the table in the `section`
```ts
doc.addSection({
const doc = new Document({
sections: [{
children: [table],
}];
});
```

View File

@ -1,38 +1,10 @@
import { expect } from "chai";
import { File, HeadingLevel, Media, Paragraph } from "file";
import { Media } from "file";
import { ImageReplacer } from "./image-replacer";
describe("ImageReplacer", () => {
let file: File;
beforeEach(() => {
file = new File({
creator: "Dolan Miu",
revision: "1",
lastModifiedBy: "Dolan Miu",
});
file.addSection({
children: [
new Paragraph({
text: "title",
heading: HeadingLevel.TITLE,
}),
new Paragraph({
text: "Hello world",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph({
text: "heading 2",
heading: HeadingLevel.HEADING_2,
}),
new Paragraph("test text"),
],
});
});
describe("#replace()", () => {
it("should replace properly", () => {
const imageReplacer = new ImageReplacer();

View File

@ -8,16 +8,17 @@ import { Compiler } from "./next-compiler";
describe("Compiler", () => {
let compiler: Compiler;
let file: File;
beforeEach(() => {
file = new File();
compiler = new Compiler();
});
describe("#compile()", () => {
it("should pack all the content", async function () {
this.timeout(99999999);
const file = new File({
sections: [],
});
const zipFile = compiler.compile(file);
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
@ -38,7 +39,9 @@ describe("Compiler", () => {
});
it("should pack all additional headers and footers", async function () {
file.addSection({
const file = new File({
sections: [
{
headers: {
default: new Header(),
},
@ -46,9 +49,8 @@ describe("Compiler", () => {
default: new Footer(),
},
children: [],
});
file.addSection({
},
{
headers: {
default: new Header(),
},
@ -56,6 +58,8 @@ describe("Compiler", () => {
default: new Footer(),
},
children: [],
},
],
});
this.timeout(99999999);
@ -80,12 +84,15 @@ describe("Compiler", () => {
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
const paragraph = new Paragraph("");
const doc = new File();
doc.addSection({
const file = new File({
sections: [
{
properties: {},
children: [paragraph],
},
],
});
// tslint:disable-next-line: no-string-literal
const spy = sinon.spy(compiler["formatter"], "format");

View File

@ -14,9 +14,8 @@ describe("Packer", () => {
creator: "Dolan Miu",
revision: "1",
lastModifiedBy: "Dolan Miu",
});
file.addSection({
sections: [
{
children: [
new Paragraph({
text: "title",
@ -32,6 +31,8 @@ describe("Packer", () => {
}),
new Paragraph("test text"),
],
},
],
});
});

View File

@ -3,12 +3,14 @@ import { ICustomPropertyOptions } from "../custom-properties";
import { IDocumentBackgroundOptions } from "../document";
import { DocumentAttributes } from "../document/document-attributes";
import { ISectionOptions } from "../file";
import { INumberingOptions } from "../numbering";
import { Paragraph } from "../paragraph";
import { IStylesOptions } from "../styles";
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
export interface IPropertiesOptions {
readonly sections: ISectionOptions[];
readonly title?: string;
readonly subject?: string;
readonly creator?: string;
@ -34,7 +36,7 @@ export interface IPropertiesOptions {
}
export class CoreProperties extends XmlComponent {
constructor(options: IPropertiesOptions) {
constructor(options: Omit<IPropertiesOptions, "sections">) {
super("cp:coreProperties");
this.root.push(
new DocumentAttributes({

View File

@ -14,8 +14,12 @@ describe("Body", () => {
describe("#addSection", () => {
it("should add section with default parameters", () => {
body.addSection({
page: {
size: {
width: 10000,
height: 10000,
},
},
});
const tree = new Formatter().format(body);

View File

@ -1,7 +1,7 @@
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties";
export class Body extends XmlComponent {
private readonly sections: SectionProperties[] = [];
@ -18,7 +18,7 @@ export class Body extends XmlComponent {
* - last section should be direct child of body
* @param options new section options
*/
public addSection(options: SectionPropertiesOptions): void {
public addSection(options: ISectionPropertiesOptions): void {
const currentSection = this.sections.pop() as SectionProperties;
this.root.push(this.createSectionParagraph(currentSection));

View File

@ -8,18 +8,18 @@ export enum LineNumberRestartFormat {
}
export interface ILineNumberAttributes {
readonly lineNumberCountBy?: number;
readonly lineNumberStart?: number;
readonly lineNumberRestart?: LineNumberRestartFormat;
readonly lineNumberDistance?: number;
readonly countBy?: number;
readonly start?: number;
readonly restart?: LineNumberRestartFormat;
readonly distance?: number;
}
export class LineNumberAttributes extends XmlAttributeComponent<ILineNumberAttributes> {
protected readonly xmlKeys = {
lineNumberCountBy: "w:countBy",
lineNumberStart: "w:start",
lineNumberRestart: "w:restart",
lineNumberDistance: "w:distance",
countBy: "w:countBy",
start: "w:start",
restart: "w:restart",
distance: "w:distance",
};
}
@ -28,10 +28,10 @@ export class LineNumberType extends XmlComponent {
super("w:lnNumType");
this.root.push(
new LineNumberAttributes({
lineNumberCountBy: countBy,
lineNumberStart: start,
lineNumberRestart: restart,
lineNumberDistance: dist,
countBy: countBy,
start: start,
restart: restart,
distance: dist,
}),
);
}

View File

@ -26,16 +26,16 @@ export enum PageNumberSeparator {
}
export interface IPageNumberTypeAttributes {
readonly pageNumberStart?: number;
readonly pageNumberFormatType?: PageNumberFormat;
readonly pageNumberSeparator?: PageNumberSeparator;
readonly start?: number;
readonly formatType?: PageNumberFormat;
readonly separator?: PageNumberSeparator;
}
export class PageNumberTypeAttributes extends XmlAttributeComponent<IPageNumberTypeAttributes> {
protected readonly xmlKeys = {
pageNumberStart: "w:start",
pageNumberFormatType: "w:fmt",
pageNumberSeparator: "w:chapSep",
start: "w:start",
formatType: "w:fmt",
separator: "w:chapSep",
};
}
@ -44,9 +44,9 @@ export class PageNumberType extends XmlComponent {
super("w:pgNumType");
this.root.push(
new PageNumberTypeAttributes({
pageNumberStart: start,
pageNumberFormatType: numberFormat,
pageNumberSeparator: separator,
start: start,
formatType: numberFormat,
separator: separator,
}),
);
}

View File

@ -19,8 +19,12 @@ describe("SectionProperties", () => {
const media = new Media();
const properties = new SectionProperties({
page: {
size: {
width: 11906,
height: 16838,
},
margin: {
top: convertInchesToTwip(1),
right: convertInchesToTwip(1),
bottom: convertInchesToTwip(1),
@ -29,24 +33,32 @@ describe("SectionProperties", () => {
footer: 708,
gutter: 0,
mirror: false,
},
pageNumbers: {
start: 10,
formatType: PageNumberFormat.CARDINAL_TEXT,
},
},
column: {
space: 708,
count: 1,
separate: true,
},
grid: {
linePitch: convertInchesToTwip(0.25),
headers: {
},
headerWrapperGroup: {
default: new HeaderWrapper(media, 100),
},
footers: {
footerWrapperGroup: {
even: new FooterWrapper(media, 200),
},
pageNumberStart: 10,
pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT,
titlePage: true,
verticalAlign: SectionVerticalAlignValue.TOP,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
@ -98,7 +110,11 @@ describe("SectionProperties", () => {
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
page: {
margin: {
top: 0,
},
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
@ -122,7 +138,11 @@ describe("SectionProperties", () => {
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
page: {
margin: {
bottom: 0,
},
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
@ -146,8 +166,12 @@ describe("SectionProperties", () => {
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
page: {
size: {
width: 0,
height: 0,
},
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
@ -171,9 +195,13 @@ describe("SectionProperties", () => {
it("should create section properties with page borders", () => {
const properties = new SectionProperties({
page: {
borders: {
pageBorders: {
offsetFrom: PageBorderOffsetFrom.PAGE,
},
},
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
@ -185,7 +213,11 @@ describe("SectionProperties", () => {
it("should create section properties with page number type, but without start attribute", () => {
const properties = new SectionProperties({
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
page: {
pageNumbers: {
formatType: PageNumberFormat.UPPER_ROMAN,
},
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
@ -217,10 +249,12 @@ describe("SectionProperties", () => {
it("should create section properties line number type", () => {
const properties = new SectionProperties({
lineNumberCountBy: 2,
lineNumberStart: 2,
lineNumberRestart: LineNumberRestartFormat.CONTINUOUS,
lineNumberDistance: 4,
lineNumbers: {
countBy: 2,
start: 2,
restart: LineNumberRestartFormat.CONTINUOUS,
distance: 4,
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);

View File

@ -1,4 +1,6 @@
// http://officeopenxml.com/WPsection.php
// tslint:disable: no-unnecessary-initializer
import { convertInchesToTwip } from "convenience-functions";
import { FooterWrapper } from "file/footer-wrapper";
import { HeaderWrapper } from "file/header-wrapper";
@ -21,7 +23,7 @@ import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attr
import { TitlePage } from "./title-page/title-page";
import { Type } from "./type/section-type";
import { SectionType } from "./type/section-type-attributes";
import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align";
import { SectionVerticalAlign, SectionVerticalAlignValue } from "./vertical-align";
export interface IHeaderFooterGroup<T> {
readonly default?: T;
@ -29,46 +31,31 @@ export interface IHeaderFooterGroup<T> {
readonly even?: T;
}
interface IHeadersOptions {
readonly headers?: IHeaderFooterGroup<HeaderWrapper>;
}
interface IFootersOptions {
readonly footers?: IHeaderFooterGroup<FooterWrapper>;
}
interface ITitlePageOptions {
export interface ISectionPropertiesOptions {
readonly page?: {
readonly size?: IPageSizeAttributes;
readonly margin?: IPageMarginAttributes;
readonly pageNumbers?: IPageNumberTypeAttributes;
readonly borders?: IPageBordersOptions;
};
readonly grid?: IDocGridAttributesProperties;
readonly headerWrapperGroup?: IHeaderFooterGroup<HeaderWrapper>;
readonly footerWrapperGroup?: IHeaderFooterGroup<FooterWrapper>;
readonly lineNumbers?: ILineNumberAttributes;
readonly titlePage?: boolean;
}
export type SectionPropertiesOptions = IPageSizeAttributes &
IPageMarginAttributes &
IDocGridAttributesProperties &
IHeadersOptions &
IFootersOptions &
IPageNumberTypeAttributes &
ILineNumberAttributes &
IPageBordersOptions &
ITitlePageOptions &
ISectionVerticalAlignAttributes & {
readonly verticalAlign?: SectionVerticalAlignValue;
readonly column?: {
readonly space?: number;
readonly count?: number;
readonly separate?: boolean;
};
readonly type?: SectionType;
};
// Need to decouple this from the attributes
}
export class SectionProperties extends XmlComponent {
public readonly width: number;
public readonly rightMargin: number;
public readonly leftMargin: number;
constructor(
{
width = 11906,
height = 16838,
constructor({
page: {
size: { width = 11906, height = 16838, orientation = PageOrientation.PORTRAIT } = {},
margin: {
top = convertInchesToTwip(1),
right = convertInchesToTwip(1),
bottom = convertInchesToTwip(1),
@ -77,41 +64,38 @@ export class SectionProperties extends XmlComponent {
footer = 708,
gutter = 0,
mirror = false,
column = {},
linePitch = 360,
orientation = PageOrientation.PORTRAIT,
headers,
footers,
pageNumberFormatType,
pageNumberStart,
pageNumberSeparator,
lineNumberCountBy,
lineNumberStart,
lineNumberRestart,
lineNumberDistance,
pageBorders,
pageBorderTop,
pageBorderRight,
pageBorderBottom,
pageBorderLeft,
} = {},
pageNumbers: {
start: pageNumberStart = undefined,
formatType: pageNumberFormatType = undefined,
separator: pageNumberSeparator = undefined,
} = {},
borders: {
pageBorders = undefined,
pageBorderTop = undefined,
pageBorderRight = undefined,
pageBorderBottom = undefined,
pageBorderLeft = undefined,
} = {},
} = {},
grid: { linePitch = 360 } = {},
headerWrapperGroup = {},
footerWrapperGroup = {},
lineNumbers: { countBy: lineNumberCountBy, start: lineNumberStart, restart: lineNumberRestart, distance: lineNumberDistance } = {},
titlePage = false,
verticalAlign,
column: { space = 708, count = 1, separate = false } = {},
type,
}: SectionPropertiesOptions = { column: {} },
) {
}: ISectionPropertiesOptions = {}) {
super("w:sectPr");
this.leftMargin = left;
this.rightMargin = right;
this.width = width;
this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1, column.separate ?? false));
this.root.push(new Columns(space, count, separate));
this.root.push(new DocumentGrid(linePitch));
this.addHeaders(headers);
this.addFooters(footers);
this.addHeaders(headerWrapperGroup);
this.addFooters(footerWrapperGroup);
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType, pageNumberSeparator));
@ -144,8 +128,7 @@ export class SectionProperties extends XmlComponent {
}
}
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
if (headers) {
private addHeaders(headers: IHeaderFooterGroup<HeaderWrapper>): void {
if (headers.default) {
this.root.push(
new HeaderReference({
@ -173,10 +156,8 @@ export class SectionProperties extends XmlComponent {
);
}
}
}
private addFooters(footers?: IHeaderFooterGroup<FooterWrapper>): void {
if (footers) {
private addFooters(footers: IHeaderFooterGroup<FooterWrapper>): void {
if (footers.default) {
this.root.push(
new FooterReference({
@ -205,4 +186,3 @@ export class SectionProperties extends XmlComponent {
}
}
}
}

View File

@ -1,11 +1,9 @@
import { XmlAttributeComponent } from "file/xml-components";
import { SectionVerticalAlignValue } from "./vertical-align";
export interface ISectionVerticalAlignAttributes {
export class SectionVerticalAlignAttributes extends XmlAttributeComponent<{
readonly verticalAlign?: SectionVerticalAlignValue;
}
export class SectionVerticalAlignAttributes extends XmlAttributeComponent<ISectionVerticalAlignAttributes> {
}> {
protected readonly xmlKeys = {
verticalAlign: "w:val",
};

View File

@ -1,33 +1,17 @@
import { expect } from "chai";
import * as sinon from "sinon";
import { Formatter } from "export/formatter";
import { File } from "./file";
import { Footer, Header } from "./header";
import { Paragraph } from "./paragraph";
import { Table, TableCell, TableRow } from "./table";
import { TableOfContents } from "./table-of-contents";
describe("File", () => {
describe("#constructor", () => {
it("should create with correct headers and footers by default", () => {
const doc = new File();
doc.addSection({
children: [],
});
const tree = new Formatter().format(doc.Document.View.Body);
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
});
it("should create with correct headers and footers", () => {
const doc = new File();
doc.addSection({
const doc = new File({
sections: [
{
headers: {
default: new Header(),
},
@ -35,6 +19,8 @@ describe("File", () => {
default: new Footer(),
},
children: [],
},
],
});
const tree = new Formatter().format(doc.Document.View.Body);
@ -44,9 +30,9 @@ describe("File", () => {
});
it("should create with first headers and footers", () => {
const doc = new File();
doc.addSection({
const doc = new File({
sections: [
{
headers: {
first: new Header(),
},
@ -54,6 +40,8 @@ describe("File", () => {
first: new Footer(),
},
children: [],
},
],
});
const tree = new Formatter().format(doc.Document.View.Body);
@ -62,9 +50,9 @@ describe("File", () => {
});
it("should create with correct headers", () => {
const doc = new File();
doc.addSection({
const doc = new File({
sections: [
{
headers: {
default: new Header(),
first: new Header(),
@ -76,6 +64,8 @@ describe("File", () => {
even: new Footer(),
},
children: [],
},
],
});
const tree = new Formatter().format(doc.Document.View.Body);
@ -90,11 +80,13 @@ describe("File", () => {
});
it("should add child", () => {
const doc = new File(undefined, undefined, [
const doc = new File({
sections: [
{
children: [new Paragraph("test")],
},
]);
],
});
const tree = new Formatter().format(doc.Document.View.Body);
@ -171,69 +163,13 @@ describe("File", () => {
});
});
describe("#addSection", () => {
it("should call the underlying document's add a Paragraph", () => {
const file = new File();
const spy = sinon.spy(file.Document.View, "add");
file.addSection({
children: [new Paragraph({})],
});
expect(spy.called).to.equal(true);
});
it("should call the underlying document's add when adding a Table", () => {
const file = new File();
const spy = sinon.spy(file.Document.View, "add");
file.addSection({
children: [
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
}),
],
});
expect(spy.called).to.equal(true);
});
it("should call the underlying document's add when adding an Image (paragraph)", () => {
const file = new File();
const spy = sinon.spy(file.Document.View, "add");
// tslint:disable-next-line:no-any
file.addSection({
children: [new Paragraph("")],
});
expect(spy.called).to.equal(true);
});
});
describe("#addSection", () => {
it("should call the underlying document's add", () => {
const file = new File();
const spy = sinon.spy(file.Document.View, "add");
file.addSection({
children: [new TableOfContents()],
});
expect(spy.called).to.equal(true);
});
});
describe("#addTrackRevisionsFeature", () => {
it("should call the underlying document's add", () => {
const file = new File({
features: {
trackRevisions: true,
},
sections: [],
});
// tslint:disable-next-line: no-unused-expression no-string-literal
@ -249,6 +185,7 @@ describe("File", () => {
children: [new Paragraph("hello")],
},
},
sections: [],
});
const tree = new Formatter().format(wrapper.FootNotes.View);
@ -435,6 +372,7 @@ describe("File", () => {
styles: {
default: {},
},
sections: [],
});
const tree = new Formatter().format(doc.Styles);
@ -454,6 +392,7 @@ describe("File", () => {
it("should create with even and odd headers and footers", () => {
const doc = new File({
evenAndOddHeaderAndFooters: true,
sections: [],
});
const tree = new Formatter().format(doc.Settings);

View File

@ -3,13 +3,7 @@ import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
import { CustomProperties } from "./custom-properties";
import { DocumentWrapper } from "./document-wrapper";
import {
FooterReferenceType,
HeaderReferenceType,
IPageSizeAttributes,
SectionPropertiesOptions,
} from "./document/body/section-properties";
import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes";
import { FooterReferenceType, HeaderReferenceType, ISectionPropertiesOptions } from "./document/body/section-properties";
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootnotesWrapper } from "./footnotes-wrapper";
@ -37,9 +31,7 @@ export interface ISectionOptions {
readonly first?: Footer;
readonly even?: Footer;
};
readonly size?: IPageSizeAttributes;
readonly margins?: IPageMarginAttributes;
readonly properties?: SectionPropertiesOptions;
readonly properties?: ISectionPropertiesOptions;
readonly children: (Paragraph | Table | TableOfContents)[];
}
@ -61,16 +53,14 @@ export class File {
private readonly appProperties: AppProperties;
private readonly styles: Styles;
constructor(
options: IPropertiesOptions = {
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
},
fileProperties: IFileProperties = {},
sections: ISectionOptions[] = [],
) {
this.coreProperties = new CoreProperties(options);
constructor(options: IPropertiesOptions, fileProperties: IFileProperties = {}) {
this.coreProperties = new CoreProperties({
...options,
creator: options.creator ?? "Un-named",
revision: options.revision ?? "1",
lastModifiedBy: options.lastModifiedBy ?? "Un-named",
});
this.numbering = new Numbering(
options.numbering
? options.numbering
@ -78,6 +68,7 @@ export class File {
config: [],
},
);
this.fileRelationships = new Relationships();
this.customProperties = new CustomProperties(options.customProperties ?? []);
this.appProperties = new AppProperties();
@ -131,12 +122,8 @@ export class File {
}
}
for (const section of sections) {
this.documentWrapper.View.Body.addSection(section.properties ? section.properties : {});
for (const child of section.children) {
this.documentWrapper.View.add(child);
}
for (const section of options.sections) {
this.addSection(section);
}
if (options.footnotes) {
@ -153,28 +140,25 @@ export class File {
}
}
public addSection({
headers = { default: new Header() },
footers = { default: new Header() },
margins = {},
size = {},
properties,
children,
}: ISectionOptions): void {
public verifyUpdateFields(): void {
if (this.documentWrapper.View.getTablesOfContents().length) {
this.settings.addUpdateFields();
}
}
private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void {
this.documentWrapper.View.Body.addSection({
...properties,
headers: {
headerWrapperGroup: {
default: headers.default ? this.createHeader(headers.default) : undefined,
first: headers.first ? this.createHeader(headers.first) : undefined,
even: headers.even ? this.createHeader(headers.even) : undefined,
},
footers: {
footerWrapperGroup: {
default: footers.default ? this.createFooter(footers.default) : undefined,
first: footers.first ? this.createFooter(footers.first) : undefined,
even: footers.even ? this.createFooter(footers.even) : undefined,
},
...margins,
...size,
});
for (const child of children) {
@ -182,12 +166,6 @@ export class File {
}
}
public verifyUpdateFields(): void {
if (this.documentWrapper.View.getTablesOfContents().length) {
this.settings.addUpdateFields();
}
}
private createHeader(header: Header): HeaderWrapper {
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);

View File

@ -6,7 +6,11 @@ describe("Index", () => {
describe("Document", () => {
it("should instantiate the Document", () => {
// tslint:disable-next-line: no-unused-expression
expect(new Document()).to.be.ok;
expect(
new Document({
sections: [],
}),
).to.be.ok;
});
});
});