コード例 #1
0
 public TextPart(int startIndex, int length, AODL.Document.Content.Text.FormatedText element)
 {
     _startIndex = startIndex;
     _length     = length;
     _element    = element;
 }
コード例 #2
0
        /// <summary>
        /// Parses a string in ODF format. The function replaces the foolowing special tags to format the text:
        ///     Bold:<b></b>
        ///     Italic:<i></i>
        ///     Underline:<u></u>
        ///     StrikeThrough:<s></s>
        /// </summary>
        /// <param name="paragraph">The text to parse.</param>
        /// <param name="doc">The ODF Textdoument that is being created</param>
        /// <returns>A list pf FormattedText that make up the paragraph.</returns>
        internal static List <AODL.Document.Content.Text.FormatedText> ParseParagraphForODF(IDocument doc, string paragraph)
        {
            List <TextPart> pars = new List <TextPart>();

            MatchCollection matches;

            //Bold Text
            matches = BoldRegex.Matches(paragraph);
            if (matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    //Create the formated text
                    var text = new AODL.Document.Content.Text.FormatedText(doc, "bold", m.Value.Substring(3, m.Value.Length - 7));
                    //Apply the style
                    text.TextStyle.TextProperties.Bold = "bold";
                    pars.Add(new TextPart(m.Index, m.Value.Length, text));
                }
            }

            //Italic Text
            matches = ItalicRegex.Matches(paragraph);
            if (matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    //Create the formated text
                    var text = new AODL.Document.Content.Text.FormatedText(doc, "italic", m.Value.Substring(3, m.Value.Length - 7));
                    //Apply the style
                    text.TextStyle.TextProperties.Italic = "italic";
                    pars.Add(new TextPart(m.Index, m.Value.Length, text));
                }
            }


            //Underlined Text
            matches = UnderlinedRegex.Matches(paragraph);
            if (matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    //Create the formated text
                    var text = new AODL.Document.Content.Text.FormatedText(doc, "underline", m.Value.Substring(3, m.Value.Length - 7));
                    //Apply the style
                    text.TextStyle.TextProperties.Underline = "solid";
                    pars.Add(new TextPart(m.Index, m.Value.Length, text));
                }
            }


            //Strike through Text
            matches = StrikedRegex.Matches(paragraph);
            if (matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    //Create the formated text
                    var text = new AODL.Document.Content.Text.FormatedText(doc, "strikethrough", m.Value.Substring(3, m.Value.Length - 7));
                    //Apply the style
                    text.TextStyle.TextProperties.TextLineThrough = "solid";
                    pars.Add(new TextPart(m.Index, m.Value.Length, text));
                }
            }

            //Create the list of OpenXmlElements in the correct order for the paragraph
            List <AODL.Document.Content.Text.FormatedText> p = new List <AODL.Document.Content.Text.FormatedText>();
            int index = 0;

            var sorted = from par in pars
                         orderby par.StartIndex
                         select par;

            foreach (var element in sorted)
            {
                //If the current index is not equal to the start index of the text part then there is plain text in between.
                if (element.StartIndex > index)
                {
                    //Create the formated text
                    var text = new AODL.Document.Content.Text.FormatedText(doc, "normal", paragraph.Substring(index, element.StartIndex - index));
                    p.Add(text);
                }
                p.Add(element.ODFElement);
                index = element.StartIndex + element.Length;
            }

            //Add any left over text
            if (paragraph.Substring(index).Length > 0)
            {
                //Create the formated text
                var text = new AODL.Document.Content.Text.FormatedText(doc, "T1", paragraph.Substring(index));
                p.Add(text);
            }

            return(p);
        }