数学文本
Contents
[
Hide
]
本文演示如何使用 Aspose.Slides for Node.js via Java 处理数学文本形状并格式化公式。
添加数学文本
创建包含分数和勾股公式的数学形状。
function addMathText() {
let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
// 添加一个数学形状到幻灯片。
let mathShape = slide.getShapes().addMathShape(0, 0, 720, 150);
// 访问数学段落。
let paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
let textPortion = paragraph.getPortions().get_Item(0);
let mathParagraph = textPortion.getMathParagraph();
// 添加一个简单的分数:x / y。
let fraction = new aspose.slides.MathematicalText("x").divide("y");
mathParagraph.add(new aspose.slides.MathBlock(fraction));
// 添加公式:c² = a² + b²。
let mathBlock = new aspose.slides.MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new aspose.slides.MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new aspose.slides.MathematicalText("b").setSuperscript("2"));
mathParagraph.add(mathBlock);
presentation.save("math_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
访问数学文本
在幻灯片上定位包含数学段落的形状。
function accessMathText() {
let presentation = new aspose.slides.Presentation("math_text.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
// 查找第一个包含数学段落的形状。
let mathShape = null;
for (let shapeIndex = 0; shapeIndex < slide.getShapes().size(); shapeIndex++) {
let shape = slide.getShapes().get_Item(shapeIndex);
if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
let autoShape = shape;
let textFrame = autoShape.getTextFrame();
if (textFrame != null) {
let hasMath = false;
for (let paragraphIndex = 0; paragraphIndex < textFrame.getParagraphs().getCount(); paragraphIndex++) {
let paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
for (let portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
let portion = paragraph.getPortions().get_Item(portionIndex);
if (java.instanceOf(portion, "com.aspose.slides.MathPortion")) {
hasMath = true;
break;
}
}
if (hasMath) break;
}
if (hasMath) {
mathShape = autoShape;
break;
}
}
}
}
if (mathShape != null) {
let paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
let textPortion = paragraph.getPortions().get_Item(0);
let mathParagraph = textPortion.getMathParagraph();
// ...
}
} finally {
presentation.dispose();
}
}
删除数学文本
从幻灯片中删除数学形状。
function removeMathText() {
let presentation = new aspose.slides.Presentation("math_text.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
// 假设第一个形状是数学形状。
let mathShape = slide.getShapes().get_Item(0);
// 删除数学形状。
slide.getShapes().remove(mathShape);
presentation.save("math_text_removed.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
格式化数学文本
为数学部分设置字体属性。
function formatMathText() {
let presentation = new aspose.slides.Presentation("math_text.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
// 假设第一个形状是数学形状。
let mathShape = slide.getShapes().get_Item(0);
let paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
let textPortion = paragraph.getPortions().get_Item(0);
textPortion.getPortionFormat().setFontHeight(20);
presentation.save("math_text_formatted.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}