Ejemplo n.º 1
0
 private static void ModifyWordParagraphTextContent(OXW.Paragraph paragraph, string txt)
 {
     if (null != paragraph)
     {
         OXW.Run run = paragraph.Descendants <OXW.Run>().FirstOrDefault();
         if (null != run)
         {
             run = run.CloneNode(true) as OXW.Run;
             paragraph.RemoveAllChildren <OXW.Run>();
         }
         else
         {
             run = new OXW.Run();
         }
         OXW.Text text = run.Descendants <OXW.Text>().FirstOrDefault();
         text = (null == text ? new OXW.Text() : text.CloneNode(true) as OXW.Text);
         run.RemoveAllChildren <OXW.Text>();
         text.Text = txt;
         if (!string.IsNullOrEmpty(txt) && (char.IsWhiteSpace(txt[0]) || char.IsWhiteSpace(txt[txt.Length - 1])))
         {
             text.Space = SpaceProcessingModeValues.Preserve;
         }
         run.Append(text);
         paragraph.Append(run);
     }
 }
Ejemplo n.º 2
0
        private static void ModifyWordParagraphTextContent(OpenXmlElement paragraph, string txt, string txtColor, string effect)
        {
            if (null == paragraph)
            {
                return;
            }
            OXW.Run run = paragraph.Descendants <OXW.Run>().FirstOrDefault();
            if (null != run)
            {
                run = run.CloneNode(true) as OXW.Run;
                paragraph.RemoveAllChildren <OXW.Run>();
            }
            else
            {
                run = new OXW.Run();
            }
            OXW.Text text = run?.Descendants <OXW.Text>().FirstOrDefault();
            text = (null == text ? new OXW.Text() : text.CloneNode(true) as OXW.Text);
            run?.RemoveAllChildren <OXW.Text>();
            if (txtColor != string.Empty && effect != string.Empty)
            {
                OXW.RunProperties runProperties1 = run?.Descendants <OXW.RunProperties>().FirstOrDefault();
                if (runProperties1 != null)
                {
                    runProperties1 = runProperties1.CloneNode(true) as OXW.RunProperties;
                    run.RemoveAllChildren <OXW.RunProperties>();
                }
                else
                {
                    runProperties1 = new OXW.RunProperties();
                }

                switch (effect)
                {
                case "bold":
                    OXW.Bold bold2 = new OXW.Bold();
                    runProperties1?.Append(bold2);
                    break;

                case "italic":
                    OXW.Italic italic2 = new OXW.Italic();
                    runProperties1?.Append(italic2);
                    break;

                case "bold+italic":
                    OXW.Bold bold3 = new OXW.Bold();
                    runProperties1?.Append(bold3);
                    OXW.Italic italic3 = new OXW.Italic();
                    runProperties1?.Append(italic3);
                    break;
                }

                OXW.Color color2 = new OXW.Color()
                {
                    Val = txtColor
                };
                runProperties1?.Append(color2);
                run?.Append(runProperties1);
            }
            if (text != null)
            {
                text.Text = txt;
                if (!string.IsNullOrEmpty(txt) && (char.IsWhiteSpace(txt[0]) || char.IsWhiteSpace(txt[txt.Length - 1])))
                {
                    text.Space = SpaceProcessingModeValues.Preserve;
                }
                run?.Append(text);
            }
            paragraph.Append(run);
        }