deno.land / x / docx@8.5.0 / demo / 61-text-frame.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187// Text Frame (Text Box) example
import * as fs from "fs";import { AlignmentType, BorderStyle, Document, FrameAnchorType, HorizontalPositionAlign, Packer, Paragraph, Tab, TextRun, VerticalPositionAlign,} from "docx";
const doc = new Document({ sections: [ { properties: {}, children: [ new Paragraph({ frame: { type: "absolute", position: { x: 1000, y: 3000, }, width: 4000, height: 1000, anchor: { horizontal: FrameAnchorType.MARGIN, vertical: FrameAnchorType.MARGIN, }, }, border: { top: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, bottom: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, left: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, right: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, }, children: [ new TextRun("Hello World"), new TextRun({ text: "Foo Bar", bold: true, }), new TextRun({ children: [new Tab(), "Github is the best"], bold: true, }), ], }), new Paragraph({ frame: { type: "alignment", width: 4000, height: 1000, anchor: { horizontal: FrameAnchorType.MARGIN, vertical: FrameAnchorType.MARGIN, }, alignment: { x: HorizontalPositionAlign.CENTER, y: VerticalPositionAlign.TOP, }, }, border: { top: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, bottom: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, left: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, right: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, }, children: [ new TextRun("Hello World"), new TextRun({ text: "Foo Bar", bold: true, }), new TextRun({ children: [new Tab(), "Github is the best"], bold: true, }), ], }), new Paragraph({ frame: { type: "alignment", width: 4000, height: 1000, anchor: { horizontal: FrameAnchorType.MARGIN, vertical: FrameAnchorType.MARGIN, }, alignment: { x: HorizontalPositionAlign.CENTER, y: VerticalPositionAlign.BOTTOM, }, }, border: { top: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, bottom: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, left: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, right: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6, }, }, alignment: AlignmentType.RIGHT, children: [ new TextRun("Hello World"), new TextRun({ text: "Foo Bar", bold: true, }), new TextRun({ children: [new Tab(), "Github is the best"], bold: true, }), ], }), ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info