diff --git a/OpenXmlFormats/Wordprocessing/wml.cs b/OpenXmlFormats/Wordprocessing/wml.cs index c65f2190f..8a9e4e607 100644 --- a/OpenXmlFormats/Wordprocessing/wml.cs +++ b/OpenXmlFormats/Wordprocessing/wml.cs @@ -18,6 +18,7 @@ using NPOI.OpenXml4Net.Util; using System.Xml; using NPOI.OpenXmlFormats.Dml.WordProcessing; +using System.Text; namespace NPOI.OpenXmlFormats.Wordprocessing { @@ -2246,9 +2247,26 @@ public static CT_Text Parse(XmlNode node, XmlNamespaceManager namespaceManager) { if (node == null) return null; + CT_Text ctObj = new CT_Text(); ctObj.space = XmlHelper.ReadString(node.Attributes["xml:space"]); - ctObj.Value = node.InnerText; + + //Check if the current Xml Node contains "" elements. + //Each cr element should be replaced with \n + StringBuilder sb = new StringBuilder(); + foreach (XmlNode elem in node.ChildNodes) + { + if (elem.NodeType==XmlNodeType.Text) + { + sb.Append(elem.InnerText); + } + else if (elem.NodeType==XmlNodeType.Element && elem.LocalName=="cr") + { + sb.Append("\n"); + } + } + + ctObj.Value = sb.ToString(); return ctObj; } @@ -2263,7 +2281,13 @@ internal void Write(StreamWriter sw, string nodeName) sw.Write(">"); if (this.valueField != null) { - sw.Write(XmlHelper.EncodeXml(this.valueField)); + string[] parts = this.valueField.Split('\n'); + for(int i = 0; i"); + } } sw.WriteEndW(nodeName); } diff --git a/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs b/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs index af7a95dc8..ad900da1c 100644 --- a/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs +++ b/testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs @@ -28,6 +28,8 @@ namespace TestCases.XWPF.UserModel using System; using System.Collections.Generic; using System.IO; + using System.Linq; + using System.Text; using TestCases; [TestFixture] @@ -135,6 +137,53 @@ public void TestAddParagraph() //Assert.AreSame(cP, doc.Paragraphs[(0)]); //Assert.AreEqual(5, doc.Paragraphs.Count); } + + [Test] + public void ReplaceParagraphText() + { + XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("WordReplaceCRLF.docx"); + + //Find and replace text in document body + doc.FindAndReplaceText("$replace_text$", "Regel1\nRegel2\nRegel3"); + + //Find and replace text io tabel cell + doc.FindAndReplaceText("$replace_cell_text$", "Regel1\nRegel2\nRegel3"); + + //Save Word Document + XWPFDocument outputDocument = outputDocument = XWPFTestDataSamples.WriteOutAndReadBack(doc); + + //Combine all runs of all paragraphs + StringBuilder builder = new StringBuilder(); + foreach (var paragraph in outputDocument.Paragraphs) + { + foreach (var run in paragraph.Runs) + { + builder.Append(run.GetText(0)); + } + } + + //Check + Assert.AreEqual("Regel1\nRegel2\nRegel3", builder.ToString()); + + //Check text was replaced correctly in table cell + var table = outputDocument.Tables.FirstOrDefault(); + Assert.IsNotNull(table); + + var dataRow = table.Rows[1]; + builder.Clear(); + foreach (var tableCellParagraph in dataRow.GetCell(0).Paragraphs) + { + foreach(var run in tableCellParagraph.Runs) + { + builder.Append(run.GetText(0)); + } + } + + //Check + Assert.AreEqual("Table replace multiple enters Regel1\nRegel2\nRegel3 text after last enter", builder.ToString()); + + } + [Test] public void TestAddPicture() { diff --git a/testcases/test-data/document/WordReplaceCRLF.docx b/testcases/test-data/document/WordReplaceCRLF.docx new file mode 100644 index 000000000..1e573b011 Binary files /dev/null and b/testcases/test-data/document/WordReplaceCRLF.docx differ