diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts new file mode 100644 index 0000000000..a9eb7986da --- /dev/null +++ b/demo/48-vertical-align.ts @@ -0,0 +1,31 @@ +// Example of making content of section vertically aligned +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, SectionVerticalAlignValue, TextRun } from "../build"; + +const doc = new Document(); + +doc.addSection({ + properties: { + verticalAlign: SectionVerticalAlignValue.CENTER, + }, + children: [ + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "Github is the best", + bold: true, + }).tab(), + ], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/document/body/section-properties/index.ts b/src/file/document/body/section-properties/index.ts index a8985b1249..47e56ec172 100644 --- a/src/file/document/body/section-properties/index.ts +++ b/src/file/document/body/section-properties/index.ts @@ -5,3 +5,4 @@ export * from "./page-size"; export * from "./page-number"; export * from "./page-border"; export * from "./line-number"; +export * from "./vertical-align"; diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index abdbf9e1ed..70fc567b83 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -8,6 +8,7 @@ import { Media } from "file/media"; import { PageBorderOffsetFrom } from "./page-border"; import { PageNumberFormat } from "./page-number"; import { SectionProperties } from "./section-properties"; +import { SectionVerticalAlignValue } from "./vertical-align"; describe("SectionProperties", () => { describe("#constructor()", () => { @@ -39,6 +40,7 @@ describe("SectionProperties", () => { 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"]); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 072f68acca..37e09e4498 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -18,6 +18,7 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./page-number"; import { PageSize } from "./page-size/page-size"; import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes"; import { TitlePage } from "./title-page/title-page"; +import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align"; export interface IHeaderFooterGroup { readonly default?: T; @@ -45,7 +46,8 @@ export type SectionPropertiesOptions = IPageSizeAttributes & IPageNumberTypeAttributes & ILineNumberAttributes & IPageBordersOptions & - ITitlePageOptions & { + ITitlePageOptions & + ISectionVerticalAlignAttributes & { readonly column?: { readonly space?: number; readonly count?: number; @@ -87,6 +89,7 @@ export class SectionProperties extends XmlComponent { pageBorderBottom, pageBorderLeft, titlePage = false, + verticalAlign, } = options; this.options = options; @@ -121,6 +124,10 @@ export class SectionProperties extends XmlComponent { if (titlePage) { this.root.push(new TitlePage()); } + + if (verticalAlign) { + this.root.push(new SectionVerticalAlign(verticalAlign)); + } } private addHeaders(headers?: IHeaderFooterGroup): void { diff --git a/src/file/document/body/section-properties/vertical-align/index.ts b/src/file/document/body/section-properties/vertical-align/index.ts new file mode 100644 index 0000000000..1f3fb76bb2 --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/index.ts @@ -0,0 +1,2 @@ +export * from "./vertical-align"; +export * from "./vertical-align-attributes"; diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts new file mode 100644 index 0000000000..fa09712a00 --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts @@ -0,0 +1,12 @@ +import { XmlAttributeComponent } from "file/xml-components"; +import { SectionVerticalAlignValue } from "./vertical-align"; + +export interface ISectionVerticalAlignAttributes { + readonly verticalAlign?: SectionVerticalAlignValue; +} + +export class SectionVerticalAlignAttributes extends XmlAttributeComponent { + protected readonly xmlKeys = { + verticalAlign: "w:val", + }; +} diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align.ts b/src/file/document/body/section-properties/vertical-align/vertical-align.ts new file mode 100644 index 0000000000..b025059d16 --- /dev/null +++ b/src/file/document/body/section-properties/vertical-align/vertical-align.ts @@ -0,0 +1,18 @@ +// http://officeopenxml.com/WPsection.php + +import { XmlComponent } from "file/xml-components"; +import { SectionVerticalAlignAttributes } from "./vertical-align-attributes"; + +export enum SectionVerticalAlignValue { + BOTH = "both", + BOTTOM = "bottom", + CENTER = "center", + TOP = "top", +} + +export class SectionVerticalAlign extends XmlComponent { + constructor(value: SectionVerticalAlignValue) { + super("w:vAlign"); + this.root.push(new SectionVerticalAlignAttributes({ verticalAlign: value })); + } +}