Manage Superscript and Subscript in C#

Overview

Aspose.Slides for .NET provides features for integrating superscript and subscript text into your PowerPoint (PPT, PPTX) and OpenDocument (ODP) presentations. Whether you need to highlight chemical formulas, mathematical equations, or annotate content with footnotes, these specialized formatting options help maintain clarity and precision. In this article, you’ll learn how to seamlessly apply superscript and subscript styles and ensure professional results in every slide.

Add Superscript and Subscript Text

You can add superscript and subscript text inside any paragraph in a presentation. To achieve this with Aspose.Slides, you must use the Escapement property of the PortionFormat class.

This property allows you to set superscript or subscript text, with values ranging from -100% (subscript) to 100% (superscript).

Implementation steps:

  1. Create an instance of the Presentation class.
  2. Get a reference to a slide using its index.
  3. Add an IAutoShape of type Rectangle to the slide.
  4. Access the ITextFrame associated with the IAutoShape.
  5. Clear existing paragraphs.
  6. Create a new Paragraph for superscript text and add it to the paragraph collection of the ITextFrame.
  7. Create a new text portion object.
  8. Set the Escapement property for the text portion between 0 to 100 to apply superscript (0 means no superscript).
  9. Set some text for the Portion and add it to the paragraph’s portion collection.
  10. Create another Paragraph for subscript text and add it to the paragraph collection.
  11. Create a new text portion object.
  12. Set the Escapement property for the text portion between 0 to -100 to apply subscript (0 means no subscript).
  13. Set some text for the Portion and add it to the paragraph’s portion collection.
  14. Save the presentation as a PPTX file.

The following C# code implements these steps:

using (Presentation presentation = new Presentation())
{
    // Get the first slide.
    ISlide slide = presentation.Slides[0];

    // Create a text box.
    IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 200, 100);
    ITextFrame textFrame = shape.TextFrame;

    textFrame.Paragraphs.Clear();

    // Create a paragraph for superscript text.
    IParagraph superPar = new Paragraph();

    // Create a text portion with regular text.
    IPortion portion1 = new Portion();
    portion1.Text = "MyProduct";
    superPar.Portions.Add(portion1);

    // Create a text portion with superscript text.
    IPortion superPortion = new Portion();
    superPortion.PortionFormat.Escapement = 30;
    superPortion.Text = "TM";
    superPar.Portions.Add(superPortion);

    // Create a paragraph for subscript text.
    IParagraph paragraph2 = new Paragraph();

    // Create a text portion with regular text.
    IPortion portion2 = new Portion();
    portion2.Text = "a";
    paragraph2.Portions.Add(portion2);

    // Create a text portion with subscript text.
    IPortion subPortion = new Portion();
    subPortion.PortionFormat.Escapement = -25;
    subPortion.Text = "i";
    paragraph2.Portions.Add(subPortion);

    // Add the paragraphs to the text box.
    textFrame.Paragraphs.Add(superPar);
    textFrame.Paragraphs.Add(paragraph2);

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

The result:

Superscript and Subscript

FAQs

1. Will superscript and subscript be preserved when exporting to PDF or other formats?

Yes, Aspose.Slides for .NET properly retains superscript and subscript formatting when exporting presentations to PDF, PPT/PPTX, images, and other supported formats. The specialized formatting remains intact in all output files.

2. Can superscript and subscript be combined with other formatting styles such as bold or italics?

Yes, Aspose.Slides allows you to mix various text styles within a single portion of text. You can enable bold, italics, underline, and simultaneously apply superscript or subscript by configuring the corresponding properties in PortionFormat.

3. Do superscript and subscript formatting work for text inside tables, charts, or SmartArt?

Yes, Aspose.Slides for .NET supports formatting within most objects, including tables and chart elements. When working with SmartArt, you need to access the appropriate elements (such as SmartArtNode) and their text containers, and then configure the PortionFormat properties in a similar manner.