Texte mathématique

Cet article montre comment travailler avec des formes de texte mathématique et mettre en forme des équations à l’aide de Aspose.Slides for .NET.

Ajouter du texte mathématique

Créez une forme mathématique contenant une fraction et la formule de Pythagore.

static void AddMathText()
{
    using var presentation = new Presentation();
    var slide = presentation.Slides[0];

    // Ajouter une forme Math à la diapositive.
    var mathShape = slide.Shapes.AddMathShape(0, 0, 720, 150);

    // Accéder au paragraphe mathématique.
    var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

    // Ajouter une fraction simple : x / y
    var fraction = new MathematicalText("x").Divide("y");
    mathParagraph.Add(new MathBlock(fraction));

    // Ajouter une équation : c² = a² + b²
    var mathBlock = new MathematicalText("c")
        .SetSuperscript("2")
        .Join("=")
        .Join(new MathematicalText("a").SetSuperscript("2"))
        .Join("+")
        .Join(new MathematicalText("b").SetSuperscript("2"));

    mathParagraph.Add(mathBlock);
}

Accéder au texte mathématique

Localisez une forme contenant un paragraphe mathématique sur la diapositive.

static void AccessMathText()
{
    using var presentation = new Presentation("sample.pptx");
    var slide = presentation.Slides[0];

    // Trouver la première forme qui contient un paragraphe mathématique.
    var mathShape = slide.Shapes
        .OfType<IAutoShape>()
        .FirstOrDefault(s =>
            s.TextFrame != null &&
            s.TextFrame.Paragraphs.Any(p =>
                p.Portions.Any(portion => portion is MathPortion)));

    if (mathShape != null)
    {
        var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

        // Exemple : créer une fraction (non ajoutée ici).
        var fraction = new MathematicalText("x").Divide("y");

        // Utiliser mathParagraph ou fraction selon les besoins...
    }
}

Supprimer le texte mathématique

Supprimez une forme mathématique de la diapositive.

static void RemoveMathText()
{
    using var presentation = new Presentation();
    var slide = presentation.Slides[0];

    var mathShape = slide.Shapes.AddMathShape(50, 50, 100, 50);
    var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    var fraction = new MathematicalText("x").Divide("y");
    mathParagraph.Add(new MathBlock(fraction));

    slide.Shapes.Remove(mathShape);
}

Formater le texte mathématique

Définissez les propriétés de police pour une partie mathématique.

static void FormatMathText()
{
    using var presentation = new Presentation();
    var slide = presentation.Slides[0];

    var mathShape = slide.Shapes.AddMathShape(50, 50, 100, 50);
    var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    var fraction = new MathematicalText("x").Divide("y");
    mathParagraph.Add(new MathBlock(fraction));

    mathShape.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 20;
}