Fix referenceId +1 error and spelling mistakes

Add demo
This commit is contained in:
Dolan
2018-01-30 01:16:48 +00:00
parent 989446ef36
commit c873abfe18
12 changed files with 46 additions and 21 deletions

View File

@ -13,4 +13,4 @@ doc.addParagraph(paragraph);
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document'); exporter.pack('My Document');
console.log('Document created succesfully at project root!'); console.log('Document created successfully at project root!');

View File

@ -70,3 +70,5 @@ para.createTextRun('and back to normal.');
const exporter = new docx.LocalPacker(doc); const exporter = new docx.LocalPacker(doc);
exporter.pack('My Document'); exporter.pack('My Document');
console.log('Document created successfully at project root!');

View File

@ -32,4 +32,4 @@ doc.addParagraph(subSubP);
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document'); exporter.pack('My Document');
console.log('Document created succesfully at project root!'); console.log('Document created successfully at project root!');

View File

@ -9,4 +9,4 @@ table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document'); exporter.pack('My Document');
console.log('Document created succesfully at project root!'); console.log('Document created successfully at project root!');

View File

@ -22,4 +22,4 @@ doc.createParagraph("5th Dec 2015");
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack("My Document"); exporter.pack("My Document");
console.log("Document created succesfully at project root!"); console.log("Document created successfully at project root!");

View File

@ -11,4 +11,4 @@ doc.addParagraph(paragraph);
var exporter = new docx.LocalPacker(doc); var exporter = new docx.LocalPacker(doc);
exporter.pack("My Document"); exporter.pack("My Document");
console.log("Document created succesfully at project root!"); console.log("Document created successfully at project root!");

View File

@ -10,4 +10,4 @@ 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');
console.log('Document created succesfully at project root!'); console.log('Document created successfully at project root!');

13
demo/demo9.js Normal file
View File

@ -0,0 +1,13 @@
const docx = require('../build');
var doc = new docx.File();
doc.createParagraph("Hello World");
doc.Header.createImage("./demo/images/pizza.gif");
// doc.Footer.createImage("./demo/images/pizza.gif");
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');
console.log('Document created successfully at project root!');

View File

@ -38,8 +38,8 @@ export class File {
this.numbering = new Numbering(); this.numbering = new Numbering();
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.media, this.relationships);
this.footer = new Footer(); this.footer = new Footer(this.media, this.relationships);
} }
public addParagraph(paragraph: Paragraph): void { public addParagraph(paragraph: Paragraph): void {

View File

@ -1,12 +1,13 @@
// http://officeopenxml.com/WPfooters.php // http://officeopenxml.com/WPfooters.php
import { IMediaData } from "file/media"; import { IMediaData, Media } from "file/media";
import { Relationships } from "file/relationships";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph"; import { Paragraph, PictureRun } from "../paragraph";
import { Table } from "../table"; import { Table } from "../table";
import { FooterAttributes } from "./footer-attributes"; import { FooterAttributes } from "./footer-attributes";
export class Footer extends XmlComponent { export class Footer extends XmlComponent {
constructor() { constructor(private readonly media: Media, private readonly relationships: Relationships) {
super("w:ftr"); super("w:ftr");
this.root.push( this.root.push(
new FooterAttributes({ new FooterAttributes({
@ -58,9 +59,13 @@ export class Footer extends XmlComponent {
this.root.push(paragraph); this.root.push(paragraph);
} }
public createDrawing(imageData: IMediaData): void { public createImage(image: string): void {
this.addDrawing(imageData); const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
return; mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
} }
} }

View File

@ -1,12 +1,13 @@
// http://officeopenxml.com/WPheaders.php // http://officeopenxml.com/WPheaders.php
import { IMediaData } from "file/media"; import { IMediaData, Media } from "file/media";
import { Relationships } from "file/relationships";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph"; import { Paragraph, PictureRun } from "../paragraph";
import { Table } from "../table"; import { Table } from "../table";
import { HeaderAttributes } from "./header-attributes"; import { HeaderAttributes } from "./header-attributes";
export class Header extends XmlComponent { export class Header extends XmlComponent {
constructor() { constructor(private readonly media: Media, private readonly relationships: Relationships) {
super("w:hdr"); super("w:hdr");
this.root.push( this.root.push(
new HeaderAttributes({ new HeaderAttributes({
@ -58,9 +59,13 @@ export class Header extends XmlComponent {
this.root.push(paragraph); this.root.push(paragraph);
} }
public createDrawing(imageData: IMediaData): void { public createImage(image: string): void {
this.addDrawing(imageData); const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
return; mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
} }
} }

View File

@ -26,7 +26,7 @@ export class Media {
const dimensions = sizeOf(filePath); const dimensions = sizeOf(filePath);
const imageData = { const imageData = {
referenceId: this.map.size + relationshipsCount, referenceId: this.map.size + relationshipsCount + 1,
stream: fs.createReadStream(filePath), stream: fs.createReadStream(filePath),
path: filePath, path: filePath,
fileName: key, fileName: key,