Files
docx-js/src/file/file.ts

295 lines
10 KiB
TypeScript
Raw Normal View History

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";
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";
import { FooterWrapper } from "./footer-wrapper";
2018-06-03 02:11:21 +01:00
import { FootNotes } from "./footnotes";
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";
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;
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
private nextId: number = 1;
2017-12-15 02:15:44 +00: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",
};
}
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(
this.nextId++,
2018-02-05 01:44:28 +00:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"styles.xml",
);
this.docRelationships.createRelationship(
this.nextId++,
2018-02-05 01:44:28 +00:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
"numbering.xml",
);
this.contentTypes = new ContentTypes();
2018-06-11 00:48:50 +01:00
this.docRelationships.createRelationship(
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
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-03 02:11:21 +01:00
this.footNotes = new FootNotes();
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 {
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
}
public createImageData(imageName: string, data: Buffer, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMediaWithData(imageName, data, this.nextId++, width, height);
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);
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;
}
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions);
}
2018-06-25 19:49:46 +01:00
public createFootnote(paragraph: Paragraph): void {
this.footNotes.createFootNote(paragraph);
}
/**
* Creates new header.
*/
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;
}
/**
* Creates new footer.
*/
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;
}
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,
}),
);
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
public get Header(): HeaderWrapper {
return this.headerWrapper[0];
}
public get Headers(): HeaderWrapper[] {
return this.headerWrapper;
2018-01-29 01:54:10 +00:00
}
2018-01-29 21:53:22 +00:00
public HeaderByRefNumber(refId: number): HeaderWrapper {
const entry = this.headerWrapper.find((h) => h.Header.referenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no header with given reference id ${refId}`);
2018-05-12 20:04:54 -04:00
}
public get Footer(): FooterWrapper {
return this.footerWrapper[0];
}
public get Footers(): FooterWrapper[] {
return this.footerWrapper;
2018-01-29 21:53:22 +00:00
}
2018-02-03 20:56:20 +00:00
public FooterByRefNumber(refId: number): FooterWrapper {
const entry = this.footerWrapper.find((h) => h.Footer.referenceId === refId);
if (entry) {
return entry;
}
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
}