openXmlSdk insert new line inside of a Run Element

白昼怎懂夜的黑 提交于 2019-12-18 09:48:43

问题


I have text inside of the Run Element. I'm trying to replace the \r in the string with a line break.

The text as follows

This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes

and the innerXml of the Run Element is translated into

<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
   <w:rFonts w:cstheme="minorHAnsi" />
      </w:rPr>
        <w:t xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
           This is an example project for testing purposes.  This is all sample data, none of this is real information.&lt;w:br /&gt;&lt;w:br /&gt;This field allows for the entry of more information, a larger text field for example purposes.</w:t>

Line breaks aren't being inserted when the document is generated.

How can I replace every '\r' inside of the <w:t> </w:t> with a line break?

I've tried.

s.InnerXml = s.InnerXml.Replace("&lt;w:br /&gt;", "<w:br />");

I've also tried replacing it directly in the string but that doesn't work either.

it just comes out as a string

This is an example project for testing purposes.  This is all sample data, none of this is real information.<w:br xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" /><w:br xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />This field allows for the entry of more information, a larger text field for example purposes.

回答1:


The documentation states that a Text element contains literal text. The SDK will not make assumptions about new line characters in a string that you write to a Text element. How would it know if you wanted a break or if you wanted a paragraph?

If you are building the document from literal strings you will have some work to do:

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

namespace OXmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var wordDocument = WordprocessingDocument
                .Create("c:\\deleteme\\testdoc.docx", WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

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

                string theString = "This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes";

                foreach (string s in theString.Split(new char[] { '\r' }))
                {
                    r.AppendChild(new Text(s));
                    r.AppendChild(new Break());
                }

                wordDocument.Save();
            }
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:p>
            <w:r>
                <w:t>This is an example project for testing purposes. </w:t>
                <w:br />
                <w:t>This is all sample data, none of this is real information. </w:t>
                <w:br />
                <w:t/>
                <w:br />
                <w:t>This field allows for the entry of more information, a larger text field for example purposes</w:t>
                <w:br />
            </w:r>
        </w:p>
    </w:body>
</w:document>


来源:https://stackoverflow.com/questions/51566826/openxmlsdk-insert-new-line-inside-of-a-run-element

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