From 5e921f1dfc0ce4c41d993a4982c8a8a5edc6cf5e Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 29 Jan 2018 21:53:22 +0000 Subject: [PATCH] Add footer --- demo/demo8.js | 1 + src/export/packer/compiler.ts | 5 ++ .../footer-reference-attributes.ts | 13 ++++ .../footer-reference/footer-reference.ts | 14 ++++ .../section-properties/section-properties.ts | 2 + src/file/file.ts | 7 ++ src/file/footer/footer-attributes.ts | 53 +++++++++++++++ src/file/footer/footer.ts | 66 +++++++++++++++++++ src/file/header/header-attributes.ts | 46 +++++++++---- src/file/header/header.ts | 17 +++-- .../relationship/relationship.ts | 3 +- src/file/relationships/relationships.ts | 1 + template/[Content_Types].xml | 1 + 13 files changed, 210 insertions(+), 19 deletions(-) create mode 100644 src/file/document/body/section-properties/footer-reference/footer-reference-attributes.ts create mode 100644 src/file/document/body/section-properties/footer-reference/footer-reference.ts create mode 100644 src/file/footer/footer-attributes.ts create mode 100644 src/file/footer/footer.ts diff --git a/demo/demo8.js b/demo/demo8.js index 4bde11e1fc..6df6d3e77e 100644 --- a/demo/demo8.js +++ b/demo/demo8.js @@ -5,6 +5,7 @@ var doc = new docx.File(); doc.createParagraph("Hello World"); doc.Header.createParagraph("Header text"); +doc.Footer.createParagraph("Footer text"); var exporter = new docx.LocalPacker(doc); exporter.pack('My Document'); diff --git a/src/export/packer/compiler.ts b/src/export/packer/compiler.ts index 76bdcc2dad..9a3a57d998 100644 --- a/src/export/packer/compiler.ts +++ b/src/export/packer/compiler.ts @@ -43,6 +43,7 @@ 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)); this.archive.append(xmlDocument, { name: "word/document.xml", @@ -64,6 +65,10 @@ export class Compiler { name: "word/header1.xml", }); + this.archive.append(xmlFooter, { + name: "word/footer1.xml", + }); + this.archive.append(xmlRelationships, { name: "word/_rels/document.xml.rels", }); diff --git a/src/file/document/body/section-properties/footer-reference/footer-reference-attributes.ts b/src/file/document/body/section-properties/footer-reference/footer-reference-attributes.ts new file mode 100644 index 0000000000..0097de0dbb --- /dev/null +++ b/src/file/document/body/section-properties/footer-reference/footer-reference-attributes.ts @@ -0,0 +1,13 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IFooterReferenceAttributes { + type: string; + id: string; +} + +export class FooterReferenceAttributes extends XmlAttributeComponent { + protected xmlKeys = { + type: "w:type", + id: "r:id", + }; +} diff --git a/src/file/document/body/section-properties/footer-reference/footer-reference.ts b/src/file/document/body/section-properties/footer-reference/footer-reference.ts new file mode 100644 index 0000000000..6b7012b6d3 --- /dev/null +++ b/src/file/document/body/section-properties/footer-reference/footer-reference.ts @@ -0,0 +1,14 @@ +import { XmlComponent } from "file/xml-components"; +import { FooterReferenceAttributes } from "./footer-reference-attributes"; + +export class FooterReference extends XmlComponent { + constructor() { + super("w:footerReference"); + this.root.push( + new FooterReferenceAttributes({ + type: "default", + id: `rId${4}`, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index a7aae06f10..27d3f0bd99 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -4,6 +4,7 @@ import { Columns } from "./columns/columns"; import { IColumnsAttributes } from "./columns/columns-attributes"; import { DocumentGrid } from "./doc-grid/doc-grid"; import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; +import { FooterReference } from "./footer-reference/footer-reference"; import { HeaderReference } from "./header-reference/header-reference"; import { PageMargin } from "./page-margin/page-margin"; import { IPageMarginAttributes } from "./page-margin/page-margin-attributes"; @@ -51,5 +52,6 @@ export class SectionProperties extends XmlComponent { this.root.push(new Columns(mergedOptions.space)); this.root.push(new DocumentGrid(mergedOptions.linePitch)); this.root.push(new HeaderReference()); + this.root.push(new FooterReference()); } } diff --git a/src/file/file.ts b/src/file/file.ts index 9f6f3016a8..a674a453b9 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -1,5 +1,6 @@ import { Document } from "./document"; import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties"; +import { Footer } from "./footer/footer"; import { Header } from "./header/header"; import { Media } from "./media"; import { Numbering } from "./numbering"; @@ -18,6 +19,7 @@ export class File { private readonly media: Media; private readonly relationships: Relationships; private readonly header: Header; + private readonly footer: Footer; constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { this.document = new Document(sectionPropertiesOptions); @@ -37,6 +39,7 @@ export class File { this.relationships = new Relationships(); this.media = new Media(); this.header = new Header(); + this.footer = new Footer(); } public addParagraph(paragraph: Paragraph): void { @@ -92,4 +95,8 @@ export class File { public get Header(): Header { return this.header; } + + public get Footer(): Footer { + return this.footer; + } } diff --git a/src/file/footer/footer-attributes.ts b/src/file/footer/footer-attributes.ts new file mode 100644 index 0000000000..86f821f5f4 --- /dev/null +++ b/src/file/footer/footer-attributes.ts @@ -0,0 +1,53 @@ +import { XmlAttributeComponent } from "file/xml-components"; + +export interface IFooterAttributesProperties { + wpc?: string; + mc?: string; + o?: string; + r?: string; + m?: string; + v?: string; + wp14?: string; + wp?: string; + w10?: string; + w?: string; + w14?: string; + w15?: string; + wpg?: string; + wpi?: string; + wne?: string; + wps?: string; + cp?: string; + dc?: string; + dcterms?: string; + dcmitype?: string; + xsi?: string; + type?: string; +} + +export class FooterAttributes extends XmlAttributeComponent { + protected xmlKeys = { + wpc: "xmlns:wpc", + mc: "xmlns:mc", + o: "xmlns:o", + r: "xmlns:r", + m: "xmlns:m", + v: "xmlns:v", + wp14: "xmlns:wp14", + wp: "xmlns:wp", + w10: "xmlns:w10", + w: "xmlns:w", + w14: "xmlns:w14", + w15: "xmlns:w15", + wpg: "xmlns:wpg", + wpi: "xmlns:wpi", + wne: "xmlns:wne", + wps: "xmlns:wps", + cp: "xmlns:cp", + dc: "xmlns:dc", + dcterms: "xmlns:dcterms", + dcmitype: "xmlns:dcmitype", + xsi: "xmlns:xsi", + type: "xsi:type", + }; +} diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts new file mode 100644 index 0000000000..6ec1df5abf --- /dev/null +++ b/src/file/footer/footer.ts @@ -0,0 +1,66 @@ +// http://officeopenxml.com/WPfooters.php +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() { + super("w:ftr"); + this.root.push( + new FooterAttributes({ + wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", + o: "urn:schemas-microsoft-com:office:office", + r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + m: "http://schemas.openxmlformats.org/officeDocument/2006/math", + v: "urn:schemas-microsoft-com:vml", + wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + w10: "urn:schemas-microsoft-com:office:word", + w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + w14: "http://schemas.microsoft.com/office/word/2010/wordml", + w15: "http://schemas.microsoft.com/office/word/2012/wordml", + wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + wne: "http://schemas.microsoft.com/office/word/2006/wordml", + wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + }), + ); + } + + public addParagraph(paragraph: Paragraph): void { + this.root.push(paragraph); + } + + public createParagraph(text?: string): Paragraph { + const para = new Paragraph(text); + this.addParagraph(para); + return para; + } + + public addTable(table: Table): void { + this.root.push(table); + } + + public createTable(rows: number, cols: number): Table { + const table = new Table(rows, cols); + this.addTable(table); + return table; + } + + public addDrawing(imageData: IMediaData): void { + const paragraph = new Paragraph(); + const run = new PictureRun(imageData); + paragraph.addRun(run); + + this.root.push(paragraph); + } + + public createDrawing(imageData: IMediaData): void { + this.addDrawing(imageData); + + return; + } +} diff --git a/src/file/header/header-attributes.ts b/src/file/header/header-attributes.ts index e893306192..e47271841c 100644 --- a/src/file/header/header-attributes.ts +++ b/src/file/header/header-attributes.ts @@ -1,31 +1,53 @@ import { XmlAttributeComponent } from "file/xml-components"; export interface IHeaderAttributesProperties { + wpc?: string; + mc?: string; o?: string; r?: string; + m?: string; v?: string; - w?: string; - w10?: string; - wp?: string; - wps?: string; - wpg?: string; - mc?: string; wp14?: string; + wp?: string; + w10?: string; + w?: string; w14?: string; + w15?: string; + wpg?: string; + wpi?: string; + wne?: string; + wps?: string; + cp?: string; + dc?: string; + dcterms?: string; + dcmitype?: string; + xsi?: string; + type?: string; } export class HeaderAttributes extends XmlAttributeComponent { protected xmlKeys = { + wpc: "xmlns:wpc", + mc: "xmlns:mc", o: "xmlns:o", r: "xmlns:r", + m: "xmlns:m", v: "xmlns:v", - w: "xmlns:w", - w10: "xmlns:w10", - wp: "xmlns:wp", - wps: "xmlns:wps", - wpg: "xmlns:wpg", - mc: "xmlns:mc", wp14: "xmlns:wp14", + wp: "xmlns:wp", + w10: "xmlns:w10", + w: "xmlns:w", w14: "xmlns:w14", + w15: "xmlns:w15", + wpg: "xmlns:wpg", + wpi: "xmlns:wpi", + wne: "xmlns:wne", + wps: "xmlns:wps", + cp: "xmlns:cp", + dc: "xmlns:dc", + dcterms: "xmlns:dcterms", + dcmitype: "xmlns:dcmitype", + xsi: "xmlns:xsi", + type: "xsi:type", }; } diff --git a/src/file/header/header.ts b/src/file/header/header.ts index 8e2c37fe2d..ee65a2bd5b 100644 --- a/src/file/header/header.ts +++ b/src/file/header/header.ts @@ -10,17 +10,22 @@ export class Header extends XmlComponent { super("w:hdr"); this.root.push( new HeaderAttributes({ + wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", o: "urn:schemas-microsoft-com:office:office", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + m: "http://schemas.openxmlformats.org/officeDocument/2006/math", v: "urn:schemas-microsoft-com:vml", - w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", - w10: "urn:schemas-microsoft-com:office:word", - wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", - wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", - wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", - mc: "http://schemas.openxmlformats.org/markup-compatibility/2006", wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + w10: "urn:schemas-microsoft-com:office:word", + w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", w14: "http://schemas.microsoft.com/office/word/2010/wordml", + w15: "http://schemas.microsoft.com/office/word/2012/wordml", + wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + wne: "http://schemas.microsoft.com/office/word/2006/wordml", + wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", }), ); } diff --git a/src/file/relationships/relationship/relationship.ts b/src/file/relationships/relationship/relationship.ts index e3ac807946..016df01506 100644 --- a/src/file/relationships/relationship/relationship.ts +++ b/src/file/relationships/relationship/relationship.ts @@ -9,7 +9,8 @@ export type RelationshipType = | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" - | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"; + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" + | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"; export class Relationship extends XmlComponent { constructor(id: string, type: RelationshipType, target: string) { diff --git a/src/file/relationships/relationships.ts b/src/file/relationships/relationships.ts index c665be063e..2b7043d0a3 100644 --- a/src/file/relationships/relationships.ts +++ b/src/file/relationships/relationships.ts @@ -14,6 +14,7 @@ export class Relationships extends XmlComponent { this.createRelationship(1, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "styles.xml"); this.createRelationship(2, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering", "numbering.xml"); this.createRelationship(3, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", "header1.xml"); + this.createRelationship(4, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", "footer1.xml"); } public addRelationship(relationship: Relationship): void { diff --git a/template/[Content_Types].xml b/template/[Content_Types].xml index 978bf4e3fe..78030c0829 100644 --- a/template/[Content_Types].xml +++ b/template/[Content_Types].xml @@ -9,6 +9,7 @@ +