|
| 1 | +// WPS (WordProcessing Shape) text boxes - modern DrawingML-based alternative to legacy VML text boxes (demo 94) |
| 2 | +// Demonstrates: basic text box, styled fill/outline, rotation, floating positioning, and vertical alignment |
| 3 | +import * as fs from "fs"; |
| 4 | + |
| 5 | +import { |
| 6 | + Document, |
| 7 | + HorizontalPositionRelativeFrom, |
| 8 | + Packer, |
| 9 | + Paragraph, |
| 10 | + TextRun, |
| 11 | + VerticalAnchor, |
| 12 | + VerticalPositionRelativeFrom, |
| 13 | + WpsShapeRun, |
| 14 | +} from "docx"; |
| 15 | + |
| 16 | +const doc = new Document({ |
| 17 | + sections: [ |
| 18 | + { |
| 19 | + children: [ |
| 20 | + // 1. Basic text box - minimal options |
| 21 | + new Paragraph({ |
| 22 | + children: [ |
| 23 | + new WpsShapeRun({ |
| 24 | + type: "wps", |
| 25 | + children: [ |
| 26 | + new Paragraph({ |
| 27 | + children: [new TextRun("This is a basic WPS text box with just width and height.")], |
| 28 | + }), |
| 29 | + ], |
| 30 | + transformation: { |
| 31 | + width: 4000000, |
| 32 | + height: 800000, |
| 33 | + }, |
| 34 | + }), |
| 35 | + ], |
| 36 | + }), |
| 37 | + |
| 38 | + new Paragraph({ children: [new TextRun("")] }), |
| 39 | + |
| 40 | + // 2. Styled text box - solid fill and outline |
| 41 | + new Paragraph({ |
| 42 | + children: [ |
| 43 | + new WpsShapeRun({ |
| 44 | + type: "wps", |
| 45 | + children: [ |
| 46 | + new Paragraph({ |
| 47 | + children: [ |
| 48 | + new TextRun({ |
| 49 | + text: "Styled text box with a light blue background and dark blue border.", |
| 50 | + bold: true, |
| 51 | + color: "1F3864", |
| 52 | + }), |
| 53 | + ], |
| 54 | + }), |
| 55 | + ], |
| 56 | + transformation: { |
| 57 | + width: 4000000, |
| 58 | + height: 800000, |
| 59 | + }, |
| 60 | + solidFill: { |
| 61 | + type: "rgb", |
| 62 | + value: "D6E4F0", |
| 63 | + }, |
| 64 | + outline: { |
| 65 | + type: "solidFill", |
| 66 | + solidFillType: "rgb", |
| 67 | + value: "2E74B5", |
| 68 | + width: 25400, |
| 69 | + }, |
| 70 | + }), |
| 71 | + ], |
| 72 | + }), |
| 73 | + |
| 74 | + new Paragraph({ children: [new TextRun("")] }), |
| 75 | + |
| 76 | + // 3. Rotated text box |
| 77 | + new Paragraph({ |
| 78 | + children: [ |
| 79 | + new WpsShapeRun({ |
| 80 | + type: "wps", |
| 81 | + children: [ |
| 82 | + new Paragraph({ |
| 83 | + children: [new TextRun("This text box is rotated 15 degrees.")], |
| 84 | + }), |
| 85 | + ], |
| 86 | + transformation: { |
| 87 | + width: 3000000, |
| 88 | + height: 600000, |
| 89 | + rotation: 15, |
| 90 | + }, |
| 91 | + outline: { |
| 92 | + type: "solidFill", |
| 93 | + solidFillType: "rgb", |
| 94 | + value: "FF0000", |
| 95 | + width: 12700, |
| 96 | + }, |
| 97 | + }), |
| 98 | + ], |
| 99 | + }), |
| 100 | + |
| 101 | + new Paragraph({ children: [new TextRun("")] }), |
| 102 | + new Paragraph({ children: [new TextRun("")] }), |
| 103 | + |
| 104 | + // 4. Floating text box - positioned via offset |
| 105 | + new Paragraph({ |
| 106 | + children: [ |
| 107 | + new WpsShapeRun({ |
| 108 | + type: "wps", |
| 109 | + children: [ |
| 110 | + new Paragraph({ |
| 111 | + children: [new TextRun("Floating text box positioned at a specific offset on the page.")], |
| 112 | + }), |
| 113 | + ], |
| 114 | + transformation: { |
| 115 | + width: 3500000, |
| 116 | + height: 700000, |
| 117 | + }, |
| 118 | + outline: { |
| 119 | + type: "solidFill", |
| 120 | + solidFillType: "rgb", |
| 121 | + value: "70AD47", |
| 122 | + width: 19050, |
| 123 | + }, |
| 124 | + floating: { |
| 125 | + horizontalPosition: { |
| 126 | + relative: HorizontalPositionRelativeFrom.PAGE, |
| 127 | + offset: 3500000, |
| 128 | + }, |
| 129 | + verticalPosition: { |
| 130 | + relative: VerticalPositionRelativeFrom.PARAGRAPH, |
| 131 | + offset: 100000, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }), |
| 135 | + ], |
| 136 | + }), |
| 137 | + |
| 138 | + new Paragraph({ children: [new TextRun("")] }), |
| 139 | + new Paragraph({ children: [new TextRun("")] }), |
| 140 | + new Paragraph({ children: [new TextRun("")] }), |
| 141 | + |
| 142 | + // 5. Centered vertical alignment with custom margins |
| 143 | + new Paragraph({ |
| 144 | + children: [ |
| 145 | + new WpsShapeRun({ |
| 146 | + type: "wps", |
| 147 | + children: [ |
| 148 | + new Paragraph({ |
| 149 | + children: [new TextRun("Vertically centered text with custom margins.")], |
| 150 | + }), |
| 151 | + ], |
| 152 | + transformation: { |
| 153 | + width: 4000000, |
| 154 | + height: 1200000, |
| 155 | + }, |
| 156 | + solidFill: { |
| 157 | + type: "rgb", |
| 158 | + value: "FFF2CC", |
| 159 | + }, |
| 160 | + outline: { |
| 161 | + type: "solidFill", |
| 162 | + solidFillType: "rgb", |
| 163 | + value: "BF8F00", |
| 164 | + width: 19050, |
| 165 | + }, |
| 166 | + bodyProperties: { |
| 167 | + verticalAnchor: VerticalAnchor.CENTER, |
| 168 | + margins: { |
| 169 | + top: 72000, |
| 170 | + bottom: 72000, |
| 171 | + left: 144000, |
| 172 | + right: 144000, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }), |
| 176 | + ], |
| 177 | + }), |
| 178 | + ], |
| 179 | + }, |
| 180 | + ], |
| 181 | +}); |
| 182 | + |
| 183 | +Packer.toBuffer(doc).then((buffer) => { |
| 184 | + fs.writeFileSync("My Document.docx", buffer); |
| 185 | +}); |
0 commit comments