add ability to vertically align content of section

This commit is contained in:
alexbogomolov
2019-10-29 12:55:15 +02:00
parent 43ddeec6e1
commit 2276572902
6 changed files with 72 additions and 1 deletions

31
demo/48-vertical-align.ts Normal file
View File

@ -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);
});

View File

@ -5,3 +5,4 @@ export * from "./page-size";
export * from "./page-number";
export * from "./page-border";
export * from "./line-number";
export * from "./vertical-align";

View File

@ -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<T> {
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<HeaderWrapper>): void {

View File

@ -0,0 +1,2 @@
export * from "./vertical-align";
export * from "./vertical-align-attributes";

View File

@ -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<ISectionVerticalAlignAttributes> {
protected readonly xmlKeys = {
valign: "w:val",
};
}

View File

@ -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 }));
}
}