Add footer

This commit is contained in:
Dolan
2018-01-29 21:53:22 +00:00
parent 8b8c664f0f
commit 5e921f1dfc
13 changed files with 210 additions and 19 deletions

View File

@ -5,6 +5,7 @@ var doc = new docx.File();
doc.createParagraph("Hello World"); doc.createParagraph("Hello World");
doc.Header.createParagraph("Header text"); doc.Header.createParagraph("Header text");
doc.Footer.createParagraph("Footer text");
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document'); exporter.pack('My Document');

View File

@ -43,6 +43,7 @@ export class Compiler {
const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
const xmlRelationships = xml(this.formatter.format(this.file.Relationships)); const xmlRelationships = xml(this.formatter.format(this.file.Relationships));
const xmlHeader = xml(this.formatter.format(this.file.Header)); const xmlHeader = xml(this.formatter.format(this.file.Header));
const xmlFooter = xml(this.formatter.format(this.file.Footer));
this.archive.append(xmlDocument, { this.archive.append(xmlDocument, {
name: "word/document.xml", name: "word/document.xml",
@ -64,6 +65,10 @@ export class Compiler {
name: "word/header1.xml", name: "word/header1.xml",
}); });
this.archive.append(xmlFooter, {
name: "word/footer1.xml",
});
this.archive.append(xmlRelationships, { this.archive.append(xmlRelationships, {
name: "word/_rels/document.xml.rels", name: "word/_rels/document.xml.rels",
}); });

View File

@ -0,0 +1,13 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IFooterReferenceAttributes {
type: string;
id: string;
}
export class FooterReferenceAttributes extends XmlAttributeComponent<IFooterReferenceAttributes> {
protected xmlKeys = {
type: "w:type",
id: "r:id",
};
}

View File

@ -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}`,
}),
);
}
}

View File

@ -4,6 +4,7 @@ import { Columns } from "./columns/columns";
import { IColumnsAttributes } from "./columns/columns-attributes"; import { IColumnsAttributes } from "./columns/columns-attributes";
import { DocumentGrid } from "./doc-grid/doc-grid"; import { DocumentGrid } from "./doc-grid/doc-grid";
import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes";
import { FooterReference } from "./footer-reference/footer-reference";
import { HeaderReference } from "./header-reference/header-reference"; import { HeaderReference } from "./header-reference/header-reference";
import { PageMargin } from "./page-margin/page-margin"; import { PageMargin } from "./page-margin/page-margin";
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes"; 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 Columns(mergedOptions.space));
this.root.push(new DocumentGrid(mergedOptions.linePitch)); this.root.push(new DocumentGrid(mergedOptions.linePitch));
this.root.push(new HeaderReference()); this.root.push(new HeaderReference());
this.root.push(new FooterReference());
} }
} }

View File

@ -1,5 +1,6 @@
import { Document } from "./document"; import { Document } from "./document";
import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties"; import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties";
import { Footer } from "./footer/footer";
import { Header } from "./header/header"; import { Header } from "./header/header";
import { Media } from "./media"; import { Media } from "./media";
import { Numbering } from "./numbering"; import { Numbering } from "./numbering";
@ -18,6 +19,7 @@ export class File {
private readonly media: Media; private readonly media: Media;
private readonly relationships: Relationships; private readonly relationships: Relationships;
private readonly header: Header; private readonly header: Header;
private readonly footer: Footer;
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
this.document = new Document(sectionPropertiesOptions); this.document = new Document(sectionPropertiesOptions);
@ -37,6 +39,7 @@ export class File {
this.relationships = new Relationships(); this.relationships = new Relationships();
this.media = new Media(); this.media = new Media();
this.header = new Header(); this.header = new Header();
this.footer = new Footer();
} }
public addParagraph(paragraph: Paragraph): void { public addParagraph(paragraph: Paragraph): void {
@ -92,4 +95,8 @@ export class File {
public get Header(): Header { public get Header(): Header {
return this.header; return this.header;
} }
public get Footer(): Footer {
return this.footer;
}
} }

View File

@ -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<IFooterAttributesProperties> {
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",
};
}

66
src/file/footer/footer.ts Normal file
View File

@ -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;
}
}

View File

@ -1,31 +1,53 @@
import { XmlAttributeComponent } from "file/xml-components"; import { XmlAttributeComponent } from "file/xml-components";
export interface IHeaderAttributesProperties { export interface IHeaderAttributesProperties {
wpc?: string;
mc?: string;
o?: string; o?: string;
r?: string; r?: string;
m?: string;
v?: string; v?: string;
w?: string;
w10?: string;
wp?: string;
wps?: string;
wpg?: string;
mc?: string;
wp14?: string; wp14?: string;
wp?: string;
w10?: string;
w?: string;
w14?: 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<IHeaderAttributesProperties> { export class HeaderAttributes extends XmlAttributeComponent<IHeaderAttributesProperties> {
protected xmlKeys = { protected xmlKeys = {
wpc: "xmlns:wpc",
mc: "xmlns:mc",
o: "xmlns:o", o: "xmlns:o",
r: "xmlns:r", r: "xmlns:r",
m: "xmlns:m",
v: "xmlns:v", v: "xmlns:v",
w: "xmlns:w",
w10: "xmlns:w10",
wp: "xmlns:wp",
wps: "xmlns:wps",
wpg: "xmlns:wpg",
mc: "xmlns:mc",
wp14: "xmlns:wp14", wp14: "xmlns:wp14",
wp: "xmlns:wp",
w10: "xmlns:w10",
w: "xmlns:w",
w14: "xmlns:w14", 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",
}; };
} }

View File

@ -10,17 +10,22 @@ export class Header extends XmlComponent {
super("w:hdr"); super("w:hdr");
this.root.push( this.root.push(
new HeaderAttributes({ 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", o: "urn:schemas-microsoft-com:office:office",
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships", r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
v: "urn:schemas-microsoft-com:vml", 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", 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", 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",
}), }),
); );
} }

View File

@ -9,7 +9,8 @@ export type RelationshipType =
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" | "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" | "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 { export class Relationship extends XmlComponent {
constructor(id: string, type: RelationshipType, target: string) { constructor(id: string, type: RelationshipType, target: string) {

View File

@ -14,6 +14,7 @@ export class Relationships extends XmlComponent {
this.createRelationship(1, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "styles.xml"); 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(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(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 { public addRelationship(relationship: Relationship): void {

View File

@ -9,6 +9,7 @@
<Default Extension="xml" ContentType="application/xml" /> <Default Extension="xml" ContentType="application/xml" />
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" /> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
<Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/> <Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/>
<Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/>
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml" /> <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml" />
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" /> <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" /> <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />