News Category

C#/VB.NET: Insert Superscripts and Subscripts into Word

Superscripts or subscripts are characters positioned slightly above or below the normal line of text. They are commonly used in scientific formulas such as mathematical equations or chemical expressions. If you are creating a document containing scientific formulas, you most likely need to insert superscripts or subscripts. In this article, we will demonstrate how to insert superscripts and subscripts into Word in C# and VB.NET using Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert Superscripts and Subscripts into Word using C# and VB.NET

The following are the main steps to insert a superscript or subscript into a Word document using Spire.Doc for .NET:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the specific section through Document.Sections[sectionIndex] property.
  • Add a paragraph to the section using Section.AddParagraph() method.
  • Add normal text to the paragraph using Paragraph.AppendText() method.
  • Add superscript or subscript text to the paragraph using Paragraph.AppendText() method.
  • Apply superscript or subscript formatting to the superscript or subscript text through TextRange.CharacterFormat.SubSuperScript property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertSuperscriptAndSubscript
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            //Add normal text to the paragraph
            paragraph.AppendText("E = mc");
            //Add superscript text to the paragraph
            TextRange superscriptText = paragraph.AppendText("2");
            //Apply superscript formatting to the superscript text
            superscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;

            //Start a new line
            paragraph.AppendBreak(BreakType.LineBreak);

            //Add normal text to the paragraph
            paragraph.AppendText("H");
            //Add subscript text to the paragraph
            TextRange subscriptText = paragraph.AppendText("2");
            //Apply subscript formatting to the subscript text
            subscriptText.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            //Add normal text to the paragraph
            paragraph.AppendText("O");

            //Set font size for the text in the paragraph
            foreach (var item in paragraph.Items)
            {
                if (item is TextRange)
                {
                    TextRange textRange = item as TextRange;
                    textRange.CharacterFormat.FontSize = 36f;
                }
            }

            //Save the result document
            document.SaveToFile("InsertSuperscriptAndSubscript.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Insert Superscripts and Subscripts into Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title: Set superscript and subscript
Last modified on Thursday, 01 June 2023 01:28