2018-05-06 02:58:16 +01:00
|
|
|
import { IMediaData } from "file/media";
|
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-06-26 00:03:28 +01:00
|
|
|
import { FooterReferenceType, HeaderReference, HeaderReferenceType } from "./document/body/section-properties";
|
2018-01-24 13:09:34 +00:00
|
|
|
import { SectionPropertiesOptions } from "./document/body/section-properties/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";
|
2017-12-20 00:06:08 +00:00
|
|
|
import { Media } from "./media";
|
2017-12-20 00:01:23 +00:00
|
|
|
import { Numbering } from "./numbering";
|
2018-07-24 15:56:13 -04:00
|
|
|
import { Bookmark, Hyperlink, Paragraph, PictureRun } 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";
|
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-06-21 12:03:34 +02:00
|
|
|
private readonly headerWrapper: HeaderWrapper[] = [];
|
|
|
|
private readonly footerWrapper: FooterWrapper[] = [];
|
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-06-21 12:03:34 +02:00
|
|
|
private nextId: 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-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-02-04 00:58:34 +00:00
|
|
|
this.docRelationships = new Relationships();
|
2018-02-05 01:44:28 +00:00
|
|
|
this.docRelationships.createRelationship(
|
2018-06-21 12:03:34 +02:00
|
|
|
this.nextId++,
|
2018-02-05 01:44:28 +00:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
|
|
|
"styles.xml",
|
|
|
|
);
|
|
|
|
this.docRelationships.createRelationship(
|
2018-06-21 12:03:34 +02:00
|
|
|
this.nextId++,
|
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-06-27 23:34:23 +01:00
|
|
|
this.nextId++,
|
2018-06-11 00:48:50 +01:00
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
|
|
|
|
"footnotes.xml",
|
|
|
|
);
|
2018-01-29 01:54:10 +00:00
|
|
|
this.media = new Media();
|
2018-05-12 20:04:54 -04:00
|
|
|
|
2018-06-26 00:03:28 +01:00
|
|
|
const header = this.createHeader();
|
|
|
|
const footer = this.createFooter();
|
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-06-21 12:03:34 +02:00
|
|
|
if (!sectionPropertiesOptions) {
|
|
|
|
sectionPropertiesOptions = {
|
|
|
|
footerType: FooterReferenceType.DEFAULT,
|
|
|
|
headerType: HeaderReferenceType.DEFAULT,
|
|
|
|
headerId: header.Header.referenceId,
|
|
|
|
footerId: footer.Footer.referenceId,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
sectionPropertiesOptions.headerId = header.Header.referenceId;
|
|
|
|
sectionPropertiesOptions.footerId = footer.Footer.referenceId;
|
|
|
|
}
|
|
|
|
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-03-24 19:06:10 +00:00
|
|
|
public createImage(image: string): PictureRun {
|
2018-06-21 12:03:34 +02:00
|
|
|
const mediaData = this.media.addMedia(image, this.nextId++);
|
2018-02-04 00:58:34 +00:00
|
|
|
this.docRelationships.createRelationship(
|
2018-01-23 01:33:12 +00:00
|
|
|
mediaData.referenceId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
|
|
|
`media/${mediaData.fileName}`,
|
|
|
|
);
|
2018-03-24 19:06:10 +00:00
|
|
|
return this.document.createDrawing(mediaData);
|
2018-01-11 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:32:31 +01:00
|
|
|
public createImageFromBuffer(key: string, data: Buffer, width?: number, height?: number): IMediaData {
|
|
|
|
const mediaData = this.media.addMediaWithData(key, data, this.nextId++, width, height);
|
2018-04-20 15:59:06 +02:00
|
|
|
this.docRelationships.createRelationship(
|
|
|
|
mediaData.referenceId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
|
|
|
`media/${mediaData.fileName}`,
|
|
|
|
);
|
|
|
|
return mediaData;
|
|
|
|
}
|
|
|
|
|
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-06-21 12:03:34 +02:00
|
|
|
public createHeader(): HeaderWrapper {
|
|
|
|
const header = new HeaderWrapper(this.media, this.nextId++);
|
|
|
|
this.headerWrapper.push(header);
|
|
|
|
this.docRelationships.createRelationship(
|
|
|
|
header.Header.referenceId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
|
|
|
|
`header${this.headerWrapper.length}.xml`,
|
|
|
|
);
|
|
|
|
this.contentTypes.addHeader(this.headerWrapper.length);
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
public createFooter(): FooterWrapper {
|
|
|
|
const footer = new FooterWrapper(this.media, this.nextId++);
|
|
|
|
this.footerWrapper.push(footer);
|
|
|
|
this.docRelationships.createRelationship(
|
|
|
|
footer.Footer.referenceId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
|
|
|
|
`footer${this.footerWrapper.length}.xml`,
|
|
|
|
);
|
|
|
|
this.contentTypes.addFooter(this.footerWrapper.length);
|
|
|
|
return footer;
|
|
|
|
}
|
|
|
|
|
2018-06-26 00:03:28 +01:00
|
|
|
public createFirstPageHeader(): HeaderWrapper {
|
|
|
|
const headerWrapper = this.createHeader();
|
|
|
|
|
2018-06-26 00:10:08 +01:00
|
|
|
this.document.Body.DefaultSection.addChildElement(
|
|
|
|
new HeaderReference({
|
|
|
|
headerType: HeaderReferenceType.FIRST,
|
|
|
|
headerId: headerWrapper.Header.referenceId,
|
|
|
|
}),
|
|
|
|
);
|
2018-06-26 00:03:28 +01:00
|
|
|
|
|
|
|
return headerWrapper;
|
|
|
|
}
|
|
|
|
|
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-06-21 12:03:34 +02:00
|
|
|
return this.headerWrapper[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public get Headers(): HeaderWrapper[] {
|
2018-01-31 00:31:54 +00:00
|
|
|
return this.headerWrapper;
|
2018-01-29 01:54:10 +00:00
|
|
|
}
|
2018-01-29 21:53:22 +00:00
|
|
|
|
2018-06-21 12:03:34 +02:00
|
|
|
public HeaderByRefNumber(refId: number): HeaderWrapper {
|
|
|
|
const entry = this.headerWrapper.find((h) => h.Header.referenceId === refId);
|
2018-06-22 22:59:38 +01:00
|
|
|
if (entry) {
|
|
|
|
return entry;
|
|
|
|
}
|
2018-06-21 12:03:34 +02:00
|
|
|
throw new Error(`There is no header with given reference id ${refId}`);
|
2018-05-12 20:04:54 -04:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:31:54 +00:00
|
|
|
public get Footer(): FooterWrapper {
|
2018-06-21 12:03:34 +02:00
|
|
|
return this.footerWrapper[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public get Footers(): FooterWrapper[] {
|
2018-01-31 00:31:54 +00:00
|
|
|
return this.footerWrapper;
|
2018-01-29 21:53:22 +00:00
|
|
|
}
|
2018-02-03 20:56:20 +00:00
|
|
|
|
2018-06-21 12:03:34 +02:00
|
|
|
public FooterByRefNumber(refId: number): FooterWrapper {
|
|
|
|
const entry = this.footerWrapper.find((h) => h.Footer.referenceId === refId);
|
2018-06-22 22:59:38 +01:00
|
|
|
if (entry) {
|
|
|
|
return entry;
|
|
|
|
}
|
2018-06-21 12:03:34 +02:00
|
|
|
throw new Error(`There is no footer with given reference id ${refId}`);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|