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

70 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-09-06 08:30:23 +01:00
import { IMediaData } from "file/media";
2018-06-09 23:49:01 +01:00
import { XmlComponent } from "file/xml-components";
import { Header } from "./header/header";
2018-08-09 01:55:50 +01:00
import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
2018-08-29 18:36:48 +03:00
export class HeaderWrapper {
private readonly header: Header;
private readonly relationships: Relationships;
2018-09-04 17:16:31 +03:00
public readonly media = new Media();
2018-09-04 17:16:31 +03:00
// constructor(private readonly media: Media, referenceId: number, initContent? : XmlComponent) {
2018-09-06 08:30:23 +01:00
constructor(referenceId: number, initContent?: XmlComponent) {
2018-08-29 18:36:48 +03:00
this.header = new Header(referenceId, initContent);
this.relationships = new Relationships();
}
public addParagraph(paragraph: Paragraph): void {
this.header.addParagraph(paragraph);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): void {
this.header.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.header.createTable(rows, cols);
}
2018-06-09 23:49:01 +01:00
public addChildElement(childElement: XmlComponent | string): void {
this.header.addChildElement(childElement);
}
2018-09-06 08:30:23 +01:00
public addImageRelation(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
2018-09-04 17:16:31 +03:00
const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship(
2018-09-04 17:16:31 +03:00
refId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
2018-09-04 17:16:31 +03:00
return mediaData;
}
2018-09-06 08:30:23 +01:00
2018-09-04 17:16:31 +03:00
public createImage(image: Buffer, width?: number, height?: number): void {
2018-09-06 08:30:23 +01:00
const mediaData = this.addImageRelation(image, this.relationships.RelationshipCount, width, height);
2018-08-09 01:55:50 +01:00
this.addImage(new Image(new ImageParagraph(mediaData)));
}
2018-08-07 01:25:28 +01:00
public addImage(image: Image): HeaderWrapper {
2018-08-09 01:55:50 +01:00
this.header.addParagraph(image.Paragraph);
return this;
}
public get Header(): Header {
return this.header;
}
public get Relationships(): Relationships {
return this.relationships;
}
}