加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用Word Automation在段落中添加下标字符?

发布时间:2020-12-15 04:14:19 所属栏目:百科 来源:网络整理
导读:我正在使用C#编写一个程序,该程序使用Microsoft Word 14.0对象库创建.doc文件,为其添加段落并保存.有一个小形式,带有描述动作的按钮(参见下面的代码).这部分没有问题. 问题: 创建的word文件中的当前文本将如下: 一些文本beff = 3.0 我需要完成的是创建一个
我正在使用C#编写一个程序,该程序使用Microsoft Word 14.0对象库创建.doc文件,为其添加段落并保存.有一个小形式,带有描述动作的按钮(参见下面的代码).这部分没有问题.

问题:

创建的word文件中的当前文本将如下:

一些文本beff = 3.0

我需要完成的是创建一个段落,里面有下标字符.(在上面的段落中,字母“eff”应该是下标的):

最终文档将包含大约100个像上面这样的行,下载不同的字符.

我找到了用线代替整个段落的方法,

paragraph1.Range.Font.Subscript = 1;

但没有办法在单独的角色上实现它.

我也知道我可以使用Unicode中的下标字母和数字,但不幸的是,Unicode没有下标格式的完整字母,所以这也不是一个选项.

题:
有没有办法让我完成目标并在新创建的Word文档的段落中的下标中插入类似“eff”的内容?

示例代码:

private void btnReport_Click(object sender,EventArgs e)
    {

        Word._Application oWord;
        Word._Document oDoc;
        oWord = new Word.Application();
        oDoc = oWord.Documents.Add();


        var paragraph1 = oDoc.Content.Paragraphs.Add();
        paragraph1.Range.Text = "Some text   beff = 3.0";

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "Word document|*.doc";
        saveFileDialog1.Title = "Save the Word Document";
        if (DialogResult.OK == saveFileDialog1.ShowDialog())
        {
            string docName = saveFileDialog1.FileName;
            if (docName.Length > 0)
            {
                object oDocName = (object)docName;
                oDoc.SaveAs(ref oDocName);
            }
        }
        oWord.Quit();
    }

解决方法

创建一个Word文档并使用subscipt / superscript添加文本并解压缩.docx以检查它的XML内容,您会注意到包含下标/上标的文本放在一个单独的run元素中.

实现此目的的一种方法是OpenXML SDK.Once您下载并安装SDK后,您可以使用以下代码:

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

编辑:

这是一个Interop解决方案:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读