Section WIP

This commit is contained in:
Dolan
2019-07-07 03:54:29 +01:00
parent f33d6da65a
commit 58346a8937
3 changed files with 66 additions and 43 deletions

View File

@ -15,6 +15,7 @@ const footer = doc.createFooter();
footer.add(new Paragraph("Footer on another page")); footer.add(new Paragraph("Footer on another page"));
doc.addSection({ doc.addSection({
properties: {
headers: { headers: {
default: header, default: header,
}, },
@ -23,11 +24,12 @@ doc.addSection({
}, },
pageNumberStart: 1, pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL, pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [new Paragraph("hello")],
}); });
doc.add(new Paragraph("hello"));
doc.addSection({ doc.addSection({
properties: {
headers: { headers: {
default: header, default: header,
}, },
@ -37,46 +39,53 @@ doc.addSection({
pageNumberStart: 1, pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL, pageNumberFormatType: PageNumberFormat.DECIMAL,
orientation: PageOrientation.LANDSCAPE, orientation: PageOrientation.LANDSCAPE,
},
children: [new Paragraph("hello in landscape")],
}); });
doc.add(new Paragraph("hello in landscape"));
const header2 = doc.createHeader(); const header2 = doc.createHeader();
const pageNumber = new TextRun("Page number: ").pageNumber(); const pageNumber = new TextRun("Page number: ").pageNumber();
header2.add(new Paragraph({}).addRun(pageNumber)); header2.add(new Paragraph({}).addRun(pageNumber));
doc.addSection({ doc.addSection({
properties: {
headers: { headers: {
default: header2, default: header2,
}, },
orientation: PageOrientation.PORTRAIT, orientation: PageOrientation.PORTRAIT,
},
children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")],
}); });
doc.add(new Paragraph("Page number in the header must be 2, because it continues from the previous section."));
doc.addSection({ doc.addSection({
properties: {
headers: { headers: {
default: header2, default: header2,
}, },
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
orientation: PageOrientation.PORTRAIT, orientation: PageOrientation.PORTRAIT,
},
children: [
new Paragraph(
"Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.",
),
],
}); });
doc.add(new Paragraph(
"Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.",
));
doc.addSection({ doc.addSection({
properties: {
headers: { headers: {
default: header2, default: header2,
}, },
pageNumberFormatType: PageNumberFormat.DECIMAL, pageNumberFormatType: PageNumberFormat.DECIMAL,
pageNumberStart: 25, pageNumberStart: 25,
orientation: PageOrientation.PORTRAIT, orientation: PageOrientation.PORTRAIT,
},
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."),
],
}); });
doc.add(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."));
const packer = new Packer(); const packer = new Packer();
packer.toBuffer(doc).then((buffer) => { packer.toBuffer(doc).then((buffer) => {

View File

@ -15,7 +15,7 @@ describe("File", () => {
const header = doc.createHeader(); const header = doc.createHeader();
const footer = doc.createFooter(); const footer = doc.createFooter();
doc.addSection({ doc.addSectionOld({
headers: { headers: {
default: header, default: header,
}, },
@ -35,7 +35,7 @@ describe("File", () => {
const header = doc.createHeader(); const header = doc.createHeader();
const footer = doc.createFooter(); const footer = doc.createFooter();
doc.addSection({ doc.addSectionOld({
headers: { headers: {
first: header, first: header,
}, },
@ -55,7 +55,7 @@ describe("File", () => {
const header = doc.createHeader(); const header = doc.createHeader();
const footer = doc.createFooter(); const footer = doc.createFooter();
doc.addSection({ doc.addSectionOld({
headers: { headers: {
default: header, default: header,
first: header, first: header,

View File

@ -27,6 +27,11 @@ import { DefaultStylesFactory } from "./styles/factory";
import { Table } from "./table"; import { Table } from "./table";
import { TableOfContents } from "./table-of-contents"; import { TableOfContents } from "./table-of-contents";
export interface ISectionOptions {
readonly properties: SectionPropertiesOptions;
readonly children: Array<Paragraph | Table | TableOfContents>;
}
export class File { export class File {
// tslint:disable-next-line:readonly-keyword // tslint:disable-next-line:readonly-keyword
private currentRelationshipId: number = 1; private currentRelationshipId: number = 1;
@ -54,6 +59,7 @@ export class File {
}, },
sectionPropertiesOptions: SectionPropertiesOptions = {}, sectionPropertiesOptions: SectionPropertiesOptions = {},
fileProperties: IFileProperties = {}, fileProperties: IFileProperties = {},
sections: ISectionOptions[] = [],
) { ) {
this.coreProperties = new CoreProperties(options); this.coreProperties = new CoreProperties(options);
this.numbering = new Numbering(); this.numbering = new Numbering();
@ -110,6 +116,14 @@ export class File {
this.document = new Document(newSectionPropertiesOptions); this.document = new Document(newSectionPropertiesOptions);
this.settings = new Settings(); this.settings = new Settings();
for (const section of sections) {
this.document.Body.addSection(section.properties);
for (const child of section.children) {
this.add(child);
}
}
} }
public add(item: Paragraph | Table | TableOfContents): File { public add(item: Paragraph | Table | TableOfContents): File {
@ -167,7 +181,7 @@ export class File {
return bookmark; return bookmark;
} }
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void { public addSectionOld(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions); this.document.Body.addSection(sectionPropertiesOptions);
} }