diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts new file mode 100644 index 0000000000..c22e6da45a --- /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: { + valign: 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.ts b/src/file/document/body/section-properties/section-properties.ts index 072f68acca..b4732dea32 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, + valign, } = options; this.options = options; @@ -121,6 +124,10 @@ export class SectionProperties extends XmlComponent { if (titlePage) { this.root.push(new TitlePage()); } + + if (valign) { + this.root.push(new SectionVerticalAlign(valign)); + } } 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..3e7af94460 --- /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 valign?: SectionVerticalAlignValue; +} + +export class SectionVerticalAlignAttributes extends XmlAttributeComponent { + protected readonly xmlKeys = { + valign: "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..bde6408ddd --- /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({ valign: value })); + } +}