deno.land / x / docx@8.5.0 / demo / 5-images.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154// Example of how to add images to the document - You can use Buffers, UInt8Arrays or Base64 strings
import * as fs from "fs";import { convertMillimetersToTwip, Document, HorizontalPositionAlign, HorizontalPositionRelativeFrom, ImageRun, Packer, Paragraph, VerticalPositionAlign, VerticalPositionRelativeFrom,} from "docx";
const doc = new Document({ sections: [ { children: [ new Paragraph("Hello World"), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/image1.jpeg"), transformation: { width: 100, height: 100, }, altText: { title: "This is an ultimate title", description: "This is an ultimate image", name: "My Ultimate Image", }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/dog.png").toString("base64"), transformation: { width: 100, height: 100, }, outline: { type: "solidFill", solidFillType: "rgb", value: "FF0000", }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/cat.jpg"), transformation: { width: 100, height: 100, flip: { vertical: true, }, }, outline: { type: "solidFill", solidFillType: "rgb", value: "0000FF", width: convertMillimetersToTwip(600), }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/parrots.bmp"), transformation: { width: 150, height: 150, flip: { horizontal: true, }, rotation: 225, }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/pizza.gif"), transformation: { width: 200, height: 200, flip: { horizontal: true, vertical: true, }, }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/pizza.gif"), transformation: { width: 200, height: 200, rotation: 45, }, floating: { zIndex: 10, horizontalPosition: { offset: 1014400, }, verticalPosition: { offset: 1014400, }, }, }), ], }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/cat.jpg"), transformation: { width: 200, height: 200, }, floating: { zIndex: 5, horizontalPosition: { relative: HorizontalPositionRelativeFrom.PAGE, align: HorizontalPositionAlign.RIGHT, }, verticalPosition: { relative: VerticalPositionRelativeFrom.PAGE, align: VerticalPositionAlign.BOTTOM, }, }, }), ], }), ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info