Change image API so that it now sends Images
This commit is contained in:
@ -6,8 +6,8 @@ var doc = new docx.Document();
|
|||||||
const table = doc.createTable(4, 4);
|
const table = doc.createTable(4, 4);
|
||||||
table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
|
table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
|
||||||
|
|
||||||
const imageData = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
|
const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg");
|
||||||
table.getCell(1, 1).addContent(new docx.Image(imageData));
|
table.getCell(1, 1).addContent(image);
|
||||||
|
|
||||||
var exporter = new docx.LocalPacker(doc);
|
var exporter = new docx.LocalPacker(doc);
|
||||||
exporter.pack('My Document');
|
exporter.pack('My Document');
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// http://officeopenxml.com/WPdocument.php
|
// http://officeopenxml.com/WPdocument.php
|
||||||
import { IMediaData } from "file/media";
|
|
||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { Image, Paragraph } from "../paragraph";
|
import { Paragraph } from "../paragraph";
|
||||||
import { Table } from "../table";
|
import { Table } from "../table";
|
||||||
import { Body } from "./body";
|
import { Body } from "./body";
|
||||||
import { SectionPropertiesOptions } from "./body/section-properties/section-properties";
|
import { SectionPropertiesOptions } from "./body/section-properties/section-properties";
|
||||||
@ -37,8 +36,9 @@ export class Document extends XmlComponent {
|
|||||||
this.root.push(this.body);
|
this.root.push(this.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addParagraph(paragraph: Paragraph): void {
|
public addParagraph(paragraph: Paragraph): Document {
|
||||||
this.body.push(paragraph);
|
this.body.push(paragraph);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createParagraph(text?: string): Paragraph {
|
public createParagraph(text?: string): Paragraph {
|
||||||
@ -57,13 +57,6 @@ export class Document extends XmlComponent {
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createDrawing(imageData: IMediaData): Image {
|
|
||||||
const image = new Image(imageData);
|
|
||||||
this.addParagraph(image);
|
|
||||||
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
get Body(): Body {
|
get Body(): Body {
|
||||||
return this.body;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { IMediaData } from "file/media";
|
|
||||||
import { AppProperties } from "./app-properties/app-properties";
|
import { AppProperties } from "./app-properties/app-properties";
|
||||||
import { ContentTypes } from "./content-types/content-types";
|
import { ContentTypes } from "./content-types/content-types";
|
||||||
import { CoreProperties, IPropertiesOptions } from "./core-properties";
|
import { CoreProperties, IPropertiesOptions } from "./core-properties";
|
||||||
@ -126,18 +125,22 @@ export class File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public createImage(filePath: string): Image {
|
public createImage(filePath: string): Image {
|
||||||
const mediaData = Media.addImage(this, filePath);
|
const image = Media.addImage(this, filePath);
|
||||||
return this.document.createDrawing(mediaData);
|
this.document.addParagraph(image);
|
||||||
|
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
public insertImage(mediaData: IMediaData): File {
|
public insertImage(image: Image): File {
|
||||||
this.document.createDrawing(mediaData);
|
this.document.addParagraph(image);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image {
|
public createImageFromBuffer(buffer: Buffer, width?: number, height?: number): Image {
|
||||||
const mediaData = Media.addImageFromBuffer(this, buffer, width, height);
|
const image = Media.addImageFromBuffer(this, buffer, width, height);
|
||||||
return this.document.createDrawing(mediaData);
|
this.document.addParagraph(image);
|
||||||
|
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createHyperlink(link: string, text?: string): Hyperlink {
|
public createHyperlink(link: string, text?: string): Hyperlink {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { Footer } from "./footer/footer";
|
import { Footer } from "./footer/footer";
|
||||||
import { IMediaData, Media } from "./media";
|
import { Media } from "./media";
|
||||||
import { Paragraph } from "./paragraph";
|
import { Image, Paragraph } from "./paragraph";
|
||||||
import { Relationships } from "./relationships";
|
import { Relationships } from "./relationships";
|
||||||
import { Table } from "./table";
|
import { Table } from "./table";
|
||||||
|
|
||||||
@ -32,10 +32,6 @@ export class FooterWrapper {
|
|||||||
return this.footer.createTable(rows, cols);
|
return this.footer.createTable(rows, cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addDrawing(imageData: IMediaData): void {
|
|
||||||
this.footer.addDrawing(imageData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public addChildElement(childElement: XmlComponent | string): void {
|
public addChildElement(childElement: XmlComponent | string): void {
|
||||||
this.footer.addChildElement(childElement);
|
this.footer.addChildElement(childElement);
|
||||||
}
|
}
|
||||||
@ -47,7 +43,12 @@ export class FooterWrapper {
|
|||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||||
`media/${mediaData.fileName}`,
|
`media/${mediaData.fileName}`,
|
||||||
);
|
);
|
||||||
this.addDrawing(mediaData);
|
this.insertImage(new Image(mediaData));
|
||||||
|
}
|
||||||
|
|
||||||
|
public insertImage(image: Image): FooterWrapper {
|
||||||
|
this.footer.addParagraph(image);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get Footer(): Footer {
|
public get Footer(): Footer {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// http://officeopenxml.com/WPfooters.php
|
// http://officeopenxml.com/WPfooters.php
|
||||||
import { IMediaData } from "file/media";
|
|
||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { Paragraph, PictureRun } from "../paragraph";
|
import { Paragraph } from "../paragraph";
|
||||||
import { Table } from "../table";
|
import { Table } from "../table";
|
||||||
import { FooterAttributes } from "./footer-attributes";
|
import { FooterAttributes } from "./footer-attributes";
|
||||||
|
|
||||||
@ -36,8 +35,10 @@ export class Footer extends XmlComponent {
|
|||||||
return this.refId;
|
return this.refId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addParagraph(paragraph: Paragraph): void {
|
public addParagraph(paragraph: Paragraph): Footer {
|
||||||
this.root.push(paragraph);
|
this.root.push(paragraph);
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createParagraph(text?: string): Paragraph {
|
public createParagraph(text?: string): Paragraph {
|
||||||
@ -55,12 +56,4 @@ export class Footer extends XmlComponent {
|
|||||||
this.addTable(table);
|
this.addTable(table);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addDrawing(imageData: IMediaData): void {
|
|
||||||
const paragraph = new Paragraph();
|
|
||||||
const run = new PictureRun(imageData);
|
|
||||||
paragraph.addRun(run);
|
|
||||||
|
|
||||||
this.root.push(paragraph);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { Header } from "./header/header";
|
import { Header } from "./header/header";
|
||||||
import { IMediaData, Media } from "./media";
|
import { Media } from "./media";
|
||||||
import { Paragraph } from "./paragraph";
|
import { Image, Paragraph } from "./paragraph";
|
||||||
import { Relationships } from "./relationships";
|
import { Relationships } from "./relationships";
|
||||||
import { Table } from "./table";
|
import { Table } from "./table";
|
||||||
|
|
||||||
@ -32,10 +32,6 @@ export class HeaderWrapper {
|
|||||||
return this.header.createTable(rows, cols);
|
return this.header.createTable(rows, cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addDrawing(imageData: IMediaData): void {
|
|
||||||
this.header.addDrawing(imageData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public addChildElement(childElement: XmlComponent | string): void {
|
public addChildElement(childElement: XmlComponent | string): void {
|
||||||
this.header.addChildElement(childElement);
|
this.header.addChildElement(childElement);
|
||||||
}
|
}
|
||||||
@ -47,7 +43,12 @@ export class HeaderWrapper {
|
|||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||||
`media/${mediaData.fileName}`,
|
`media/${mediaData.fileName}`,
|
||||||
);
|
);
|
||||||
this.addDrawing(mediaData);
|
this.insertImage(new Image(mediaData));
|
||||||
|
}
|
||||||
|
|
||||||
|
public insertImage(image: Image): HeaderWrapper {
|
||||||
|
this.header.addParagraph(image);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get Header(): Header {
|
public get Header(): Header {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// http://officeopenxml.com/WPheaders.php
|
// http://officeopenxml.com/WPheaders.php
|
||||||
import { IMediaData } from "file/media";
|
|
||||||
import { XmlComponent } from "file/xml-components";
|
import { XmlComponent } from "file/xml-components";
|
||||||
import { Paragraph, PictureRun } from "../paragraph";
|
import { Paragraph } from "../paragraph";
|
||||||
import { Table } from "../table";
|
import { Table } from "../table";
|
||||||
import { HeaderAttributes } from "./header-attributes";
|
import { HeaderAttributes } from "./header-attributes";
|
||||||
|
|
||||||
@ -55,12 +54,4 @@ export class Header extends XmlComponent {
|
|||||||
this.addTable(table);
|
this.addTable(table);
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addDrawing(imageData: IMediaData): void {
|
|
||||||
const paragraph = new Paragraph();
|
|
||||||
const run = new PictureRun(imageData);
|
|
||||||
paragraph.addRun(run);
|
|
||||||
|
|
||||||
this.root.push(paragraph);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import * as sizeOf from "image-size";
|
|||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
import { File } from "../file";
|
import { File } from "../file";
|
||||||
|
import { Image } from "../paragraph";
|
||||||
import { IMediaData } from "./data";
|
import { IMediaData } from "./data";
|
||||||
|
|
||||||
interface IHackedFile {
|
interface IHackedFile {
|
||||||
@ -10,7 +11,7 @@ interface IHackedFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Media {
|
export class Media {
|
||||||
public static addImage(file: File, filePath: string): IMediaData {
|
public static addImage(file: File, filePath: string): Image {
|
||||||
// Workaround to expose id without exposing to API
|
// Workaround to expose id without exposing to API
|
||||||
const exposedFile = (file as {}) as IHackedFile;
|
const exposedFile = (file as {}) as IHackedFile;
|
||||||
const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
|
const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
|
||||||
@ -19,10 +20,10 @@ export class Media {
|
|||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||||
`media/${mediaData.fileName}`,
|
`media/${mediaData.fileName}`,
|
||||||
);
|
);
|
||||||
return mediaData;
|
return new Image(mediaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): IMediaData {
|
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): Image {
|
||||||
// Workaround to expose id without exposing to API
|
// Workaround to expose id without exposing to API
|
||||||
const exposedFile = (file as {}) as IHackedFile;
|
const exposedFile = (file as {}) as IHackedFile;
|
||||||
const mediaData = file.Media.addMediaFromBuffer(
|
const mediaData = file.Media.addMediaFromBuffer(
|
||||||
@ -37,7 +38,8 @@ export class Media {
|
|||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||||
`media/${mediaData.fileName}`,
|
`media/${mediaData.fileName}`,
|
||||||
);
|
);
|
||||||
return mediaData;
|
|
||||||
|
return new Image(mediaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static generateId(): string {
|
private static generateId(): string {
|
||||||
|
Reference in New Issue
Block a user