Texte mathématique
Contents
[
Hide
]
Cet article montre comment travailler avec des formes de texte mathématique et formater des équations à l’aide de Aspose.Slides for C++.
Ajouter du texte mathématique
Créez une forme mathématique contenant une fraction et la formule de Pythagore.
static void AddMathText()
{
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
// Ajouter une forme Math à la diapositive.
auto mathShape = slide->get_Shapes()->AddMathShape(0, 0, 720, 150);
// Accéder au paragraphe mathématique.
auto paragraph = mathShape->get_TextFrame()->get_Paragraph(0);
auto textPortion = paragraph->get_Portion(0);
auto mathParagraph = ExplicitCast<MathPortion>(textPortion)->get_MathParagraph();
// Ajouter une fraction simple : x / y.
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
mathParagraph->Add(MakeObject<MathBlock>(fraction));
// Ajouter l'équation : 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();
}
Accéder au texte mathématique
Localisez une forme qui contient un paragraphe mathématique sur la diapositive.
static void AccessMathText()
{
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto slide = presentation->get_Slide(0);
// Trouver la première forme qui contient un paragraphe mathématique.
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();
// Exemple : créer une fraction (non ajoutée ici).
auto fraction = MakeObject<MathematicalText>(u"x")->Divide(u"y");
// Utiliser mathParagraph ou fraction selon les besoins...
}
presentation->Dispose();
}
Supprimer le texte mathématique
Supprimez une forme mathématique de la diapositive.
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));
// Supprimer la forme mathématique.
slide->get_Shapes()->Remove(mathShape);
presentation->Dispose();
}
Formater le texte mathématique
Définissez les propriétés de police pour une partie mathématique.
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();
}