private static Paragraph GenerateRuns(Paragraph p, string Buffer) { // Calculate positions of all tokens and use this to set // run styles when iterating through the string // in the same calculation note down location of tokens // so they can be ignored when loading strings into the buffer // Is this really needed? Regex pBold, pItalic, pUnderline, pHyperlink_description, pHyperlink; if (!CustomMode) pItalic = Patterns[rExKey.Asterisk]; else pItalic = Patterns[rExKey.Grave]; pUnderline = Patterns[rExKey.Underscore]; pBold = Patterns[rExKey.DblAsterisk]; pHyperlink = Patterns[rExKey.Hyperlink]; pHyperlink_description = Patterns[rExKey.Hyperlink_description]; Ranges<int> Tokens = new Ranges<int>(); Ranges<int> Bold = FindMatches(pBold, Buffer, ref Tokens); Ranges<int> Italic = FindMatches(pItalic, Buffer, ref Tokens); Ranges<int> Underline = FindMatches(pUnderline, Buffer, ref Tokens); Ranges<int> Tabs = FindTabs(new Regex(@"(\\t|[\\ ]{4})"), Buffer, ref Tokens); // TODO: make this a boolean , overload FindMatches or something. Ranges<int> Hyperlinks_description = FindMatches(pHyperlink_description, Buffer, ref Tokens); Ranges<int> Hyperlinks = FindMatches(pHyperlink, Buffer, ref Tokens); if ((Bold.Count() + Italic.Count() + Underline.Count() + Tabs.Count() + Hyperlinks_description.Count() + Hyperlinks.Count()) == 0) { Run run = new Run(); run.Append(new Text(Buffer) { Space = SpaceProcessingModeValues.Preserve }); p.Append(run); } else { int CurrentPosition = 0; Run run; // This needs optimizing so it builds a string buffer before adding the run itself // might need to make this into a Switch statement also if (Hyperlinks_description.Count() == 1) { AppendHyperlink(Buffer, ref p, true); } else if (Hyperlinks.Count() == 1) { AppendHyperlink(Buffer, ref p, false); } else { while (CurrentPosition < Buffer.Length) { if (!Tokens.ContainsValue(CurrentPosition) || Tabs.ContainsValue(CurrentPosition)) { run = new Run(); if (Tabs.ContainsValue(CurrentPosition)) { run.Append(new TabChar()); } else { RunProperties rPr = new RunProperties(); if (Bold.ContainsValue(CurrentPosition)) rPr.Append(new Bold() { Val = new OnOffValue(true) }); if (Italic.ContainsValue(CurrentPosition)) rPr.Append(new Italic()); if (Underline.ContainsValue(CurrentPosition)) rPr.Append(new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }); run.Append(rPr); string TextBuffer = Buffer.Substring(CurrentPosition, 1); run.Append(new Text(TextBuffer) { Space = SpaceProcessingModeValues.Preserve }); } p.Append(run); } CurrentPosition++; }; } } return p; }
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(); }