diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index 9a3a57d998..7a62dc0dff 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -42,8 +42,10 @@ export class Compiler { }); const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); const xmlRelationships = xml(this.formatter.format(this.file.Relationships)); - const xmlHeader = xml(this.formatter.format(this.file.Header)); - const xmlFooter = xml(this.formatter.format(this.file.Footer)); + const xmlHeader = xml(this.formatter.format(this.file.Header.Header)); + const xmlFooter = xml(this.formatter.format(this.file.Footer.Footer)); + const xmlHeaderRelationships = xml(this.formatter.format(this.file.Header.Relationships)); + const xmlFooterRelationships = xml(this.formatter.format(this.file.Footer.Relationships)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -73,6 +75,14 @@ export class Compiler { name: "word/_rels/document.xml.rels", }); + this.archive.append(xmlHeaderRelationships, { + name: "word/_rels/header1.xml.rels", + }); + + this.archive.append(xmlFooterRelationships, { + name: "word/_rels/footer1.xml.rels", + }); + for (const data of this.file.Media.array) { this.archive.append(data.stream, { name: `word/media/${data.fileName}`, diff --git a/src/file/file.ts b/src/file/file.ts index 5811661ee7..b761874ab7 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,7 +1,7 @@ import { Document } from "./document"; import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties"; -import { Footer } from "./footer/footer"; -import { Header } from "./header/header"; +import { FooterWrapper } from "./footer-wrapper"; +import { HeaderWrapper } from "./header-wrapper"; import { Media } from "./media"; import { Numbering } from "./numbering"; import { Paragraph } from "./paragraph"; @@ -18,8 +18,8 @@ export class File { private readonly numbering: Numbering; private readonly media: Media; private readonly relationships: Relationships; - private readonly header: Header; - private readonly footer: Footer; + private readonly headerWrapper: HeaderWrapper; + private readonly footerWrapper: FooterWrapper; constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { this.document = new Document(sectionPropertiesOptions); @@ -38,8 +38,8 @@ export class File { this.numbering = new Numbering(); this.relationships = new Relationships(); this.media = new Media(); - this.header = new Header(this.media, this.relationships); - this.footer = new Footer(this.media, this.relationships); + this.headerWrapper = new HeaderWrapper(this.media); + this.footerWrapper = new FooterWrapper(this.media); } public addParagraph(paragraph: Paragraph): void { @@ -92,11 +92,11 @@ export class File { return this.relationships; } - public get Header(): Header { - return this.header; + public get Header(): HeaderWrapper { + return this.headerWrapper; } - public get Footer(): Footer { - return this.footer; + public get Footer(): FooterWrapper { + return this.footerWrapper; } } diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts new file mode 100644 index 0000000000..bb6efbe25a --- /dev/null +++ b/src/file/footer-wrapper.ts @@ -0,0 +1,55 @@ +import { Footer } from "./footer/footer"; +import { IMediaData, Media } from "./media"; +import { Paragraph } from "./paragraph"; +import { Relationships } from "./relationships"; +import { Table } from "./table"; + +export class FooterWrapper { + private readonly footer: Footer; + private readonly relationships: Relationships; + + constructor(private readonly media: Media) { + this.footer = new Footer(); + this.relationships = new Relationships(); + } + + public addParagraph(paragraph: Paragraph): void { + this.footer.addParagraph(paragraph); + } + + public createParagraph(text?: string): Paragraph { + const para = new Paragraph(text); + this.addParagraph(para); + return para; + } + + public addTable(table: Table): void { + this.footer.addTable(table); + } + + public createTable(rows: number, cols: number): Table { + return this.footer.createTable(rows, cols); + } + + public addDrawing(imageData: IMediaData): void { + this.footer.addDrawing(imageData); + } + + public createImage(image: string): void { + const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); + this.relationships.createRelationship( + mediaData.referenceId, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + `media/${mediaData.fileName}`, + ); + this.addDrawing(mediaData); + } + + public get Footer(): Footer { + return this.footer; + } + + public get Relationships(): Relationships { + return this.relationships; + } +} diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index 4ad6cbf392..9a64909caa 100644 --- a/src/file/footer/footer.ts +++ b/src/file/footer/footer.ts @@ -1,13 +1,12 @@ // http://officeopenxml.com/WPfooters.php -import { IMediaData, Media } from "file/media"; -import { Relationships } from "file/relationships"; +import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; import { Paragraph, PictureRun } from "../paragraph"; import { Table } from "../table"; import { FooterAttributes } from "./footer-attributes"; export class Footer extends XmlComponent { - constructor(private readonly media: Media, private readonly relationships: Relationships) { + constructor() { super("w:ftr"); this.root.push( new FooterAttributes({ @@ -58,14 +57,4 @@ export class Footer extends XmlComponent { this.root.push(paragraph); } - - public createImage(image: string): void { - const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); - this.relationships.createRelationship( - mediaData.referenceId, - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - `media/${mediaData.fileName}`, - ); - this.addDrawing(mediaData); - } } diff --git a/src/file/footer/index.ts b/src/file/footer/index.ts new file mode 100644 index 0000000000..0df22f5218 --- /dev/null +++ b/src/file/footer/index.ts @@ -0,0 +1 @@ +export * from "./footer"; diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts new file mode 100644 index 0000000000..df15871bef --- /dev/null +++ b/src/file/header-wrapper.ts @@ -0,0 +1,55 @@ +import { Header } from "./header/header"; +import { IMediaData, Media } from "./media"; +import { Paragraph } from "./paragraph"; +import { Relationships } from "./relationships"; +import { Table } from "./table"; + +export class HeaderWrapper { + private readonly header: Header; + private readonly relationships: Relationships; + + constructor(private readonly media: Media) { + this.header = new Header(); + 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); + } + + public addDrawing(imageData: IMediaData): void { + this.header.addDrawing(imageData); + } + + public createImage(image: string): void { + const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); + this.relationships.createRelationship( + mediaData.referenceId, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", + `media/${mediaData.fileName}`, + ); + this.addDrawing(mediaData); + } + + public get Header(): Header { + return this.header; + } + + public get Relationships(): Relationships { + return this.relationships; + } +} diff --git a/src/file/header/header.ts b/src/file/header/header.ts index c2b759fbd2..b4dd0918ef 100644 --- a/src/file/header/header.ts +++ b/src/file/header/header.ts @@ -1,13 +1,12 @@ // http://officeopenxml.com/WPheaders.php -import { IMediaData, Media } from "file/media"; -import { Relationships } from "file/relationships"; +import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; import { Paragraph, PictureRun } from "../paragraph"; import { Table } from "../table"; import { HeaderAttributes } from "./header-attributes"; export class Header extends XmlComponent { - constructor(private readonly media: Media, private readonly relationships: Relationships) { + constructor() { super("w:hdr"); this.root.push( new HeaderAttributes({ @@ -58,14 +57,4 @@ export class Header extends XmlComponent { this.root.push(paragraph); } - - public createImage(image: string): void { - const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount); - this.relationships.createRelationship( - mediaData.referenceId, - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", - `media/${mediaData.fileName}`, - ); - this.addDrawing(mediaData); - } } diff --git a/src/file/header/index.ts b/src/file/header/index.ts new file mode 100644 index 0000000000..49ac70fe21 --- /dev/null +++ b/src/file/header/index.ts @@ -0,0 +1 @@ +export * from "./header";