2018-02-08 00:12:59 +00:00
|
|
|
import { AppProperties } from "./app-properties/app-properties";
|
2018-02-03 20:56:20 +00:00
|
|
|
import { ContentTypes } from "./content-types/content-types";
|
2018-02-04 01:43:03 +00:00
|
|
|
import { CoreProperties, IPropertiesOptions } from "./core-properties";
|
2017-12-15 01:16:04 +00:00
|
|
|
import { Document } from "./document";
|
2018-09-04 17:16:31 +03:00
|
|
|
import {
|
|
|
|
FooterReferenceType,
|
|
|
|
HeaderReference,
|
|
|
|
HeaderReferenceType,
|
|
|
|
IFooterOptions,
|
2018-09-06 09:09:36 +01:00
|
|
|
IHeaderOptions,
|
|
|
|
SectionPropertiesOptions,
|
2018-09-04 17:16:31 +03:00
|
|
|
} from "./document/body/section-properties";
|
2018-01-31 00:31:54 +00:00
|
|
|
import { FooterWrapper } from "./footer-wrapper";
|
2018-06-03 02:11:21 +01:00
|
|
|
import { FootNotes } from "./footnotes";
|
2018-06-25 22:13:12 +01:00
|
|
|
import { HeaderWrapper } from "./header-wrapper";
|
2018-08-09 01:55:50 +01:00
|
|
|
import { Image, Media } from "./media";
|
2017-12-20 00:01:23 +00:00
|
|
|
import { Numbering } from "./numbering";
|
2018-08-09 01:55:50 +01:00
|
|
|
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
|
2018-01-29 01:54:10 +00:00
|
|
|
import { Relationships } from "./relationships";
|
2017-12-20 00:52:41 +00:00
|
|
|
import { Styles } from "./styles";
|
2018-03-28 14:54:07 +02:00
|
|
|
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
2018-05-06 02:58:16 +01:00
|
|
|
import { DefaultStylesFactory } from "./styles/factory";
|
2017-12-20 00:01:23 +00:00
|
|
|
import { Table } from "./table";
|
2018-09-04 17:16:31 +03:00
|
|
|
|
2018-09-06 09:09:36 +01:00
|
|
|
type DocumentHeaders = Array<{ header: HeaderWrapper; type: HeaderReferenceType }>;
|
|
|
|
type DocumentFooters = Array<{ footer: FooterWrapper; type: FooterReferenceType }>;
|
2017-12-15 01:16:04 +00:00
|
|
|
|
|
|
|
export class File {
|
2018-01-29 01:54:10 +00:00
|
|
|
private readonly document: Document;
|
|
|
|
private readonly styles: Styles;
|
2018-02-04 01:43:03 +00:00
|
|
|
private readonly coreProperties: CoreProperties;
|
2018-01-29 01:54:10 +00:00
|
|
|
private readonly numbering: Numbering;
|
|
|
|
private readonly media: Media;
|
2018-02-04 00:58:34 +00:00
|
|
|
private readonly docRelationships: Relationships;
|
|
|
|
private readonly fileRelationships: Relationships;
|
2018-09-04 17:16:31 +03:00
|
|
|
private readonly documentHeaders: DocumentHeaders = [];
|
|
|
|
private readonly documentFooters: DocumentFooters = [];
|
|
|
|
|
2018-06-03 02:11:21 +01:00
|
|
|
private readonly footNotes: FootNotes;
|
2018-05-12 20:04:54 -04:00
|
|
|
|
2018-02-03 20:56:20 +00:00
|
|
|
private readonly contentTypes: ContentTypes;
|
2018-02-08 00:12:59 +00:00
|
|
|
private readonly appProperties: AppProperties;
|
2017-12-15 01:16:04 +00:00
|
|
|
|
2018-08-02 02:09:00 +01:00
|
|
|
private currentRelationshipId: number = 1;
|
2017-12-15 02:15:44 +00:00
|
|
|
|
2018-06-21 12:03:34 +02:00
|
|
|
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
|
2017-12-15 02:15:44 +00:00
|
|
|
if (!options) {
|
|
|
|
options = {
|
|
|
|
creator: "Un-named",
|
|
|
|
revision: "1",
|
|
|
|
lastModifiedBy: "Un-named",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-04 17:16:31 +03:00
|
|
|
const templateDocument = options.templateDocument;
|
|
|
|
if (templateDocument) {
|
|
|
|
this.currentRelationshipId = templateDocument.currentRelationshipId + 1;
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:54:07 +02:00
|
|
|
if (options.externalStyles) {
|
|
|
|
const stylesFactory = new ExternalStylesFactory();
|
|
|
|
this.styles = stylesFactory.newInstance(options.externalStyles);
|
|
|
|
} else {
|
|
|
|
const stylesFactory = new DefaultStylesFactory();
|
|
|
|
this.styles = stylesFactory.newInstance();
|
|
|
|
}
|
|
|
|
|
2018-02-04 01:43:03 +00:00
|
|
|
this.coreProperties = new CoreProperties(options);
|
2017-12-15 01:16:04 +00:00
|
|
|
this.numbering = new Numbering();
|
2018-09-04 17:16:31 +03:00
|
|
|
|
2018-02-04 00:58:34 +00:00
|
|
|
this.docRelationships = new Relationships();
|
2018-02-05 01:44:28 +00:00
|
|
|
this.docRelationships.createRelationship(
|
2018-08-02 02:09:00 +01:00
|
|
|
this.currentRelationshipId++,
|
2018-02-05 01:44:28 +00:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
|
|
|
"styles.xml",
|
|
|
|
);
|
|
|
|
this.docRelationships.createRelationship(
|
2018-08-02 02:09:00 +01:00
|
|
|
this.currentRelationshipId++,
|
2018-02-05 01:44:28 +00:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
|
|
|
|
"numbering.xml",
|
|
|
|
);
|
2018-06-21 12:03:34 +02:00
|
|
|
this.contentTypes = new ContentTypes();
|
2018-06-11 00:48:50 +01:00
|
|
|
|
|
|
|
this.docRelationships.createRelationship(
|
2018-08-02 02:09:00 +01:00
|
|
|
this.currentRelationshipId++,
|
2018-06-11 00:48:50 +01:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
|
|
|
|
"footnotes.xml",
|
|
|
|
);
|
2018-09-04 17:16:31 +03:00
|
|
|
|
2018-01-29 01:54:10 +00:00
|
|
|
this.media = new Media();
|
2018-05-12 20:04:54 -04:00
|
|
|
|
2018-09-04 17:16:31 +03:00
|
|
|
const templateHeaders = templateDocument && templateDocument.headers;
|
|
|
|
if (!templateHeaders) {
|
|
|
|
this.createHeader();
|
|
|
|
} else {
|
2018-09-06 08:30:23 +01:00
|
|
|
for (const templateHeader of templateHeaders) {
|
2018-09-04 17:16:31 +03:00
|
|
|
this.addHeaderToDocument(templateHeader.header, templateHeader.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const templateFooters = templateDocument && templateDocument.footers;
|
|
|
|
if (!templateFooters) {
|
|
|
|
this.createFooter();
|
|
|
|
} else {
|
2018-09-06 08:30:23 +01:00
|
|
|
for (const templateFooter of templateFooters) {
|
2018-09-04 17:16:31 +03:00
|
|
|
this.addFooterToDocument(templateFooter.footer, templateFooter.type);
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 20:04:54 -04:00
|
|
|
|
2018-02-04 00:58:34 +00:00
|
|
|
this.fileRelationships = new Relationships();
|
2018-02-05 01:44:28 +00:00
|
|
|
this.fileRelationships.createRelationship(
|
|
|
|
1,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
|
|
|
"word/document.xml",
|
|
|
|
);
|
|
|
|
this.fileRelationships.createRelationship(
|
|
|
|
2,
|
|
|
|
"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
|
|
|
|
"docProps/core.xml",
|
|
|
|
);
|
|
|
|
this.fileRelationships.createRelationship(
|
|
|
|
3,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
|
|
|
|
"docProps/app.xml",
|
|
|
|
);
|
2018-02-08 00:12:59 +00:00
|
|
|
this.appProperties = new AppProperties();
|
2018-06-21 12:03:34 +02:00
|
|
|
|
2018-06-03 02:11:21 +01:00
|
|
|
this.footNotes = new FootNotes();
|
2018-09-04 17:16:31 +03:00
|
|
|
|
2018-09-06 08:30:23 +01:00
|
|
|
const headersOptions: IHeaderOptions[] = [];
|
|
|
|
for (const documentHeader of this.documentHeaders) {
|
2018-09-04 17:16:31 +03:00
|
|
|
headersOptions.push({
|
|
|
|
headerId: documentHeader.header.Header.ReferenceId,
|
|
|
|
headerType: documentHeader.type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:30:23 +01:00
|
|
|
const footersOptions: IFooterOptions[] = [];
|
|
|
|
for (const documentFooter of this.documentFooters) {
|
2018-09-04 17:16:31 +03:00
|
|
|
footersOptions.push({
|
|
|
|
footerId: documentFooter.footer.Footer.ReferenceId,
|
|
|
|
footerType: documentFooter.type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:03:34 +02:00
|
|
|
if (!sectionPropertiesOptions) {
|
|
|
|
sectionPropertiesOptions = {
|
2018-09-04 17:16:31 +03:00
|
|
|
headers: headersOptions,
|
|
|
|
footers: footersOptions,
|
2018-06-21 12:03:34 +02:00
|
|
|
};
|
2018-09-06 08:30:23 +01:00
|
|
|
} else {
|
2018-09-04 17:16:31 +03:00
|
|
|
sectionPropertiesOptions.headers = headersOptions;
|
|
|
|
sectionPropertiesOptions.footers = footersOptions;
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:03:34 +02:00
|
|
|
this.document = new Document(sectionPropertiesOptions);
|
2017-12-15 01:16:04 +00:00
|
|
|
}
|
2017-12-15 02:15:44 +00:00
|
|
|
|
|
|
|
public addParagraph(paragraph: Paragraph): void {
|
|
|
|
this.document.addParagraph(paragraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
public createParagraph(text?: string): Paragraph {
|
2018-01-29 02:56:35 +00:00
|
|
|
return this.document.createParagraph(text);
|
2017-12-15 02:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addTable(table: Table): void {
|
|
|
|
return this.document.addTable(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
public createTable(rows: number, cols: number): Table {
|
|
|
|
return this.document.createTable(rows, cols);
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:25:28 +01:00
|
|
|
public addImage(image: Image): File {
|
2018-08-09 01:55:50 +01:00
|
|
|
this.document.addParagraph(image.Paragraph);
|
2018-08-02 02:09:00 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-08-15 22:20:43 +01:00
|
|
|
public createImage(buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
|
2018-08-12 23:07:31 +01:00
|
|
|
const image = Media.addImage(this, buffer, width, height);
|
2018-08-09 01:55:50 +01:00
|
|
|
this.document.addParagraph(image.Paragraph);
|
2018-08-03 00:01:42 +01:00
|
|
|
|
|
|
|
return image;
|
2018-04-20 15:59:06 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:24:16 -05:00
|
|
|
public createHyperlink(link: string, text?: string): Hyperlink {
|
|
|
|
text = text === undefined ? link : text;
|
|
|
|
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount);
|
|
|
|
this.docRelationships.createRelationship(
|
|
|
|
hyperlink.linkId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
|
|
|
link,
|
|
|
|
"External",
|
|
|
|
);
|
|
|
|
return hyperlink;
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:56:13 -04:00
|
|
|
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
|
|
|
|
text = text === undefined ? anchor : text;
|
|
|
|
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
|
2018-07-24 16:56:27 -04:00
|
|
|
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
|
|
|
|
// we don't need to create a new relationship.
|
2018-07-24 15:56:13 -04:00
|
|
|
return hyperlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
public createBookmark(name: string, text?: string): Bookmark {
|
|
|
|
text = text === undefined ? name : text;
|
|
|
|
const bookmark = new Bookmark(name, text, this.docRelationships.RelationshipCount);
|
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:59:38 +01:00
|
|
|
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
|
2018-06-21 12:03:34 +02:00
|
|
|
this.document.Body.addSection(sectionPropertiesOptions);
|
|
|
|
}
|
|
|
|
|
2018-06-25 19:49:46 +01:00
|
|
|
public createFootnote(paragraph: Paragraph): void {
|
|
|
|
this.footNotes.createFootNote(paragraph);
|
|
|
|
}
|
|
|
|
|
2018-09-04 17:16:31 +03:00
|
|
|
public createHeader(): HeaderWrapper {
|
|
|
|
const header = new HeaderWrapper(this.currentRelationshipId++);
|
|
|
|
this.addHeaderToDocument(header);
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2018-09-07 21:48:59 +01:00
|
|
|
public createFooter(): FooterWrapper {
|
|
|
|
const footer = new FooterWrapper(this.currentRelationshipId++);
|
|
|
|
this.addFooterToDocument(footer);
|
|
|
|
return footer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public createFirstPageHeader(): HeaderWrapper {
|
|
|
|
const headerWrapper = this.createHeader();
|
|
|
|
|
|
|
|
this.document.Body.DefaultSection.addChildElement(
|
|
|
|
new HeaderReference({
|
|
|
|
headerType: HeaderReferenceType.FIRST,
|
|
|
|
headerId: headerWrapper.Header.ReferenceId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return headerWrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getFooterByRefNumber(refId: number): FooterWrapper {
|
|
|
|
const entry = this.documentFooters.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId);
|
|
|
|
if (entry) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
throw new Error(`There is no footer with given reference id ${refId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getHeaderByRefNumber(refId: number): HeaderWrapper {
|
|
|
|
const entry = this.documentHeaders.map((item) => item.header).find((h) => h.Header.ReferenceId === refId);
|
|
|
|
if (entry) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
throw new Error(`There is no header with given reference id ${refId}`);
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:30:23 +01:00
|
|
|
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
|
2018-09-04 17:16:31 +03:00
|
|
|
this.documentHeaders.push({ header, type });
|
2018-06-21 12:03:34 +02:00
|
|
|
this.docRelationships.createRelationship(
|
2018-08-07 01:25:28 +01:00
|
|
|
header.Header.ReferenceId,
|
2018-06-21 12:03:34 +02:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
|
2018-09-04 17:16:31 +03:00
|
|
|
`header${this.documentHeaders.length}.xml`,
|
2018-06-21 12:03:34 +02:00
|
|
|
);
|
2018-09-04 17:16:31 +03:00
|
|
|
this.contentTypes.addHeader(this.documentHeaders.length);
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 08:30:23 +01:00
|
|
|
private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void {
|
2018-09-04 17:16:31 +03:00
|
|
|
this.documentFooters.push({ footer, type });
|
2018-06-21 12:03:34 +02:00
|
|
|
this.docRelationships.createRelationship(
|
2018-08-07 01:25:28 +01:00
|
|
|
footer.Footer.ReferenceId,
|
2018-06-21 12:03:34 +02:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
|
2018-09-04 17:16:31 +03:00
|
|
|
`footer${this.documentFooters.length}.xml`,
|
2018-06-21 12:03:34 +02:00
|
|
|
);
|
2018-09-04 17:16:31 +03:00
|
|
|
this.contentTypes.addFooter(this.documentFooters.length);
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:15:44 +00:00
|
|
|
public get Document(): Document {
|
|
|
|
return this.document;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get Styles(): Styles {
|
|
|
|
return this.styles;
|
|
|
|
}
|
|
|
|
|
2018-02-04 01:43:03 +00:00
|
|
|
public get CoreProperties(): CoreProperties {
|
|
|
|
return this.coreProperties;
|
2017-12-15 02:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public get Numbering(): Numbering {
|
|
|
|
return this.numbering;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get Media(): Media {
|
|
|
|
return this.media;
|
|
|
|
}
|
2018-01-10 00:29:17 +00:00
|
|
|
|
2018-02-04 00:58:34 +00:00
|
|
|
public get DocumentRelationships(): Relationships {
|
|
|
|
return this.docRelationships;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get FileRelationships(): Relationships {
|
|
|
|
return this.fileRelationships;
|
2018-01-10 00:29:17 +00:00
|
|
|
}
|
2018-01-29 01:54:10 +00:00
|
|
|
|
2018-01-31 00:31:54 +00:00
|
|
|
public get Header(): HeaderWrapper {
|
2018-09-04 17:16:31 +03:00
|
|
|
return this.documentHeaders[0].header;
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get Headers(): HeaderWrapper[] {
|
2018-09-04 17:16:31 +03:00
|
|
|
return this.documentHeaders.map((item) => item.header);
|
2018-01-29 01:54:10 +00:00
|
|
|
}
|
2018-01-29 21:53:22 +00:00
|
|
|
|
2018-01-31 00:31:54 +00:00
|
|
|
public get Footer(): FooterWrapper {
|
2018-09-04 17:16:31 +03:00
|
|
|
return this.documentFooters[0].footer;
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get Footers(): FooterWrapper[] {
|
2018-09-04 17:16:31 +03:00
|
|
|
return this.documentFooters.map((item) => item.footer);
|
2018-01-29 21:53:22 +00:00
|
|
|
}
|
2018-02-03 20:56:20 +00:00
|
|
|
|
|
|
|
public get ContentTypes(): ContentTypes {
|
|
|
|
return this.contentTypes;
|
|
|
|
}
|
2018-02-08 00:12:59 +00:00
|
|
|
|
|
|
|
public get AppProperties(): AppProperties {
|
|
|
|
return this.appProperties;
|
|
|
|
}
|
2018-06-03 02:11:21 +01:00
|
|
|
|
|
|
|
public get FootNotes(): FootNotes {
|
|
|
|
return this.footNotes;
|
|
|
|
}
|
2017-12-15 01:16:04 +00:00
|
|
|
}
|