Issue merging rich text content into word document using OPEN XML-AltChuck RRS feed

  • Question

  • Hello all - During a  merge process  in a docx with rich text content, below approach is used.

    • Place holders are configured in docx as "field code" as comments .
    • Get the rich text html content from the application.
    • Create a new Open XML altchunk element and embed the html content.
    • Append the updated altchunk into the respective placeholders in the word doc.
    • Issue is the thought  the place holder is in the middle of the sentence or paragraph, the value is getting appended in the end of the statement.  {text1}  is the place holder and Text underlined is the merged text  in the below example. Any suggestion/alternates ?

    Eg:  This is the {test1} content

    Expected result after merge :  This is the merged content 

    Actual result : This is the content  merged .(appends at the end).

    Code Snippet below.

    if (!string.IsNullOrEmpty(dynamicField?.HtmlContent) && !dynamicField.HtmlContent.Equals("undefined"))
                                                    var htmlContentToBeReplaced = "<html><head><style type='text/css'>  .dynamicfreetext {font-family: Times New Roman !important;font-size: 12pt !important;} </style>" +
                                                                      "</head><body><pre><div class = 'dynamicfreetext'>" + dynamicField.HtmlContent + "</div></pre></body></html>";
                                                   var doc = new HtmlDocument();
                                                   var newListItemDetails = new List<Tuple<string, string>>();
                                                    var nodesList = doc.DocumentNode.Descendants();
                                                       if (newListItemDetails.Any())
                                                            foreach (var newListItem in newListItemDetails)
                                                                htmlContentToBeReplaced = htmlContentToBeReplaced.Replace(newListItem.Item1, newListItem.Item2);
                                                   var parentNode = p?.Parent;
                                                   var altChunkId = "richTextId" + Guid.NewGuid().ToString();
                                                    var ms = new MemoryStream(System.Text.Encoding.Unicode.GetPreamble().Concat(System.Text.Encoding.Unicode.GetBytes(htmlContentToBeReplaced)).ToArray());
                                                   // Create alternative format import part.
                                                    var formatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
                                                   // Feed HTML data into format import part (chunk).
                                                    var altChunk = new AltChunk { Id = altChunkId };
                                                    altChunk.AltChunkProperties = new AltChunkProperties { MatchSource = new MatchSource { Val = true } };
                                                    CreateRunForDynamicRichTextArea(ref parentNode, ref runList, altChunk);
     private static void CreateRunForDynamicRichTextArea(ref OpenXmlElement parentNode, ref List<Run> runList, AltChunk altChunk)
                 var txtNodes = (from run in runList
                                 from text in run.Descendants<Text>()
                                 select text);
                if (txtNodes != null)
                     var txtContents = txtNodes.ToList();
                     foreach (var txtContent in txtContents)
                         txtContent.Text = string.Empty;
                var runWithHtmlContent = new Run();

    • Changed type KareninstructorMVP, Moderator Wednesday, January 22, 2020 12:44 AM This is not a discussion, it's a question
    • Edited by Noorul L Wednesday, January 22, 2020 6:56 PM coorection
    Tuesday, January 21, 2020 10:40 PM

All replies

  • Hello,

    Please edit your post and place code into code blocks.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Wednesday, January 22, 2020 12:46 AM
  • Hi Noorul L,
    Based on your description, I can't see the "expected result after merge" and "Actual result".
    >> Red boxed text in the below example
    And where is Red boxed text you pointed?
    Please describe it in detail.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, January 22, 2020 2:39 AM
  • HI Daniel .. Using underline instead of redbox.. Updated the example ..
    Wednesday, January 22, 2020 6:56 PM