Testo matematico

Questo articolo dimostra come lavorare con forme di testo matematico e formattare equazioni usando Aspose.Slides for .NET.

Aggiungi testo matematico

Crea una forma matematica contenente una frazione e la formula pitagorica.

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

    // Aggiungi una forma Math alla diapositiva.
    var mathShape = slide.Shapes.AddMathShape(0, 0, 720, 150);

    // Accedi al paragrafo matematico.
    var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

    // Aggiungi una frazione semplice: x / y
    var fraction = new MathematicalText("x").Divide("y");
    mathParagraph.Add(new MathBlock(fraction));

    // Aggiungi equazione: 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);
}

Accedi al testo matematico

Trova una forma che contiene un paragrafo matematico nella diapositiva.

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

    // Trova la prima forma che contiene un paragrafo matematico.
    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;

        // Esempio: crea una frazione (non aggiunta qui).
        var fraction = new MathematicalText("x").Divide("y");

        // Usa mathParagraph o fraction secondo necessità...
    }
}

Rimuovi testo matematico

Elimina una forma matematica dalla diapositiva.

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);
}

Formatta testo matematico

Imposta le proprietà del carattere per una porzione matematica.

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;
}