How to add subscript characters in paragraphs using Word Automation?

岁酱吖の 提交于 2019-12-03 07:31:13

Create a Word document and add text with subscipt/superscript and unzip the .docx to examine it's XML content you will notice that the text containing the subscript/superscript is placed in a separate run element.

One way to achieve this is OpenXML SDK.Once you've downloaded and installed the SDK you can use the following code:

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXML
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var doc = WordprocessingDocument.Create("C:\\Subscript.docx", WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();

                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph p = body.AppendChild(new Paragraph());

                p.AppendChild(AddRun(false, "Some text   b "));
                p.AppendChild(AddRun(true, "eff"));
                p.AppendChild(AddRun(false, "= 3.0"));
            }

            Console.WriteLine("Done...");
            Console.ReadLine();
        }

        public static Run AddRun(bool isSubscript, string text)
        {
            Run run = new Run();
            if (isSubscript)
            {
                var props = new RunProperties();
                var fontSize = new FontSizeComplexScript() { Val = "20" };
                var vAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Subscript };

                props.Append(fontSize);
                props.Append(vAlignment);
                run.Append(props);
            }
            run.Append(new Text(text));
            return run;
        }
    }
}

EDIT:

And here's a Interop solution:

    using WordNS = Microsoft.Office.Interop.Word;

    WordNS.Document doc = _application.ActiveDocument;
    WordNS.Paragraph p = doc.Paragraphs.Add();
    p.Range.Text = "Some text   beff = 3.0";

    int start = p.Range.Text.IndexOf("eff");
    int end = p.Range.Text.IndexOf("=");

    WordNS.Range range = doc.Range(start, end);
    range.Select();

    WordNS.Selection currentSelection = _application.Selection;
    currentSelection.Font.Subscript = 1;

    doc.SaveAs2("C:\\SubscriptInterop.docx");

You can play around with the range's start and end point to get that job done, and even find the offset.

Range range = paragraph1.Range;
range.Text = "Some text   beff = 3.0";
if (range.Text.Contains("eff ="))
{
   range.Start = range.Start + range.Text.IndexOf("eff =");
   range.End = range.Start + 3;
   range.Font.Subscript = 1;
}

Hope it helps.

In Word create a Word Macro which simulates the process you want. When done look at the VBA code it creates. That code will give you the what/how on the working of the interops which need to be called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!