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

176 lines
5.7 KiB
TypeScript
Raw Normal View History

2018-01-24 00:01:38 +00:00
// http://officeopenxml.com/WPsection.php
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";
2018-01-29 02:56:35 +00:00
import { IColumnsAttributes } from "./columns/columns-attributes";
2018-01-24 00:01:38 +00:00
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";
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";
2018-10-26 01:04:07 +01:00
import { IPageNumberTypeAttributes, PageNumberFormat, 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";
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
}
2018-09-07 21:48:59 +01:00
interface IHeadersOptions {
readonly headers?: IHeaderFooterGroup<HeaderWrapper>;
2018-09-07 21:48:59 +01:00
}
interface IFootersOptions {
readonly footers?: IHeaderFooterGroup<FooterWrapper>;
2018-10-05 01:33:17 +01:00
}
interface ITitlePageOptions {
readonly titlePage?: boolean;
2018-09-07 21:48:59 +01:00
}
2018-01-24 00:01:38 +00:00
export type SectionPropertiesOptions = IPageSizeAttributes &
IPageMarginAttributes &
IColumnsAttributes &
IDocGridAttributesProperties &
2018-09-04 17:16:31 +03:00
IHeadersOptions &
IFootersOptions &
IPageNumberTypeAttributes &
2018-10-05 01:33:17 +01:00
IPageBordersOptions &
ITitlePageOptions;
2018-01-24 00:01:38 +00:00
export class SectionProperties extends XmlComponent {
2018-08-07 01:38:15 +01:00
private readonly options: SectionPropertiesOptions;
2018-10-05 01:33:17 +01:00
constructor(options: SectionPropertiesOptions = {}) {
2018-01-24 00:01:38 +00:00
super("w:sectPr");
2018-10-05 01:33:17 +01:00
const {
width = 11906,
height = 16838,
top = 1440,
right = 1440,
bottom = 1440,
left = 1440,
header = 708,
footer = 708,
gutter = 0,
mirror = false,
space = 708,
linePitch = 360,
orientation = PageOrientation.PORTRAIT,
headers,
footers,
pageNumberFormatType = PageNumberFormat.DECIMAL,
pageNumberStart,
pageBorders,
pageBorderTop,
pageBorderRight,
pageBorderBottom,
pageBorderLeft,
titlePage = false,
} = options;
2018-10-05 01:33:17 +01:00
this.options = options;
this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(space));
this.root.push(new DocumentGrid(linePitch));
this.addHeaders(headers);
this.addFooters(footers);
2018-09-07 21:48:59 +01:00
2018-10-05 01:33:17 +01:00
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
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());
}
2018-10-05 01:33:17 +01:00
}
2018-09-04 17:16:31 +03:00
2018-10-05 01:33:17 +01:00
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
if (headers) {
if (headers.default) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.DEFAULT,
headerId: headers.default.Header.ReferenceId,
}),
);
}
if (headers.first) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headers.first.Header.ReferenceId,
}),
);
}
if (headers.even) {
this.root.push(
new HeaderReference({
headerType: HeaderReferenceType.EVEN,
headerId: headers.even.Header.ReferenceId,
}),
);
}
}
}
private addFooters(footers?: IHeaderFooterGroup<FooterWrapper>): void {
if (footers) {
if (footers.default) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.DEFAULT,
footerId: footers.default.Footer.ReferenceId,
}),
);
}
if (footers.first) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footers.first.Footer.ReferenceId,
}),
);
}
if (footers.even) {
this.root.push(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footers.even.Footer.ReferenceId,
}),
);
}
}
}
2018-08-03 02:29:58 +01:00
public get Options(): SectionPropertiesOptions {
return this.options;
2018-01-24 00:01:38 +00:00
}
}