Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions OpenXmlFormats/Wordprocessing/wml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using NPOI.OpenXml4Net.Util;
using System.Xml;
using NPOI.OpenXmlFormats.Dml.WordProcessing;
using System.Text;

namespace NPOI.OpenXmlFormats.Wordprocessing
{
Expand Down Expand Up @@ -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 "<w:cr/>" 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;
}

Expand All @@ -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<parts.Length; i++)
{
sw.Write(XmlHelper.EncodeXml(parts[i]));
if(i < parts.Length - 1)
sw.Write("<w:cr/>");
}
}
sw.WriteEndW(nodeName);
}
Expand Down
49 changes: 49 additions & 0 deletions testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
{
Expand Down
Binary file added testcases/test-data/document/WordReplaceCRLF.docx
Binary file not shown.