Ejemplo n.º 1
0
        public void Execute(bool upper)
        {
            using (var manager = new ApplicationManager())
            {
                var page = manager.CurrentPage();
                var ns   = page.GetNamespaceOfPrefix("one");

                var selections =
                    from e in page.Descendants(ns + "OE").Elements(ns + "T")
                    where e.Attributes("selected").Any(a => a.Value.Equals("all"))
                    select e;

                foreach (var selection in selections)
                {
                    if (selection.FirstNode?.NodeType == XmlNodeType.CDATA)
                    {
                        var wrapper = XElement.Parse("<x>" + selection.FirstNode.Parent.Value + "</x>");

                        foreach (var part in wrapper.DescendantNodes().OfType <XText>().ToList())
                        {
                            part.ReplaceWith(upper ? part.Value.ToUpper() : part.Value.ToLower());
                        }

                        selection.FirstNode.ReplaceWith(
                            new XCData(
                                string.Concat(wrapper.Nodes().Select(x => x.ToString()).ToArray())
                                )
                            );
                    }
                }

                manager.UpdatePageContent(page);
            }
        }
Ejemplo n.º 2
0
        public void Execute()
        {
            try
            {
                using (var manager = new ApplicationManager())
                {
                    page = new Page(manager.CurrentPage());
                    if (page != null)
                    {
                        var styles = new StyleProvider().GetStyles();

                        if (ApplyStyles(styles))
                        {
                            ApplyToLists(styles);

                            if (page.GetPageColor(out _, out _).GetBrightness() < 0.5)
                            {
                                ApplyToHyperlinks();
                            }

                            manager.UpdatePageContent(page.Root);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error executing {nameof(ApplyStylesCommand)}", exc);
            }
        }
Ejemplo n.º 3
0
        public void Execute()
        {
            try
            {
                var style = CollectStyleFromContext();

                if (style != null)
                {
                    Color pageColor;
                    using (var manager = new ApplicationManager())
                    {
                        pageColor = new Page(manager.CurrentPage()).GetPageColor(out _, out _);
                    }

                    DialogResult result;
                    using (var dialog = new StyleDialog(style, pageColor))
                    {
                        result = dialog.ShowDialog(owner);
                        if (result == DialogResult.OK)
                        {
                            style = dialog.Style;
                            if (style != null)
                            {
                                new StyleProvider().Save(style);
                                ribbon.Invalidate();
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error executing {nameof(NewStyleCommand)}", exc);
            }
        }
Ejemplo n.º 4
0
        private void InsertLine(char c)
        {
            using (var manager = new ApplicationManager())
            {
                var page = new Page(manager.CurrentPage());
                var ns   = page.Namespace;

                var dark  = page.GetPageColor(out _, out _).GetBrightness() < 0.5;
                var color = dark ? "#D0D0D0" : "#202020";

                var current =
                    (from e in page.Root.Descendants(ns + "OE")
                     where e.Elements(ns + "T").Attributes("selected").Any(a => a.Value.Equals("all"))
                     select e).FirstOrDefault();

                if (current != null)
                {
                    string line = string.Empty.PadRight(LineCharCount, c);

                    page.EnsurePageWidth(line, "Courier New", 10f, manager.WindowHandle);

                    current.AddAfterSelf(
                        new XElement(ns + "OE",
                                     new XElement(ns + "T",
                                                  new XAttribute("style", $"font-family:'Courier New';font-size:10.0pt;color:{color}"),
                                                  new XCData(line + "<br/>")
                                                  )
                                     ));

                    manager.UpdatePageContent(page.Root);
                }
            }
        }
Ejemplo n.º 5
0
        public void Execute()
        {
            logger.WriteLine("NoSpellCheckCommand.Execute()");

            using (var manager = new ApplicationManager())
            {
                var page = manager.CurrentPage();
                if (page != null)
                {
                    var ns = page.GetNamespaceOfPrefix("one");

                    // remove all occurances of the "lang=" attribute across the entire page

                    var list =
                        from e in page.DescendantsAndSelf()
                        where e.Attributes().Any(a => a.Name.LocalName.ToLower().Equals("lang"))
                        select e;

                    if (list?.Count() > 0)
                    {
                        foreach (var e in list)
                        {
                            e.Attribute("lang").Remove();
                        }
                    }

                    // set lang=yo for the page and the page title

                    page.Add(new XAttribute("lang", "yo"));
                    page.Element(ns + "Title")?.Add(new XAttribute("lang", "yo"));

                    manager.UpdatePageContent(page);
                }
            }
        }
Ejemplo n.º 6
0
        public void Execute()
        {
            using (manager = new ApplicationManager())
            {
                page = new Page(manager.CurrentPage(Microsoft.Office.Interop.OneNote.PageInfo.piAll));
                ns   = page.Namespace;

                var images = page.Root.Descendants(ns + "Image")?
                             .Where(e => e.Attribute("selected")?.Value == "all");

                if ((images == null) || (images.Count() == 0))
                {
                    images = page.Root.Descendants(ns + "Image");
                }

                if (images != null)
                {
                    if (images.Count() == 1)
                    {
                        // resize single selected image only
                        ResizeOne(images.First());
                    }
                    else
                    {
                        // select many iamges, or all if none selected
                        ResizeMany(images);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void Execute()
        {
            logger.WriteLine(nameof(NameUrlsCommand) + ".Execute()");

            try
            {
                Page page;
                using (var manager = new ApplicationManager())
                {
                    page = new Page(manager.CurrentPage());
                }

                if (NameUrls(page))
                {
                    using (var manager = new ApplicationManager())
                    {
                        manager.UpdatePageContent(page.Root);
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error executing {nameof(NameUrlsCommand)}", exc);
            }
        }
Ejemplo n.º 8
0
 public PasteRtfCommand() : base()
 {
     using (var manager = new ApplicationManager())
     {
         _ = new Page(manager.CurrentPage()).GetPageColor(out _, out black);
     }
 }
Ejemplo n.º 9
0
        private void _Execute(char c)
        {
            //System.Diagnostics.Debugger.Launch();

            using (var manager = new ApplicationManager())
            {
                var page = manager.CurrentPage();
                var ns   = page.GetNamespaceOfPrefix("one");

                var current =
                    (from e in page.Descendants(ns + "OE")
                     where e.Elements(ns + "T").Attributes("selected").Any(a => a.Value.Equals("all"))
                     select e).FirstOrDefault();

                if (current != null)
                {
                    string line = string.Empty.PadRight(90, c);

                    EnsurePageWidth(page, line, (IntPtr)manager.Application.Windows.CurrentWindow.WindowHandle);

                    current.AddAfterSelf(
                        new XElement(ns + "OE",
                                     new XElement(ns + "T",
                                                  new XAttribute("style", "font-family:'Courier New';font-size:10.0pt"),
                                                  new XCData(line + "<br/>")
                                                  )
                                     ));
                }

                manager.UpdatePageContent(page);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Initialize a new instance of the editor with the given manager
        /// </summary>
        /// <param name="manager">The OneNote application manager (abstraction)</param>
        public FootnoteEditor(ApplicationManager manager)
        {
            this.manager = manager;

            page = manager.CurrentPage();
            ns   = page.GetNamespaceOfPrefix("one");

            dark = new Page(page).GetPageColor(out _, out _).GetBrightness() < 0.5;

            logger = Logger.Current;
        }
Ejemplo n.º 11
0
        public void Execute()
        {
            using (var dialog = new EmojiDialog())
            {
                var result = dialog.ShowDialog(owner);

                if (result == DialogResult.OK)
                {
                    var codes = dialog.GetSelectedCodes();

                    using (var manager = new ApplicationManager())
                    {
                        var page = manager.CurrentPage();
                        var ns   = page.GetNamespaceOfPrefix("one");

                        var title = page.Element(ns + "Title")?
                                    .Elements(ns + "OE")?.Elements(ns + "T").FirstOrDefault();

                        if (title != null)
                        {
                            if (title.FirstNode?.NodeType == XmlNodeType.CDATA)
                            {
                                var wrapper = XElement.Parse("<x>" + title.FirstNode.Parent.Value + "</x>");
                                var wns     = wrapper.GetDefaultNamespace();

                                var emojii =
                                    (from e in wrapper.Elements(wns + "span")
                                     where e.Attributes("style").Any(a => a.Value.Contains("Segoe UI Emoji"))
                                     select e).FirstOrDefault();

                                if (emojii != null)
                                {
                                    emojii.Value = string.Join(string.Empty, codes) + emojii.Value;
                                }
                                else
                                {
                                    wrapper.AddFirst(new XElement("span",
                                                                  new XAttribute("style", "font-family:'Segoe UI Emoji';font-size:16pt;"),
                                                                  string.Join(string.Empty, codes)
                                                                  ));
                                }

                                var decoded = string.Concat(wrapper.Nodes()
                                                            .Select(x => x.ToString()).ToArray())
                                              .Replace("&amp;", "&");

                                title.FirstNode.ReplaceWith(new XCData(decoded));
                                manager.UpdatePageContent(page);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        private void pageInfoBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Enum.TryParse <PageInfo>(pageInfoBox.Text, out var info))
            {
                var page = manager.CurrentPage(info);
                if (page != null)
                {
                    var xml = page.ToString(SaveOptions.None);
                    pageBox.Text = xml;

                    logger.WriteLine("XmlDialog loaded page, " + xml.Length + " chars");
                }
            }
        }
Ejemplo n.º 13
0
 public void Execute(int selectedIndex)
 {
     using (var style = new StylesProvider().GetStyle(selectedIndex, false))
     {
         using (var manager = new ApplicationManager())
         {
             var page = manager.CurrentPage();
             if (page != null)
             {
                 EvaluatePage(page, style, manager);
             }
         }
     }
 }
Ejemplo n.º 14
0
        private void _Execute()
        {
            using (var manager = new ApplicationManager())
            {
                page = manager.CurrentPage();
                if (page != null)
                {
                    ns = page.GetNamespaceOfPrefix("one");

                    Evaluate(manager);

                    manager.UpdatePageContent(page);
                }
            }
        }
Ejemplo n.º 15
0
        public void Execute(bool increase = true)
        {
            using (var manager = new ApplicationManager())
            {
                page = manager.CurrentPage();
                if (page != null)
                {
                    ns = page.GetNamespaceOfPrefix("one");

                    Evaluate(increase);

                    manager.UpdatePageContent(page);
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Analyze the current page and determine if the text cursor is positioned in the body
        /// of the page or in the title.
        /// </summary>
        /// <param name="control">
        /// The menu item invoking the action. This should be done by one "controller" item so
        /// optimize performance. All other menu items can rely on the GetBodyContext method.
        /// </param>
        /// <returns>True</returns>
        public bool SetBodyContext(IRibbonControl control)
        {
            //Logger.Current.WriteLine($"SetBodyContext {control.Id}");

            Page page;

            using (var manager = new ApplicationManager()) { page = new Page(manager.CurrentPage()); }

            // set the context for the getters
            bodyContext   = page.ConfirmBodyContext();
            imageSelected = page.ConfirmImageSelected();

            // the setter always returns true; the getter will return bodyContext
            return(true);
        }
Ejemplo n.º 17
0
        private void InsertExpand()
        {
            using (var manager = new ApplicationManager())
            {
                var page = new Page(manager.CurrentPage());
                var ns   = page.Namespace;

                if (!page.ConfirmBodyContext(true))
                {
                    return;
                }

                var table = new Table(ns);
                table.SetBordersVisible(true);
                table.AddColumn(550, true);
                var row = table.AddRow();
                row.SetCellContent(0, "Your content here");

                var expand = new XElement(ns + "OE",
                                          new XAttribute("explicitExpandCollapseEnabled", "true"),
                                          new XElement(ns + "T", new XCData("Your title here")),
                                          new XElement(ns + "OEChildren",
                                                       new XElement(ns + "OE", table.Root),
                                                       new XElement(ns + "OE", new XElement(ns + "T", new XCData(string.Empty)))
                                                       )
                                          );

                // find the first (the default) paragraph style so we can apply it to the
                // new content; this help when the page background is dark
                var quickdef = page.Root.Elements(ns + "QuickStyleDef")
                               .Where(e => e.Attribute("name").Value == "p")
                               .Select(e => e.Attribute("index")?.Value)
                               .FirstOrDefault();

                if (quickdef != null)
                {
                    expand.Add(new XAttribute("quickStyleIndex", quickdef));
                    foreach (var oe in expand.Descendants(ns + "OE"))
                    {
                        oe.Add(new XAttribute("quickStyleIndex", quickdef));
                    }
                }

                page.AddNextParagraph(expand);
                manager.UpdatePageContent(page.Root);
            }
        }
Ejemplo n.º 18
0
        public void Execute()
        {
            using (var manager = new ApplicationManager())
            {
                var page = new Page(manager.CurrentPage(Microsoft.Office.Interop.OneNote.PageInfo.piAll));
                var ns   = page.Namespace;

                var image = page.Root.Descendants(ns + "Image")?
                            .Where(e => e.Attribute("selected").Value.Equals("all"))
                            .FirstOrDefault();

                if (image != null)
                {
                    if (AlreadyCaptioned(image, ns, manager))
                    {
                        return;
                    }

                    var table = new Table(ns);
                    table.AddColumn(0f);                     // OneNote will set width accordingly

                    var cdata = new XCData("Caption");

                    var row = table.AddRow();
                    row.SetCellContent(0,
                                       new XElement(ns + "OEChildren",
                                                    new XElement(ns + "OE",
                                                                 new XAttribute("alignment", "center"),
                                                                 image),
                                                    new XElement(ns + "OE",
                                                                 new XAttribute("alignment", "center"),
                                                                 new XElement(ns + "Meta",
                                                                              new XAttribute("name", "om"),
                                                                              new XAttribute("content", "caption")),
                                                                 new XElement(ns + "T", cdata)
                                                                 )
                                                    ));

                    var style = GetStyle();
                    new Stylizer(style).ApplyStyle(cdata);

                    image.ReplaceWith(table.Root);
                    manager.UpdatePageContent(page.Root);
                }
            }
        }
Ejemplo n.º 19
0
        public IStream GetStyleGalleryItemImage(IRibbonControl control, int itemIndex)
        {
            //logger.WriteLine($"GetStyleGalleryItemImage({control.Id}, {itemIndex})");

            Color pageColor;

            using (var manager = new ApplicationManager())
            {
                pageColor = new Page(manager.CurrentPage()).GetPageColor(out _, out var black);
                if (black)
                {
                    pageColor = ColorTranslator.FromHtml("#201F1E");
                }
            }

            return(factory.GetCommand <GalleryTileFactory>().MakeTile(itemIndex, pageColor));
        }
Ejemplo n.º 20
0
        //========================================================================================
        // Addin menu behaviors

        public bool EnsureBodyContext(IRibbonControl control)
        {
            XElement page;

            using (var manager = new ApplicationManager()) { page = manager.CurrentPage(); }

            var ns = page.GetNamespaceOfPrefix("one");

            var found = page.Elements(ns + "Outline")?
                        .Descendants(ns + "T")?
                        .Attributes("selected")?
                        .Any(a => a.Value.Equals("all"));

            ribbon.Invalidate();

            return(found ?? true);
        }
Ejemplo n.º 21
0
        public void Execute()
        {
            logger.WriteLine($"{nameof(ChangePageColorCommand)}.Execute()");

            Page page = null;

            using (var manager = new ApplicationManager())
            {
                page = new Page(manager.CurrentPage());
            }

            using (var dialog = new ChangePageColorDialog(page.GetPageColor(out _, out _)))
            {
                if (dialog.ShowDialog(owner) == DialogResult.OK)
                {
                    var element = page.Root
                                  .Elements(page.Namespace + "PageSettings")
                                  .FirstOrDefault();

                    if (element != null)
                    {
                        var attr = element.Attribute("color");
                        if (attr != null)
                        {
                            attr.Value = dialog.PageColor;
                        }
                        else
                        {
                            element.Add(new XAttribute("color", dialog.PageColor));
                        }

                        using (var manager = new ApplicationManager())
                        {
                            manager.UpdatePageContent(page.Root);
                        }
                    }
                    else
                    {
                        logger.WriteLine("ChangePageColor failed because PageSettings not found");
                    }
                }
            }
        }
Ejemplo n.º 22
0
        private void _Execute()
        {
            DialogResult result = DialogResult.None;
            string       whatText;
            string       withText;
            bool         matchCase;

            using (var dialog = new SearchAndReplaceDialog())
            {
                result    = dialog.ShowDialog();
                whatText  = dialog.WhatText;
                withText  = dialog.WithText;
                matchCase = dialog.MatchCase;
            }

            if (result == DialogResult.OK)
            {
                using (var manager = new ApplicationManager())
                {
                    var page = manager.CurrentPage();
                    var ns   = page.GetNamespaceOfPrefix("one");

                    var ranges = page.Elements(ns + "Outline")?.Descendants(ns + "T")
                                 .Where(e => !e.DescendantNodes().OfType <XCData>().Any(d => d.Value.Equals(string.Empty)));

                    if (ranges != null)
                    {
                        var count = 0;

                        foreach (var range in ranges)
                        {
                            count += Replace(range, whatText, withText, matchCase);
                        }

                        manager.UpdatePageContent(page);

                        var msg = count == 1 ? "occurance was" : "occurances were";
                        MessageBox.Show($"{count} {msg} replaced", "Replaced",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
Ejemplo n.º 23
0
        private void ChangeInfoScope(object sender, EventArgs e)
        {
            if (Enum.TryParse <PageInfo>(pageInfoBox.Text, out var info))
            {
                var page = manager.CurrentPage(info);
                if (page != null)
                {
                    var xml = page.ToString(SaveOptions.None);
                    //var pi = manager.GetCurrentPageInfo();

                    pageBox.Text =
                        //$"Name=[{pi.Name}]" + Environment.NewLine +
                        //$"Path=[{pi.Path}]" + Environment.NewLine +
                        //$"Link=[{pi.Link}]" + Environment.NewLine +
                        //Environment.NewLine +
                        xml;

                    logger.WriteLine("XmlDialog loaded page, " + xml.Length + " chars");
                }
            }
        }
Ejemplo n.º 24
0
        public override void Execute(params object[] args)
        {
            // transform RTF and Xaml data on clipboard to HTML

            var html = PrepareClipboard();

            //logger.WriteLine(html);
#if H
            if (html != null)
            {
                // TODO: find and replace selected region

                logger.WriteLine("Adding HTML blcok");
                using (var manager = new ApplicationManager())
                {
                    var page = new OM.Page(manager.CurrentPage(PageInfo.piBasic));
                    var ns   = page.Namespace;

                    var outline = page.Root.Elements(ns + "Outline").Elements(ns + "OEChildren").FirstOrDefault();

                    outline.Add(new XElement(ns + "HTMLBlock",
                                             new XElement(ns + "Data", new XCData(html))
                                             ));

                    manager.UpdatePageContent(page.Root);
                    return;
                }
            }
#endif
            // paste what's remaining from clipboard, letting OneNote do the
            // heavy lifting of converting the HTML into one:xml schema

            using (var one = new OneNote())
            {
                // since the Hotkey message loop is watching all input, explicitly setting
                // focus on the OneNote main window provides a direct path for SendKeys
                Native.SetForegroundWindow(one.WindowHandle);
                System.Windows.Forms.SendKeys.SendWait("^(v)");
            }
        }
Ejemplo n.º 25
0
        public void Execute(int selectedIndex)
        {
            using (var manager = new ApplicationManager())
            {
                page = manager.CurrentPage();
                if (page != null)
                {
                    ns       = page.GetNamespaceOfPrefix("one");
                    style    = new StyleProvider().GetStyle(selectedIndex);
                    stylizer = new Stylizer(style);

                    bool success = style.StyleType == StyleType.Character
                                                ? StylizeWords()
                                                : StylizeParagraphs();

                    if (success)
                    {
                        manager.UpdatePageContent(page);
                    }
                }
            }
        }
Ejemplo n.º 26
0
        public void Execute(int delta)
        {
            try
            {
                using (var manager = new ApplicationManager())
                {
                    page = manager.CurrentPage();
                    if (page != null)
                    {
                        this.delta = delta;

                        // determine if range is selected or entire page

                        var ns = page.GetNamespaceOfPrefix("one");

                        selected = page.Element(ns + "Outline").Descendants(ns + "T")
                                   .Where(e => e.Attributes("selected").Any(a => a.Value.Equals("all")))
                                   .Any(e => e.GetCData().Value.Length > 0);

                        //System.Diagnostics.Debugger.Launch();

                        var count = AlterByName();
                        count += AlterElementsByValue();
                        count += AlterCDataByValue();

                        if (count > 0)
                        {
                            manager.UpdatePageContent(page);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine("Error executing AlterSizeCommand", exc);
            }
        }
Ejemplo n.º 27
0
        public void Execute()
        {
            try
            {
                Color pageColor;
                using (var manager = new ApplicationManager())
                {
                    pageColor = new Page(manager.CurrentPage()).GetPageColor(out _, out var black);
                    if (black)
                    {
                        pageColor = ColorTranslator.FromHtml("#201F1E");
                    }
                }

                var provider = new StyleProvider();

                var          styles = provider.GetStyles();
                DialogResult result;

                using (var dialog = new StyleDialog(styles, pageColor))
                {
                    result = dialog.ShowDialog(owner);

                    if (result == DialogResult.OK)
                    {
                        // save styles to remove delete items and preserve ordering
                        styles = dialog.GetStyles();
                        provider.Save(styles);
                        ribbon.Invalidate();
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error executing {nameof(EditStylesCommand)}", exc);
            }
        }
Ejemplo n.º 28
0
        public void Execute()
        {
            try
            {
                using (var manager = new ApplicationManager())
                {
                    page = manager.CurrentPage();
                    if (page != null)
                    {
                        ns     = page.GetNamespaceOfPrefix("one");
                        pageId = page.Attribute("ID").Value;

                        CollectHeadingDefinitions();

                        Evaluate(manager);
                        manager.UpdatePageContent(page);
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error executing {nameof(InsertTocCommand)}", exc);
            }
        }
Ejemplo n.º 29
0
        private void InsertInfoBlock(bool warning)
        {
            using (var manager = new ApplicationManager())
            {
                var page = new Page(manager.CurrentPage());
                var ns   = page.Namespace;

                var dark = page.GetPageColor(out _, out var black).GetBrightness() < 0.5;

                string title, symbol, titleColor, symbolColor, textColor, shading;

                if (warning)
                {
                    title       = "Warning";
                    symbol      = "\u26a0";
                    symbolColor = black ? WarnSymbolBlack : (dark ? WarnSymbolBlack : WarnSymbolColor);
                    shading     = black ? WarnShadingBlack : (dark ? WarnShadingDark : WarnShading);
                }
                else
                {
                    title       = "Information";
                    symbol      = "\U0001F6C8";
                    symbolColor = black ? InfoSymbolBlack : InfoSymbolColor;
                    shading     = black ? InfoShadingBlack : (dark ? InfoShadingDark : InfoShading);
                }

                titleColor = black ? TItleBlack : (dark ? TItleDark : TitleColor);
                textColor  = black ? TextBlack : (dark ? TextDark : TextColor);

                // table...

                var inner = new Table(ns);
                inner.AddColumn(37f, true);
                inner.AddColumn(100f);

                var row = inner.AddRow();

                row.SetCellContent(0, new XElement(ns + "OE",
                                                   new XAttribute("alignment", "center"),
                                                   new XAttribute("style", $"font-family:'Segoe UI Symbol';font-size:22.0pt;color:{symbolColor};&#xA;text-align:center"),
                                                   new XElement(ns + "T",
                                                                new XCData($"<span style='font-weight:bold'>{symbol}</span>"))
                                                   ));

                row.SetCellContent(1, new XElement(ns + "OEChildren",
                                                   new XElement(ns + "OE",
                                                                new XAttribute("style", $"font-family:'Segoe UI';font-size:11.0pt;color:{titleColor}"),
                                                                new XElement(ns + "T",
                                                                             new XCData($"<span style='font-weight:bold'>{title}</span>"))
                                                                ),
                                                   new XElement(ns + "OE",
                                                                new XAttribute("style", $"font-family:'Segoe UI';font-size:11.0pt;color:{textColor}"),
                                                                new XElement(ns + "T",
                                                                             new XCData("Your content here..."))
                                                                )));

                var outer = new Table(ns, shading);
                outer.SetBordersVisible(true);
                outer.AddColumn(600f, true);
                row = outer.AddRow();
                row.SetCellContent(0, inner);

                page.AddNextParagraph(outer.Root);
                manager.UpdatePageContent(page.Root);
            }
        }
Ejemplo n.º 30
0
        public void Execute(bool upper)
        {
            using (var manager = new ApplicationManager())
            {
                var page = manager.CurrentPage();
                var ns   = page.GetNamespaceOfPrefix("one");

                var cursor = page.Descendants(ns + "T")
                             .Where(e =>
                                    e.Attributes("selected").Any(a => a.Value.Equals("all")) &&
                                    e.FirstNode.NodeType == XmlNodeType.CDATA &&
                                    ((XCData)e.FirstNode).Value.Length == 0)
                             .FirstOrDefault();

                if (cursor != null)
                {
                    var prev = cursor.PreviousNode as XElement;
                    if ((prev != null) && prev.GetCData().EndsWithWhitespace())
                    {
                        prev = null;
                    }

                    var next = cursor.NextNode as XElement;
                    if ((next != null) && next.GetCData().StartsWithWhitespace())
                    {
                        next = null;
                    }

                    if ((prev != null) && (next != null))
                    {
                        var word = new StringBuilder();

                        if (prev != null)
                        {
                            if (!prev.GetCData().EndsWithWhitespace())
                            {
                                word.Append(prev.ExtractLastWord());
                                if (prev.GetCData().Value.Length == 0)
                                {
                                    prev.Remove();
                                }
                            }
                        }

                        if (next != null)
                        {
                            if (!next.GetCData().StartsWithWhitespace())
                            {
                                word.Append(next.ExtractFirstWord());
                                if (next.GetCData().Value.Length == 0)
                                {
                                    next.Remove();
                                }
                            }
                        }

                        if (word.Length > 0)
                        {
                            var text = upper ? word.ToString().ToUpper() : word.ToString().ToLower();

                            cursor.DescendantNodes()
                            .OfType <XCData>()
                            .First()
                            .ReplaceWith(new XCData(text));
                        }
                    }
                }
                else
                {
                    var selections =
                        from e in page.Descendants(ns + "OE").Elements(ns + "T")
                        where e.Attributes("selected").Any(a => a.Value.Equals("all"))
                        select e;

                    if (selections?.Any() == true)
                    {
                        foreach (var selection in selections)
                        {
                            if (selection.FirstNode?.NodeType == XmlNodeType.CDATA)
                            {
                                var wrapper = XElement.Parse("<x>" + selection.FirstNode.Parent.Value + "</x>");

                                foreach (var part in wrapper.DescendantNodes().OfType <XText>().ToList())
                                {
                                    part.ReplaceWith(upper ? part.Value.ToUpper() : part.Value.ToLower());
                                }

                                selection.FirstNode.ReplaceWith(
                                    new XCData(
                                        string.Concat(wrapper.Nodes().Select(x => x.ToString()).ToArray())
                                        ));
                            }
                        }
                    }
                }

                manager.UpdatePageContent(page);
            }
        }