Files
docx-js/src/file/document/body/section-properties/section-properties.ts

189 lines
6.9 KiB
TypeScript
Raw Normal View History

2018-01-24 00:01:38 +00:00
// http://officeopenxml.com/WPsection.php
2021-03-19 20:53:56 +00:00
// tslint:disable: no-unnecessary-initializer
2020-12-24 03:37:43 +00:00
import { convertInchesToTwip } from "convenience-functions";
2018-10-26 01:04:07 +01:00
import { FooterWrapper } from "file/footer-wrapper";
import { HeaderWrapper } from "file/header-wrapper";
2018-01-24 00:01:38 +00:00
import { XmlComponent } from "file/xml-components";
2018-10-26 01:04:07 +01:00
2018-01-24 00:01:38 +00:00
import { Columns } from "./columns/columns";
import { DocumentGrid } from "./doc-grid/doc-grid";
import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes";
2018-10-05 01:33:17 +01:00
import { FooterReferenceType } from "./footer-reference";
import { FooterReference } from "./footer-reference/footer-reference";
import { HeaderReferenceType } from "./header-reference";
import { HeaderReference } from "./header-reference/header-reference";
import { ILineNumberAttributes, LineNumberType } from "./line-number";
2018-10-26 01:04:07 +01:00
import { IPageBordersOptions, PageBorders } from "./page-border";
2018-01-24 00:01:38 +00:00
import { PageMargin } from "./page-margin/page-margin";
2018-01-29 02:56:35 +00:00
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
2018-01-24 00:01:38 +00:00
import { PageSize } from "./page-size/page-size";
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
2018-09-04 17:16:31 +03:00
import { TitlePage } from "./title-page/title-page";
2021-02-22 21:04:02 +00:00
import { Type } from "./type/section-type";
import { SectionType } from "./type/section-type-attributes";
2021-03-19 20:53:56 +00:00
import { SectionVerticalAlign, SectionVerticalAlignValue } from "./vertical-align";
2018-09-04 17:16:31 +03:00
2018-10-05 01:33:17 +01:00
export interface IHeaderFooterGroup<T> {
readonly default?: T;
readonly first?: T;
readonly even?: T;
2018-10-05 01:33:17 +01:00
}
2021-03-19 20:53:56 +00:00
export interface ISectionPropertiesOptions {
readonly page?: {
readonly size?: IPageSizeAttributes;
readonly margin?: IPageMarginAttributes;
readonly pageNumbers?: IPageNumberTypeAttributes;
readonly borders?: IPageBordersOptions;
};
readonly grid?: IDocGridAttributesProperties;
readonly headerWrapperGroup?: IHeaderFooterGroup<HeaderWrapper>;
readonly footerWrapperGroup?: IHeaderFooterGroup<FooterWrapper>;
readonly lineNumbers?: ILineNumberAttributes;
readonly titlePage?: boolean;
2021-03-19 20:53:56 +00:00
readonly verticalAlign?: SectionVerticalAlignValue;
readonly column?: {
readonly space?: number;
readonly count?: number;
readonly separate?: boolean;
2019-06-28 01:57:43 +01:00
};
2021-03-19 20:53:56 +00:00
readonly type?: SectionType;
}
2018-01-24 00:01:38 +00:00
export class SectionProperties extends XmlComponent {
2021-03-19 20:53:56 +00:00
constructor({
page: {
size: { width = 11906, height = 16838, orientation = PageOrientation.PORTRAIT } = {},
margin: {
top = convertInchesToTwip(1),
right = convertInchesToTwip(1),
bottom = convertInchesToTwip(1),
left = convertInchesToTwip(1),
header = 708,
footer = 708,
gutter = 0,
mirror = false,
} = {},
pageNumbers: {
start: pageNumberStart = undefined,
formatType: pageNumberFormatType = undefined,
separator: pageNumberSeparator = undefined,
} = {},
borders: {
pageBorders = undefined,
pageBorderTop = undefined,
pageBorderRight = undefined,
pageBorderBottom = undefined,
pageBorderLeft = undefined,
} = {},
} = {},
grid: { linePitch = 360 } = {},
headerWrapperGroup = {},
footerWrapperGroup = {},
lineNumbers: { countBy: lineNumberCountBy, start: lineNumberStart, restart: lineNumberRestart, distance: lineNumberDistance } = {},
titlePage = false,
verticalAlign,
column: { space = 708, count = 1, separate = false } = {},
type,
}: ISectionPropertiesOptions = {}) {
super("w:sectPr");
2018-10-05 01:33:17 +01:00
this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
2021-03-19 20:53:56 +00:00
this.root.push(new Columns(space, count, separate));
2018-10-05 01:33:17 +01:00
this.root.push(new DocumentGrid(linePitch));
2021-03-19 20:53:56 +00:00
this.addHeaders(headerWrapperGroup);
this.addFooters(footerWrapperGroup);
2018-09-07 21:48:59 +01:00
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType, pageNumberSeparator));
2019-02-04 18:49:12 +05:30
if (lineNumberCountBy || lineNumberStart || lineNumberRestart || lineNumberDistance) {
this.root.push(new LineNumberType(lineNumberCountBy, lineNumberStart, lineNumberRestart, lineNumberDistance));
}
2018-10-05 01:33:17 +01:00
if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) {
this.root.push(
new PageBorders({
2018-10-05 01:33:17 +01:00
pageBorders: pageBorders,
pageBorderTop: pageBorderTop,
pageBorderRight: pageBorderRight,
pageBorderBottom: pageBorderBottom,
pageBorderLeft: pageBorderLeft,
}),
);
}
2018-10-05 01:33:17 +01:00
if (titlePage) {
2018-09-04 17:16:31 +03:00
this.root.push(new TitlePage());
}
2019-10-31 13:54:53 +02:00
if (verticalAlign) {
this.root.push(new SectionVerticalAlign(verticalAlign));
}
2021-02-22 21:04:02 +00:00
if (type) {
this.root.push(new Type(type));
}
2018-10-05 01:33:17 +01:00
}
2018-09-04 17:16:31 +03:00
2021-03-19 20:53:56 +00:00
private addHeaders(headers: IHeaderFooterGroup<HeaderWrapper>): void {
if (headers.default) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.DEFAULT,
headerId: headers.default.View.ReferenceId,
}),
);
}
if (headers.first) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headers.first.View.ReferenceId,
}),
);
}
if (headers.even) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.EVEN,
headerId: headers.even.View.ReferenceId,
}),
);
2018-10-05 01:33:17 +01:00
}
}
2021-03-19 20:53:56 +00:00
private addFooters(footers: IHeaderFooterGroup<FooterWrapper>): void {
if (footers.default) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.DEFAULT,
footerId: footers.default.View.ReferenceId,
}),
);
}
if (footers.first) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footers.first.View.ReferenceId,
}),
);
}
if (footers.even) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footers.even.View.ReferenceId,
}),
);
2018-10-05 01:33:17 +01:00
}
}
2018-01-24 00:01:38 +00:00
}