Add header objects and demo

This commit is contained in:
Dolan
2018-01-29 02:56:35 +00:00
parent 079334f71b
commit 8b8c664f0f
9 changed files with 54 additions and 15 deletions

13
.vscode/settings.json vendored
View File

@ -1,16 +1,9 @@
{ {
"cSpell.words": [ "cSpell.words": ["clippy", "docx", "dolan", "miu", "officegen", "typedoc"],
"clippy",
"docx",
"dolan",
"miu",
"officegen",
"typedoc"
],
"prettier.trailingComma": "all", "prettier.trailingComma": "all",
"prettier.printWidth": 140, "prettier.printWidth": 140,
"editor.formatOnSave": true, "editor.formatOnSave": false,
"prettier.tabWidth": 4, "prettier.tabWidth": 4,
"prettier.arrowParens": "always", "prettier.arrowParens": "always",
"prettier.bracketSpacing": true, "prettier.bracketSpacing": true
} }

12
demo/demo8.js Normal file
View File

@ -0,0 +1,12 @@
const docx = require('../build');
var doc = new docx.File();
doc.createParagraph("Hello World");
doc.Header.createParagraph("Header text");
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');
console.log('Document created succesfully at project root!');

View File

@ -42,6 +42,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));
this.archive.append(xmlDocument, { this.archive.append(xmlDocument, {
name: "word/document.xml", name: "word/document.xml",
@ -59,6 +60,10 @@ export class Compiler {
name: "word/numbering.xml", name: "word/numbering.xml",
}); });
this.archive.append(xmlHeader, {
name: "word/header1.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 IHeaderReferenceAttributes {
type: string;
id: string;
}
export class HeaderReferenceAttributes extends XmlAttributeComponent<IHeaderReferenceAttributes> {
protected xmlKeys = {
type: "w:type",
id: "r:id",
};
}

View File

@ -0,0 +1,14 @@
import { XmlComponent } from "file/xml-components";
import { HeaderReferenceAttributes } from "./header-reference-attributes";
export class HeaderReference extends XmlComponent {
constructor() {
super("w:headerReference");
this.root.push(
new HeaderReferenceAttributes({
type: "default",
id: `rId${3}`,
}),
);
}
}

View File

@ -1,13 +1,14 @@
// http://officeopenxml.com/WPsection.php // http://officeopenxml.com/WPsection.php
import { IColumnsAttributes } from "file/document/body/section-properties/columns/columns-attributes";
import { IPageMarginAttributes } from "file/document/body/section-properties/page-margin/page-margin-attributes";
import { IPageSizeAttributes } from "file/document/body/section-properties/page-size/page-size-attributes";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Columns } from "./columns/columns"; import { Columns } from "./columns/columns";
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 { 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 { PageSize } from "./page-size/page-size"; import { PageSize } from "./page-size/page-size";
import { IPageSizeAttributes } from "./page-size/page-size-attributes";
export type SectionPropertiesOptions = IPageSizeAttributes & IPageMarginAttributes & IColumnsAttributes & IDocGridAttributesProperties; export type SectionPropertiesOptions = IPageSizeAttributes & IPageMarginAttributes & IColumnsAttributes & IDocGridAttributesProperties;
@ -49,5 +50,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());
} }
} }

View File

@ -44,7 +44,7 @@ export class File {
} }
public createParagraph(text?: string): Paragraph { public createParagraph(text?: string): Paragraph {
return this.document.createParagraph(); return this.document.createParagraph(text);
} }
public addTable(table: Table): void { public addTable(table: Table): void {

View File

@ -2,7 +2,6 @@ import * as fs from "fs";
import * as sizeOf from "image-size"; import * as sizeOf from "image-size";
import * as path from "path"; import * as path from "path";
import { Relationships } from "file/relationships/relationships";
import { IMediaData } from "./data"; import { IMediaData } from "./data";
export class Media { export class Media {

View File

@ -8,6 +8,7 @@
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" /> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<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/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" />