简体   繁体   English

使用OpenXML将文本替换为MS Word中的等式

[英]Replace text to equation in MS Word using OpenXML

Hello I need to replace text in my template to Mathematical equation. 您好我需要将模板中的文本替换为数学方程式。 I want to do this by replace a paragraph or his part. 我想通过替换段落或他的部分来做到这一点。 I'm trying to do this with OpenXML. 我正在尝试使用OpenXML。 My program at the begining is opening a template and copy to new word document, then i'm looking in every paragraph specific words when i find it will replace whole paragraph to a new with equation. 我的程序在开始时打开一个模板并复制到新的word文档,然后我在查找每个段落的特定单词,当我发现它将整个段落替换为新的等式。 Program will compile and finish work, but when i'm trying to open a new document i can't because error appears: problem with the content. 程序将编译并完成工作,但是当我尝试打开新文档时,我不能因为出现错误:内容出现问题。

In file ExampleXML.xml i have xml what i want to replace: 在文件ExampleXML.xml中我有xml我要替换的内容:

<?xml verios="1.0"?>
<w:p w:rsidRPr="00A750D6" w:rsidR="00212F73" w:rsidP="005437B7" w:rsidRDefault="0027760B" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:r>
    <w:rPr>
        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
    </w:rPr>
        <m:t>a=</m:t>
</m:r>
<m:f>
    <m:fPr>
        <m:ctrlPr>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                <w:i />
            </w:rPr>
        </m:ctrlPr>
    </m:fPr>
    <m:num>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>c</m:t>
        </m:r>
    </m:num>
    <m:den>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>b</m:t>
        </m:r>
    </m:den>
</m:f>
<m:r>
    <w:rPr>
        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
    </w:rPr>
    <m:t>*</m:t>
</m:r>
<m:rad>
    <m:radPr>
        <m:degHide m:val="1" />
        <m:ctrlPr>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                <w:i />
            </w:rPr>
        </m:ctrlPr>
    </m:radPr>
    <m:deg />
    <m:e>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>a</m:t>
        </m:r>
    </m:e>
</m:rad>
</m:oMath>
</m:oMathPara>
<w:bookmarkStart w:name="_GoBack" w:id="0" />
<w:bookmarkEnd w:id="0" /></w:p>

This is my code: 这是我的代码:

public void CreateXmlDocument() { public void CreateXmlDocument(){

        var wzor = File.ReadAllText(@"E:\ExampleXML.xml");
        using (var template = File.Open(@"E:\ExampleDOCX.docx", FileMode.Open, FileAccess.Read))
        {
            using (MemoryStream stream = new MemoryStream())
            {
                template.CopyTo(stream);

                using (WordprocessingDocument WordTemplate = WordprocessingDocument.Open(stream, true))
                {

                    MainDocumentPart mainDocumentPart = WordTemplate.MainDocumentPart;

                    foreach (var paragraph in mainDocumentPart.Document.Descendants<Paragraph>().ToList())
                    {
                        var parent = paragraph.Parent;

                        var paragraph_xml = (paragraph.OuterXml);
                        bool containsParam = false;

                        if (paragraph.InnerText.Contains("test"))
                            {
                                var tekst_replace = paragraph_xml;

                                paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);
                                containsParam = true;
                            }
                        if (containsParam)
                        {
                            parent.InsertBefore(new Paragraph(paragraph_xml), paragraph);
                            paragraph.Remove();
                        }
                    }
                    mainDocumentPart.Document.Save();
                    stream.Seek(0, SeekOrigin.Begin);
                }

                using (var fileStream = File.Create(@"E:\po.docx"))
                {
                    stream.CopyTo(fileStream);
                }
            }
        }
    }

What is wrong? 怎么了? or how can i change text to equation? 或者我怎样才能将文字改为等式? Thanks for your help. 谢谢你的帮助。

From your snippet: 从您的代码段:

var wzor = File.ReadAllText(@"E:\ExampleXML.xml");

...

var paragraph_xml = (paragraph.OuterXml);

...

var tekst_replace = paragraph_xml;

paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);

It looks like this could be simplified to: 看起来这可以简化为:

string a = "a";
string b_0 = "b";
string b_1 = b_0; // b_1 == "b"

b_0 = b_1.Replace(b_0, a); // "b".Replace("b", "a")

So why not just directly set b_0 = a ? 那么为什么不直接设置b_0 = a

Or in your case, replace: 或者在您的情况下,替换:

var paragraph_xml = (paragraph.OuterXml);
bool containsParam = false;

if (paragraph.InnerText.Contains("test"))
{
    var tekst_replace = paragraph_xml;

    paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);
    containsParam = true;
}
if (containsParam)
{
    parent.InsertBefore(new Paragraph(paragraph_xml), paragraph);
    paragraph.Remove();
}

with: 有:

if (paragraph.InnerText.Contains("test"))
{
    parent.InsertBefore(new Paragraph(wzor), paragraph);
    paragraph.Remove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM