Set superscript and subscript in Word document

Spire.Doc for .NET, a professional .NET word component to fast generate, open, modify and save Word documents without using MS Office Automation, enables users to set superscript and subscript in word document by using C#.NET. This guide introduces a method how to set superscript and subscript via Spire.Doc for .NET.

The screenshot below presents result after setting superscript and subscript in word:

Set superscript and subscript in Word document

There is a guide to introduce the detail method to set superscript and subscript in word document.

The main steps of method are:

Step 1: create a document and add new paragraph.

Document document = new Document(@"Blank.doc");
Paragraph paragraph = document.LastSection.AddParagraph();

Step 2: use the range1.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SuperScript to set suberscript.

paragraph.AppendText("E = mc");
TextRange range1 = paragraph.AppendText("2");
//supperscript
range1.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SuperScript;

Step 3: use range2.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript to set subscript.

paragraph.AppendText("F");
TextRange range2 = paragraph.AppendText("n");
//subscript
range2.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript;

Step 4: save the document.

document.SaveToFile("result.docx", FileFormat.Docx);

Download and install Spire.Doc for .NET and use below code to experience this method to set superscript and subscript in word document.

The full code:

[C#]
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SubSuperScript
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"Blank.doc");
            Paragraph paragraph = document.LastSection.AddParagraph();
            paragraph.AppendText("E = mc");
            TextRange range1 = paragraph.AppendText("2");

            //supperscript
            range1.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SuperScript;

            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendText("F");
            TextRange range2 = paragraph.AppendText("n");

            //subscript
            range2.CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript;

            paragraph.AppendText(" = F");
            paragraph.AppendText("n-1").CharacterFormat.SubSuperScript =Spire.Doc.Documents.SubSuperScript.SubScript;
            paragraph.AppendText(" + F");
            paragraph.AppendText("n-2").CharacterFormat.SubSuperScript = Spire.Doc.Documents.SubSuperScript.SubScript;

            //fontsize
            foreach (var i in paragraph.Items)
            {
                if (i is TextRange)
                {
                    (i as TextRange).CharacterFormat.FontSize = 36;
                }
            } 
            document.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
      }
}

If you couldn't successfully use the Spire.Doc please refer the Spire.Doc Quick Start which can guide you quickly use the Spire.Doc.