PPTX
形状与文本
创建带格式文本、段落、对齐、列表和文本自适应的形状
形状是幻灯片的主要构建块。一个 Shape 可以通过 Paragraph 和 TextRun 对象来包含文本。
基本形状
import { Shape } from "@office-open/pptx";
new Shape({
x: 1,
y: 1,
width: 5,
height: 3,
text: "Simple text",
});
几何预设
使用 geometry 选项设置形状类型:
new Shape({ geometry: "rect" }); // 矩形(默认)
new Shape({ geometry: "ellipse" }); // 椭圆/圆形
new Shape({ geometry: "roundRect" }); // 圆角矩形
new Shape({ geometry: "triangle" });
new Shape({ geometry: "diamond" });
new Shape({ geometry: "star5" });
new Shape({ geometry: "hexagon" });
new Shape({ geometry: "chevron" });
new Shape({ geometry: "arrow" });
段落和文本片段
使用 paragraphs 完全控制文本格式:
new Shape({
x: 1,
y: 1,
width: 6,
height: 3,
paragraphs: [
new Paragraph({
children: [
new TextRun({ text: "Bold title", bold: true, fontSize: 28, color: "2E74B5" }),
],
}),
new Paragraph({
children: [
new TextRun({ text: "Normal text with " }),
new TextRun({ text: "italic", italics: true }),
new TextRun({ text: " and " }),
new TextRun({ text: "colored", color: "FF0000", underline: {} }),
],
}),
],
});
TextRun 格式选项
new TextRun({
text: "Formatted text",
fontSize: 24, // 字号(磅)
bold: true,
italics: true,
underline: {}, // 单下划线
color: "4472C4", // 十六进制颜色,不带 #
font: "Calibri", // 字体
break: 1, // 换行数
});
对齐方式
new Paragraph({
alignment: "left", // "left" | "center" | "right" | "justify"
children: [new TextRun({ text: "Aligned text" })],
});
项目符号列表
new Shape({
x: 1,
y: 1,
width: 6,
height: 4,
paragraphs: [
new Paragraph({
bullet: true,
children: [new TextRun({ text: "First item" })],
}),
new Paragraph({
bullet: true,
children: [new TextRun({ text: "Second item" })],
}),
new Paragraph({
bullet: { type: "number" },
children: [new TextRun({ text: "Numbered item" })],
}),
],
});
文本锚点(垂直对齐)
控制文本在形状内的垂直位置:
new Shape({
x: 1,
y: 1,
width: 5,
height: 3,
textAnchor: "top", // "top" | "middle" | "bottom"
text: "Top-aligned text",
});
文本自适应
new Shape({
x: 1,
y: 1,
width: 4,
height: 2,
text: "This text will shrink to fit the shape",
textAutoFit: "shrink",
// 或 textAutoFit: "resize" 来调整形状大小
});
旋转和翻转
new Shape({
x: 1,
y: 1,
width: 3,
height: 2,
rotation: 45, // 角度
flip: "vertical", // "horizontal" | "vertical" | "both"
text: "Rotated shape",
});
文本分栏
new Shape({
x: 1,
y: 1,
width: 8,
height: 4,
textColumns: { count: 2, spacing: 0.5 },
paragraphs: [
new Paragraph({ children: [new TextRun({ text: "Column 1 content..." })] }),
new Paragraph({ children: [new TextRun({ text: "Column 2 content..." })] }),
],
});
文本边距
new Shape({
x: 1,
y: 1,
width: 6,
height: 3,
textMargins: { top: 0.2, bottom: 0.2, left: 0.3, right: 0.3 },
text: "Text with custom margins",
});