OpenXmlRegex.Replace
- This topic has 1 reply, 1 voice, and was last updated 1 year, 8 months ago by .
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
It worked placeholderValue = placeholderValue.ReplaceLineEndings();
public void WordReplacePlaceHolders(string sourceFilePath, Dictionary<string, string> placeholders, string outputFilePath)
{
File.Copy(sourceFilePath, outputFilePath, true);
using (var doc = WordprocessingDocument.Open(outputFilePath, true))
{
foreach (var part in doc.ContentParts())
{
var xdoc = part.GetXDocument();
var content = xdoc.Descendants(W.p);
foreach (string placeholder in placeholders.Keys)
{
var regex = new Regex(placeholder, RegexOptions.IgnoreCase);
var placeholderValue = placeholders[placeholder];
placeholderValue = placeholderValue.ReplaceLineEndings();
OpenXmlRegex.Replace(content, regex, placeholderValue, null);
}
part.PutXDocument();
}
doc.Save();
}
}