数学文本
Contents
[
Hide
]
本文演示了如何使用 Aspose.Slides for C++ 处理数学文本形状并格式化公式。
添加数学文本
创建一个包含分数和勾股公式的数学形状。
static void AddMathText()
{
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
// 向幻灯片添加数学形状。
auto mathShape = slide->get_Shapes()->AddMathShape(0, 0, 720, 150);
// 访问数学段落。
auto paragraph = mathShape->get_TextFrame()->get_Paragraph(0);
auto textPortion = paragraph->get_Portion(0);
auto mathParagraph = ExplicitCast<MathPortion>(textPortion)->get_MathParagraph();
// 添加一个简单分数:x / y。
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
mathParagraph->Add(MakeObject<MathBlock>(fraction));
// 添加公式:c² = a² + b²。
auto mathBlock = MakeObject<MathematicalText>(u"c")
- >SetSuperscript(u"2")
- >Join(u"=")
- >Join(MakeObject<MathematicalText>(u"a")->SetSuperscript(u"2"))
- >Join(u"+")
- >Join(MakeObject<MathematicalText>(u"b")->SetSuperscript(u"2"));
mathParagraph->Add(mathBlock);
presentation->Dispose();
}
访问数学文本
在幻灯片上定位包含数学段落的形状。
static void AccessMathText()
{
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
// 查找第一个包含数学段落的形状。
auto mathShape = SharedPtr<IAutoShape>();
for (auto&& shape : slide->get_Shapes())
{
if (ObjectExt::Is<IAutoShape>(shape))
{
auto autoShape = ExplicitCast<IAutoShape>(shape);
auto textFrame = autoShape->get_TextFrame();
auto hasMath = false;
for (auto&& paragraph : textFrame->get_Paragraphs())
{
for (auto&& textPortion : paragraph->get_Portions())
{
if (ObjectExt::Is<MathPortion>(textPortion))
{
hasMath = true;
break;
}
}
if (hasMath) break;
}
if (hasMath)
{
mathShape = autoShape;
break;
}
}
}
if (mathShape != nullptr)
{
auto paragraph = mathShape->get_TextFrame()->get_Paragraph(0);
auto textPortion = paragraph->get_Portion(0);
auto mathParagraph = ExplicitCast<MathPortion>(textPortion)->get_MathParagraph();
// 示例:创建一个分数(此处未添加)。
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
// 根据需要使用 mathParagraph 或 fraction...
}
presentation->Dispose();
}
移除数学文本
从幻灯片中删除数学形状。
static void RemoveMathText()
{
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto mathShape = slide->get_Shapes()->AddMathShape(50, 50, 100, 50);
auto paragraph = mathShape->get_TextFrame()->get_Paragraph(0);
auto textPortion = paragraph->get_Portion(0);
auto mathParagraph = ExplicitCast<MathPortion>(textPortion)->get_MathParagraph();
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
mathParagraph->Add(MakeObject<MathBlock>(fraction));
// 移除数学形状。
slide->get_Shapes()->Remove(mathShape);
presentation->Dispose();
}
格式化数学文本
为数学部分设置字体属性。
static void FormatMathText()
{
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto mathShape = slide->get_Shapes()->AddMathShape(50, 50, 100, 50);
auto paragraph = mathShape->get_TextFrame()->get_Paragraph(0);
auto textPortion = paragraph->get_Portion(0);
auto mathParagraph = ExplicitCast<MathPortion>(textPortion)->get_MathParagraph();
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
mathParagraph->Add(MakeObject<MathBlock>(fraction));
textPortion->get_PortionFormat()->set_FontHeight(20);
presentation->Dispose();
}