Files
docx-js/demo/demo11.ts

194 lines
3.9 KiB
TypeScript
Raw Normal View History

2018-08-21 02:46:21 +01:00
// Setting styles with JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2019-06-13 01:07:00 +01:00
import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, Table } from "../build";
2018-03-10 23:04:45 +00:00
2018-08-21 02:46:21 +01:00
const doc = new Document(undefined, {
2018-03-10 23:04:45 +00:00
top: 700,
right: 700,
bottom: 700,
left: 700,
});
doc.Styles.createParagraphStyle("Heading1", "Heading 1")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.font("Calibri")
.size(52)
.center()
.bold()
2018-08-21 19:27:07 +01:00
.color("000000")
2018-03-10 23:04:45 +00:00
.spacing({ line: 340 })
.underline("single", "000000");
doc.Styles.createParagraphStyle("Heading2", "Heading 2")
.basedOn("Normal")
.next("Normal")
.font("Calibri")
.quickFormat()
.size(26)
.bold()
.spacing({ line: 340 });
doc.Styles.createParagraphStyle("Heading3", "Heading 3")
.basedOn("Normal")
.next("Normal")
.font("Calibri")
.quickFormat()
.size(26)
.bold()
.spacing({ line: 276 });
doc.Styles.createParagraphStyle("Heading4", "Heading 4")
.basedOn("Normal")
.next("Normal")
.justified()
.font("Calibri")
.size(26)
.bold();
doc.Styles.createParagraphStyle("normalPara", "Normal Para")
.basedOn("Normal")
.next("Normal")
.font("Calibri")
.quickFormat()
.leftTabStop(453.543307087)
2018-08-21 02:46:21 +01:00
.maxRightTabStop()
2018-03-10 23:04:45 +00:00
.size(26)
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
doc.Styles.createParagraphStyle("normalPara2", "Normal Para2")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.font("Calibri")
.size(26)
.justified()
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
doc.Styles.createParagraphStyle("aside", "Aside")
.basedOn("Normal")
.next("Normal")
.color("999999")
.italics()
2018-08-21 02:46:21 +01:00
.indent({ left: 720 })
2018-03-10 23:04:45 +00:00
.spacing({ line: 276 });
doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced")
.basedOn("Normal")
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
.quickFormat()
.basedOn("Normal");
2018-08-12 23:07:31 +01:00
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"));
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "HEADING",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER,
}),
);
2019-06-25 23:17:56 +01:00
doc.Footer.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "1",
style: "normalPara",
alignment: AlignmentType.RIGHT,
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Ref. :",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Date :",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "To,",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "The Superindenting Engineer,(O &M)",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Sub : ",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Ref. : ",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Sir,",
style: "normalPara",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "BRIEF DESCRIPTION",
style: "normalPara",
}),
);
2018-03-10 23:04:45 +00:00
2019-03-18 23:50:21 +00:00
const table = new Table({
rows: 4,
columns: 4,
});
2018-08-21 02:46:21 +01:00
table
2018-03-10 23:04:45 +00:00
.getRow(0)
.getCell(0)
2019-06-25 23:17:56 +01:00
.add(new Paragraph("Pole No."));
2018-12-29 01:57:20 +00:00
2019-06-25 23:17:56 +01:00
doc.add(table);
2018-03-10 23:04:45 +00:00
2018-08-21 02:46:21 +01:00
const arrboth = [
{
image: "./demo/images/pizza.gif",
comment: "Test",
},
{
image: "./demo/images/pizza.gif",
comment: "Test 2",
},
];
arrboth.forEach((item) => {
2019-01-15 00:23:01 +00:00
doc.createImage(fs.readFileSync(item.image));
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: item.comment,
style: "normalPara2",
}),
);
2018-03-10 23:04:45 +00:00
});
2018-08-21 02:46:21 +01:00
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});