Files
docx-js/demo/53-chinese.ts

92 lines
7.3 KiB
TypeScript
Raw Normal View History

2020-06-07 12:46:21 +08:00
// Chinese text - Chinese text need to use a Chinese font. And ascii text need to use a ascii font.
// Different from the `52-japanese.ts`.
// `52-japanese.ts` will set all characters to use Japanese font.
// `53-chinese.ts` will set Chinese characters to use Chinese font, and set ascii characters to use ascii font.
2020-06-07 12:46:21 +08:00
// Note that if the OS have not install `KaiTi` font, this demo doesn't work.
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2020-06-07 14:58:59 +08:00
import { Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build";
const doc = new Document({
styles: {
2021-03-09 01:37:22 +00:00
default: {
document: {
run: {
font: {
ascii: "minorHAnsi",
eastAsia: "minorEastAsia",
cs: "minorBidi",
hAnsi: "minorHAnsi",
},
},
},
},
paragraphStyles: [
{
id: "Normal",
name: "Normal",
basedOn: "Normal",
next: "Normal",
quickFormat: true,
run: {
font: {
2021-03-09 01:37:22 +00:00
ascii: "minorHAnsi", // Can also use minorHAnsi
eastAsia: "minorEastAsia", // Can also use minorEastAsia
cs: "minorBidi",
hAnsi: "minorHAnsi",
},
},
},
],
},
2021-03-19 20:53:56 +00:00
sections: [
{
2020-06-07 14:58:59 +08:00
children: [
2021-03-19 20:53:56 +00:00
new Paragraph({
text: "中文和英文 Chinese and English",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph({
2020-06-07 14:58:59 +08:00
text: "中文和英文 Chinese and English",
2021-03-19 20:53:56 +00:00
}),
new Paragraph({
children: [
new TextRun({
text: "中文和英文 Chinese and English",
font: { eastAsia: "SimSun" }, // set eastAsia to "SimSun".
// The ascii characters will use the default font ("Times") specified in paragraphStyles
}),
],
}),
new Paragraph({
text: `店様付旅母正役基社発破班。発治治京岡局本因追意金紀統責郵滴尽。立功日庁重天富評界明済不着法返祉経正引行。載区列防新目治日群付無人道言越名。界安無死専体風木人教録覧訃。女問堂容県作先芸便効証帯債土全極的日。能山中岡仕関攻井季接店線幌温後率事阜止。成間路行究今式歌像祉帯式媛読徹。安行息古入出文侵監株登部席内文第珍鶴問用。
使
西調
使
椿
便
調綿調稿調
貿`,
2020-06-07 14:58:59 +08:00
}),
],
2021-03-19 20:53:56 +00:00
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});