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

我正在使用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。一旦下载并安装了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"); 

您可以使用范围的起点和终点来完成工作,甚至可以找到偏移量。

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

希望能帮助到你。

在Word中创建一个Word宏,模拟您想要的过程。 完成后,查看它创建的VBA代码。 该代码将为您提供需要调用的互操作的工作方式/方式。