add ability to vertically align content of section
This commit is contained in:
31
demo/48-vertical-align.ts
Normal file
31
demo/48-vertical-align.ts
Normal 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);
|
||||||
|
});
|
@ -5,3 +5,4 @@ export * from "./page-size";
|
|||||||
export * from "./page-number";
|
export * from "./page-number";
|
||||||
export * from "./page-border";
|
export * from "./page-border";
|
||||||
export * from "./line-number";
|
export * from "./line-number";
|
||||||
|
export * from "./vertical-align";
|
||||||
|
@ -18,6 +18,7 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
|
|||||||
import { PageSize } from "./page-size/page-size";
|
import { PageSize } from "./page-size/page-size";
|
||||||
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
||||||
import { TitlePage } from "./title-page/title-page";
|
import { TitlePage } from "./title-page/title-page";
|
||||||
|
import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align";
|
||||||
|
|
||||||
export interface IHeaderFooterGroup<T> {
|
export interface IHeaderFooterGroup<T> {
|
||||||
readonly default?: T;
|
readonly default?: T;
|
||||||
@ -45,7 +46,8 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
|
|||||||
IPageNumberTypeAttributes &
|
IPageNumberTypeAttributes &
|
||||||
ILineNumberAttributes &
|
ILineNumberAttributes &
|
||||||
IPageBordersOptions &
|
IPageBordersOptions &
|
||||||
ITitlePageOptions & {
|
ITitlePageOptions &
|
||||||
|
ISectionVerticalAlignAttributes & {
|
||||||
readonly column?: {
|
readonly column?: {
|
||||||
readonly space?: number;
|
readonly space?: number;
|
||||||
readonly count?: number;
|
readonly count?: number;
|
||||||
@ -87,6 +89,7 @@ export class SectionProperties extends XmlComponent {
|
|||||||
pageBorderBottom,
|
pageBorderBottom,
|
||||||
pageBorderLeft,
|
pageBorderLeft,
|
||||||
titlePage = false,
|
titlePage = false,
|
||||||
|
valign,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
@ -121,6 +124,10 @@ export class SectionProperties extends XmlComponent {
|
|||||||
if (titlePage) {
|
if (titlePage) {
|
||||||
this.root.push(new TitlePage());
|
this.root.push(new TitlePage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (valign) {
|
||||||
|
this.root.push(new SectionVerticalAlign(valign));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
|
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./vertical-align";
|
||||||
|
export * from "./vertical-align-attributes";
|
@ -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",
|
||||||
|
};
|
||||||
|
}
|
@ -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 }));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user