Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multi-line support #4

Merged
merged 1 commit into from
Apr 12, 2024
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
48 changes: 48 additions & 0 deletions NPOI.WordTemplateMapper/Extensions/XWPF/XWPFParagraphExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NPOI.XWPF.UserModel;
using System.Text.RegularExpressions;

namespace NPOI.WordTemplateMapper.Extensions.XWPF;

public static class XWPFParagraphExtensions
{
public static void ReplaceTextKeepLineEndings(this XWPFParagraph xwpfParagraph, string textToReplace, string replacementText)
{
string paragraphText = xwpfParagraph.Text;

if (!paragraphText.Contains(textToReplace))
return;

if (!replacementText.Contains('\n'))
{
string textToInsert = paragraphText.Replace(textToReplace, replacementText);
if (!string.IsNullOrEmpty(xwpfParagraph.Text))
xwpfParagraph.ReplaceText(xwpfParagraph.Text, textToInsert);
return;
}

List<int> positionsOfOldTextOccurences = new();
positionsOfOldTextOccurences
.AddRange(
Regex.Matches(paragraphText, Regex.Escape(textToReplace))
.Select(match => match.Index)
);

for (int i = positionsOfOldTextOccurences.Count - 1; i >= 0; i--)
{
int currentPosition = positionsOfOldTextOccurences[i];
paragraphText = paragraphText
.Remove(currentPosition, textToReplace.Length)
.Insert(currentPosition, replacementText);
}

string[] newParagraphsToInsert = paragraphText.Split('\n');
xwpfParagraph.ReplaceText(xwpfParagraph.Text, string.Empty);

XWPFRun run = xwpfParagraph.CreateRun();
foreach (string newParagraph in newParagraphsToInsert)
{
run.AppendText(newParagraph);
run.AddCarriageReturn();
}
}
}
6 changes: 2 additions & 4 deletions NPOI.WordTemplateMapper/XWPF/XWPFParagraphMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NPOI.WordTemplateMapper.Interfaces.XWPF;
using System.Text.RegularExpressions;
using System.Collections;
using NPOI.WordTemplateMapper.Extensions.XWPF;

namespace NPOI.WordTemplateMapper.XWPF;

Expand Down Expand Up @@ -33,10 +34,7 @@ public XWPFParagraph MapParagraph(XWPFParagraph paragraph, IDictionary<string, o
KeyValuePair<string, string> mappedValue = GetMappedValue(paragraph, mapping);
string oldText = paragraph.Text;

// Workaround for malfunctioning ReplaceText from NPOI
string newText = paragraph.Text.Replace(mappedValue.Key, mappedValue.Value);
if (!string.IsNullOrEmpty(paragraph.Text))
paragraph.ReplaceText(paragraph.Text, newText);
paragraph.ReplaceTextKeepLineEndings(mappedValue.Key, mappedValue.Value);

if (oldText == paragraph.Text)
keepMapping = false;
Expand Down
Loading