如何添加标字符使用Word自动化段落? [英] How to add subscript characters in paragraphs using Word Automation?

查看:183
本文介绍了如何添加标字符使用Word自动化段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#程序使用的Microsoft Word 14.0对象库创建一个.doc文件,添加段落,并保存它。有一个小的形式,它描述的动作(见下面的code)的按钮。这部分有没有问题。

问题:

当前中创建Word文件文本将以下内容:

某些文本贝夫= 3.0

我需要完成,正在创造一个段落,这里面有标字符(上文第字母EFF应该为下标):

最后文件将包括周围像以上线路100,与那些下标不同的字符。

我找到了一种用下标线全段,

  paragraph1.Range.Font.Subscript = 1;

但发现没有办法实现它在不同的字符。

我也知道有标字母和数字的Uni code,我可以使用,但不幸的是,统一code不具有下标格式全字母,所以这是不是一种选择无论是。

问题:
有没有办法让我实现目标并插入标像EFF一个段落里面一个新创建的Word文档?

样code:

 私人无效btnReport_Click(对象发件人,EventArgs的发送)
    {        Word._Application oWord;
        Word._Document沃达柯;
        oWord =新Word.Application();
        沃达柯= oWord.Documents.Add();
        VAR PARAGRAPH1 = oDoc.Content.Paragraphs.Add();
        paragraph1.Range.Text =一些文字贝夫= 3.0;        SaveFileDialog saveFileDialog1 =新SaveFileDialog();
        saveFileDialog1.Filter =Word文档| * .DOC
        saveFileDialog1.Title =保存Word文档;
        如果(DialogResult.OK == saveFileDialog1.ShowDialog())
        {
            字符串DOCNAME = saveFileDialog1.FileName;
            如果(docName.Length大于0)
            {
                对象oDocName =(对象)DOCNAME;
                oDoc.SaveAs(REF oDocName);
            }
        }
        oWord.Quit();
    }


解决方案

创建一个Word文档,并与subscipt /标添加文本和解压的.docx检查它的XML内容,你会发现,包含标文/标是放置在一个单独的运行元素。

实现这一目标的方法之一是 OpenXML的SDK 。一旦你已经下载并安装,你可以使用下面的code中的SDK:

 使用系统;
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Wordpro​​cessing;命名空间的OpenXML
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            使用(VAR DOC = Wordpro​​cessingDocument.Create(C:\\\\ Subscript.docx,Wordpro​​cessingDocumentType.Document))
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();                mainPart.Document =新的文档();
                美体= mainPart.Document.AppendChild(新体());
                第P = body.AppendChild(新第());                p.AppendChild(AddRun(假的,有些文字B));
                p.AppendChild(AddRun(真的,EFF));
                p.AppendChild(AddRun(假,= 3.0));
            }            Console.WriteLine(完成......);
            到Console.ReadLine();
        }        公共静态运行AddRun(布尔isSubscript,字符串文本)
        {
            RUN RUN =新的运行();
            如果(isSubscript)
            {
                VAR道具=新RunProperties();
                fontSize的VAR =新FontSizeComplexScript(){瓦尔=20};
                VAR vAlignment =新VerticalTextAlignment(){瓦尔= VerticalPositionValues​​.Subscript};                props.Append(fontSize的);
                props.Append(vAlignment);
                run.Append(道具);
            }
            run.Append(新文本(文本));
            返回运行;
        }
    }
}

编辑:

这是一个互操作的解决方案:

 使用WordNS =的Microsoft.Office.Interop.Word;    WordNS.Document DOC = _application.ActiveDocument;
    WordNS.Paragraph p值= doc.Paragraphs.Add();
    p.Range.Text =一些文字贝夫= 3.0;    INT开始= p.Range.Text.IndexOf(EFF);
    INT结束= p.Range.Text.IndexOf(=);    WordNS.Range范围= doc.Range(开始,结束);
    range.Select();    WordNS.Selection currentSelection = _application.Selection;
    currentSelection.Font.Subscript = 1;    doc.SaveAs2(C:\\\\ SubscriptInterop.docx);

I’m working on a program in C# that uses Microsoft Word 14.0 Object Library to create a .doc file, add paragraphs to it and saves it. There is a small form with a button that does described actions (see the code below). This part has no problems.

Problem:

Current text in created word file will be the following:

Some text beff = 3.0

What I need to accomplish, is creating a paragraph, which has subscript characters inside.(in paragraph above letters "eff" should be subscripted):

The final document would contain around 100 of lines like above, with different characters that are subscripted.

I found a way to subscript the whole paragraph with line,

paragraph1.Range.Font.Subscript = 1;

but found no way to implement it on separate characters.

I’m also aware that there are subscript letters and numbers in Unicode that I could use, but, unfortunately, Unicode does not have full alphabet in subscript format, so that is not an option either.

Question: Is there a way for me to accomplish the Goal and insert something like "eff" in subscript inside a paragraph in a freshly created Word Document?

Sample code:

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

解决方案

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

这篇关于如何添加标字符使用Word自动化段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆