OpenXmlRegex.Replace

Home Forums Tools OpenXmlRegex.Replace

This topic contains 1 reply, has 1 voice, and was last updated by  rabeeh 6 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #11346

    rabeeh
    Participant

    Hi Eric,
    Quick question how to use OpenXmlRegex.Replace to replace placeholder with content that has text and carriage return?

    The placeholderValue can paragraph

    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];

    OpenXmlRegex.Replace(content, regex, placeholderValue, null);
    }

    part.PutXDocument();
    }

    doc.Save();
    }

    }

    #11347

    rabeeh
    Participant

    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();
    }

    }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.