2020-10-10 13:41:26 +01:00
|
|
|
|
// Simple example to add text to a document
|
2023-06-05 00:33:43 +01:00
|
|
|
|
|
2020-10-10 13:41:26 +01:00
|
|
|
|
import * as fs from "fs";
|
|
|
|
|
import {
|
|
|
|
|
Document,
|
|
|
|
|
Math,
|
|
|
|
|
MathAngledBrackets,
|
|
|
|
|
MathCurlyBrackets,
|
|
|
|
|
MathFraction,
|
|
|
|
|
MathFunction,
|
|
|
|
|
MathPreSubSuperScript,
|
|
|
|
|
MathRadical,
|
|
|
|
|
MathRoundBrackets,
|
|
|
|
|
MathRun,
|
|
|
|
|
MathSquareBrackets,
|
|
|
|
|
MathSubScript,
|
|
|
|
|
MathSubSuperScript,
|
|
|
|
|
MathSum,
|
2022-10-08 12:56:35 +11:00
|
|
|
|
MathIntegral,
|
2020-10-10 13:41:26 +01:00
|
|
|
|
MathSuperScript,
|
|
|
|
|
Packer,
|
|
|
|
|
Paragraph,
|
|
|
|
|
TextRun,
|
2024-01-03 08:13:13 +08:00
|
|
|
|
MathLimitLower,
|
|
|
|
|
MathLimitUpper,
|
2023-06-05 00:33:43 +01:00
|
|
|
|
} from "docx";
|
2020-10-10 13:41:26 +01:00
|
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
|
const doc = new Document({
|
|
|
|
|
sections: [
|
|
|
|
|
{
|
|
|
|
|
properties: {},
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathRun("2+2"),
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("hi")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
new TextRun({
|
|
|
|
|
text: "Foo Bar",
|
|
|
|
|
bold: true,
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-13 03:00:14 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [
|
|
|
|
|
new MathRun("1"),
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
denominator: [new MathRun("2")],
|
2020-10-13 03:00:14 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathSum({
|
|
|
|
|
children: [new MathRun("test")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathSum({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSuperScript({
|
|
|
|
|
children: [new MathRun("e")],
|
|
|
|
|
superScript: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
subScript: [new MathRun("i")],
|
|
|
|
|
}),
|
|
|
|
|
new MathSum({
|
|
|
|
|
children: [
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [new MathRun("i")],
|
2022-10-08 12:56:35 +11:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
subScript: [new MathRun("i")],
|
|
|
|
|
superScript: [new MathRun("10")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
new Paragraph({
|
|
|
|
|
children: [
|
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathIntegral({
|
|
|
|
|
children: [new MathRun("test")],
|
|
|
|
|
}),
|
|
|
|
|
new MathIntegral({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSuperScript({
|
|
|
|
|
children: [new MathRun("e")],
|
|
|
|
|
superScript: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
subScript: [new MathRun("i")],
|
|
|
|
|
}),
|
|
|
|
|
new MathIntegral({
|
|
|
|
|
children: [
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [new MathRun("i")],
|
2021-03-19 20:53:56 +00:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
subScript: [new MathRun("i")],
|
|
|
|
|
superScript: [new MathRun("10")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSuperScript({
|
|
|
|
|
children: [new MathRun("test")],
|
|
|
|
|
superScript: [new MathRun("hello")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSubScript({
|
|
|
|
|
children: [new MathRun("test")],
|
|
|
|
|
subScript: [new MathRun("hello")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSubScript({
|
|
|
|
|
children: [new MathRun("x")],
|
|
|
|
|
subScript: [
|
|
|
|
|
new MathSuperScript({
|
|
|
|
|
children: [new MathRun("y")],
|
|
|
|
|
superScript: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSubSuperScript({
|
|
|
|
|
children: [new MathRun("test")],
|
|
|
|
|
superScript: [new MathRun("hello")],
|
|
|
|
|
subScript: [new MathRun("world")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathPreSubSuperScript({
|
|
|
|
|
children: [new MathRun("test")],
|
|
|
|
|
superScript: [new MathRun("hello")],
|
|
|
|
|
subScript: [new MathRun("world")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathSubScript({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
|
subScript: [new MathRun("4")],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
2020-10-13 02:06:27 +01:00
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathSubScript({
|
|
|
|
|
children: [
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
degree: [new MathRun("4")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
subScript: [new MathRun("x")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [new MathRun("4")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathFunction({
|
|
|
|
|
name: [
|
|
|
|
|
new MathSuperScript({
|
|
|
|
|
children: [new MathRun("cos")],
|
|
|
|
|
superScript: [new MathRun("-1")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
children: [new MathRun("100")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathRun("×"),
|
|
|
|
|
new MathFunction({
|
|
|
|
|
name: [new MathRun("sin")],
|
|
|
|
|
children: [new MathRun("360")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathRun("= x"),
|
2020-10-13 02:06:27 +01:00
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
new Paragraph({
|
|
|
|
|
children: [
|
|
|
|
|
new Math({
|
2020-10-13 02:06:27 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathRoundBrackets({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new MathSquareBrackets({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
new MathCurlyBrackets({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
new MathAngledBrackets({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [new MathRun("1")],
|
|
|
|
|
denominator: [new MathRun("2")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Paragraph({
|
2020-10-10 13:41:26 +01:00
|
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathFraction({
|
|
|
|
|
numerator: [
|
|
|
|
|
new MathRadical({
|
|
|
|
|
children: [new MathRun("4")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
denominator: [new MathRun("2a")],
|
2020-10-13 02:06:27 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
2020-10-10 13:41:26 +01:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2024-01-03 08:13:13 +08:00
|
|
|
|
new Paragraph({
|
|
|
|
|
children: [
|
|
|
|
|
new Math({
|
|
|
|
|
children: [
|
|
|
|
|
new MathLimitUpper({
|
|
|
|
|
children: [new MathRun("x")],
|
|
|
|
|
limit: [new MathRun("-")],
|
|
|
|
|
}),
|
|
|
|
|
new MathRun("="),
|
|
|
|
|
new MathLimitLower({
|
|
|
|
|
children: [new MathRun("lim")],
|
|
|
|
|
limit: [new MathRun("x→0")],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
2020-10-10 13:41:26 +01:00
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
|
},
|
2020-10-10 13:41:26 +01:00
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
|
});
|