Skip to content

Commit

Permalink
Merge pull request #1 from dgwaldo/dgwaldo-handle-newline
Browse files Browse the repository at this point in the history
Adds handling of newline characters in text.
  • Loading branch information
dgwaldo authored Jun 10, 2020
2 parents 53911ca + f04df83 commit d027758
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions OpenXMLTemplates/ControlReplacers/ControlReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,39 @@ private bool ValidateAndExtractTag(string tag, out string variableIdentifier, ou
}


/// <summary>
/// Sets the text of the OpenXmlElement and removes the default placeholder style that is associated by default with content controls
/// </summary>
protected static void SetTextAndRemovePlaceholderFormat(OpenXmlElement element, string newValue) {
if (newValue == null)
return;

/// <summary>
/// Sets the text of the OpenXmlElement and removes the default placeholder style that is associated by default with content controls
/// </summary>
protected static void SetTextAndRemovePlaceholderFormat(OpenXmlElement element, string newValue)
{
if (newValue == null)
return;
string[] newlineArray = { Environment.NewLine, "\n", "\r\n", "\n\r" };
string[] textArray = newValue.Split(newlineArray, StringSplitOptions.None);
bool first = true;

Text textElement = element.SetText(newValue);
var textElement = element.GetTextElement();
var textElementParent = textElement.Parent;

//Check if the style is the default placeholder style and remove it if it is
if (textElement?.Parent is Run run && run.RunProperties?.RunStyle?.Val == "PlaceholderText")
run.RunProperties.RunStyle.Val = "";
}
foreach (var line in textArray) {

if (!first) {
textElementParent.Append(new Break());
}

textElement.Parent.Append(new Text(line));

first = false;
}

//Check if the style is the default placeholder style and remove it if it is
if (textElement?.Parent is Run run && run.RunProperties?.RunStyle?.Val == "PlaceholderText") {
run.RunProperties.RunStyle.Val = "";
}

textElement.Remove();

}

}
}
}

0 comments on commit d027758

Please sign in to comment.