private void AppendText(BasicContentBlock element, Paragraph paragraph) { TextContentBlock Text = (TextContentBlock)element; ParagraphProperties pPr = new ParagraphProperties(); RunProperties rPr = new RunProperties(); Ranges <int> Tokens = new Ranges <int>(); Italic = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Italic); Italic.FindMatches(Text.Content, ref Tokens); Bold = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Bold); Bold.FindMatches(Text.Content, ref Tokens); Underline = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Underline); Underline.FindMatches(Text.Content, ref Tokens); BulletList = new PatternMatcher(Pattern.BulletList); BulletList.FindMatches(Text.Content, ref Tokens); if (!PatternsHaveMatches()) { run = new Run(); run.Append(new Text(Text.Content) { Space = SpaceProcessingModeValues.Preserve }); paragraph.Append(run); } else { int pos = 0; string buffer = ""; while (pos < Text.Content.Length) { if (!Tokens.ContainsValue(pos)) { buffer += Text.Content.Substring(pos, 1); } else if (buffer.Length > 0) { run = new Run(); Bold.SetFlagFor(pos - 1); Italic.SetFlagFor(pos - 1); Underline.SetFlagFor(pos - 1); BulletList.SetFlagFor(pos - 1); // this is the place where the code should be Refactored to catch the pattern and pass it properly if (Bold.Flag) { rPr.Append(new Bold() { Val = new OnOffValue(true) }); } if (Italic.Flag) { rPr.Append(new Italic()); } if (Underline.Flag) { rPr.Append(new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }); } if (BulletList.Flag) { rPr.Append(new NumberingFormat() { Val = NumberFormatValues.Bullet }); } run.Append(new Text(buffer) { Space = SpaceProcessingModeValues.Preserve }); //Need to do Some code changes to cath the text after Bold //run.Append(rPr); paragraph.Append(run); buffer = ""; } pos++; } ; } ////Assign the properties to the paragraph and the run. //Paragraph para = new Paragraph() //{ // ParagraphProperties = pPr //}; //Run runs = new Run(new Word.Text(Text.Content)) //{ // RunProperties = rPr //}; //paragraph.Append(runs); run.Append(rPr); Document.MainDocumentPart.Document.Body.Append(paragraph); Document.Save(); }