Files
docx-js/demo/61-text-frame.ts

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-03-14 17:00:42 +00:00
// Text Frame (Text Box) example
2023-06-05 00:33:43 +01:00
2021-03-14 17:00:42 +00:00
import * as fs from "fs";
import {
BorderStyle,
Document,
FrameAnchorType,
HorizontalPositionAlign,
Packer,
Paragraph,
2022-10-29 15:10:02 +01:00
Tab,
TextRun,
VerticalPositionAlign,
2023-06-01 02:05:35 +01:00
} from "docx";
2021-03-14 17:00:42 +00:00
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [
{
properties: {},
2021-03-14 17:00:42 +00:00
children: [
2021-03-19 20:53:56 +00:00
new Paragraph({
frame: {
position: {
x: 1000,
y: 3000,
},
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,
2021-03-19 20:53:56 +00:00
size: 6,
},
bottom: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
2021-03-19 20:53:56 +00:00
size: 6,
},
left: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
2021-03-19 20:53:56 +00:00
size: 6,
},
right: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
2021-03-19 20:53:56 +00:00
size: 6,
},
},
children: [
new TextRun("Hello World"),
new TextRun({
text: "Foo Bar",
bold: true,
}),
new TextRun({
2022-10-29 15:10:02 +01:00
children: [new Tab(), "Github is the best"],
2021-03-19 20:53:56 +00:00
bold: true,
}),
],
2021-03-14 17:00:42 +00:00
}),
],
2021-03-19 20:53:56 +00:00
},
2021-03-14 17:00:42 +00:00
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});