Example #1
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        #region InsertPagesTables
        private void InsertPagesTable(OneNote one)
        {
            var section   = one.GetSection();
            var sectionId = section.Attribute("ID").Value;

            one.CreatePage(sectionId, out var pageId);

            var page = one.GetPage(pageId);
            var ns   = page.Namespace;

            page.Title = string.Format(Resx.InsertTocCommand_TOCSections, section.Attribute("name").Value);

            var container = new XElement(ns + "OEChildren");

            var elements = section.Elements(ns + "Page");

            foreach (var element in elements)
            {
                var text  = new StringBuilder();
                var level = int.Parse(element.Attribute("pageLevel").Value);
                while (level > 0)
                {
                    text.Append(". . ");
                    level--;
                }

                var link = one.GetHyperlink(element.Attribute("ID").Value, string.Empty);

                var name = element.Attribute("name").Value;
                text.Append($"<a href=\"{link}\">{name}</a>");

                container.Add(new XElement(ns + "OE",
                                           new XElement(ns + "T", new XCData(text.ToString())
                                                        )));
            }

            var title = page.Root.Elements(ns + "Title").FirstOrDefault();

            title.AddAfterSelf(new XElement(ns + "Outline", container));
            one.Update(page);

            // move TOC page to top of section...

            // get current section again after new page is created
            section = one.GetSection();

            var entry = section.Elements(ns + "Page")
                        .FirstOrDefault(e => e.Attribute("ID").Value == pageId);

            entry.Remove();
            section.AddFirst(entry);
            one.UpdateHierarchy(section);

            one.NavigateTo(pageId);
        }
Example #2
0
        /// <summary>
        /// Add a footnote to the footer section at the bottom of the outline
        /// </summary>
        /// <param name="textId">The objectId of the paragraph containing the cursor</param>
        /// <returns>The footnote label, the footnote ID#</returns>
        private async Task <string> WriteFootnoteText(string textId)
        {
            // find next footnote label
            var label = (divider.NodesAfterSelf()
                         .OfType <XElement>().Elements(ns + "Meta")
                         .Where(e => e.Attribute("name").Value.Equals("omfootnote"))
                         .DefaultIfEmpty()         // avoids null ref exception
                         .Max(e => e == null ? 0 : int.Parse(e.Attribute("content").Value))
                         + 1).ToString();

            // find last footnote (sibling) element after which new note is to be added
            var last = divider.NodesAfterSelf()
                       .OfType <XElement>().Elements(ns + "Meta")
                       .LastOrDefault(e => e.Attribute("name").Value.Equals("omfootnote"));

            // divider is a valid precedesor sibling; otherwise last's Parent
            last = last == null ? divider : last.Parent;

            // insert new note

            /*
             * <one:OE>
             * <one:Meta name="omfootnote" content="1" />
             * <one:T>
             *      <![CDATA[<a href=""><span style='font-family:"Calibri Light";font-size:11.0pt' lang=yo>[1]</span></a>
             *      <span style='font-family:"Calibri Light";font-size:11.0pt' lang=yo> Something said here</span>]]>
             * </one:T>
             * </one:OE>
             */

            var link = one.GetHyperlink(page.PageId, textId);

            var color = dark ? ";color:#5B9BD5" : string.Empty;

            var cdatalink = new XElement("a",
                                         new XAttribute("href", link),
                                         new XElement("span",
                                                      new XAttribute("style", $"font-family:'Calibri Light';font-size:11.0pt{color}"),
                                                      $"[{label}]"
                                                      ));

            XElement cdata;

            if (rightToLeft)
            {
                cdata = new XElement("wrapper",
                                     new XElement("span",
                                                  new XAttribute("style", "font-family:'Calibri Light';font-size:11.0pt"),
                                                  "new footnote "),
                                     cdatalink
                                     );
            }
            else
            {
                cdata = new XElement("wrapper", cdatalink,
                                     new XElement("span",
                                                  new XAttribute("style", "font-family:'Calibri Light';font-size:11.0pt"),
                                                  " new footnote")
                                     );
            }

            color = dark ? "#BFBFBF" : "#151515";

            var note = new Paragraph(
                new XAttribute("style", $"color:{color}"),
                new XElement(ns + "Meta",
                             new XAttribute("name", "omfootnote"),
                             new XAttribute("content", label)
                             ),
                new XElement(ns + "T",
                             new XCData(cdata.GetInnerXml())
                             )
                );

            if (rightToLeft)
            {
                note.SetRTL(rightToLeft);
            }

            last.AddAfterSelf(note);

            // update the page so OneNote will generate a new objectID
            await one.Update(page);

            // reload the page and reset state variables...
            page = one.GetPage();
            ns   = page.Namespace;

            EnsureFootnoteFooter();

            return(label);
        }
Example #3
0
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        #region InsertHeadingsTable
        /// <summary>
        /// Inserts a table of contents at the top of the current page,
        /// of all headings on the page
        /// </summary>
        /// <param name="addTopLinks"></param>
        /// <param name="one"></param>
        private void InsertHeadingsTable(OneNote one, bool addTopLinks)
        {
            var page = one.GetPage();
            var ns   = page.Namespace;

            var headings = page.GetHeadings(one);

            var top = page.Root.Element(ns + "Outline")?.Element(ns + "OEChildren");

            if (top == null)
            {
                return;
            }

            var title = page.Root.Elements(ns + "Title").Elements(ns + "OE").FirstOrDefault();

            if (title == null)
            {
                return;
            }

            var titleLink     = one.GetHyperlink(page.PageId, title.Attribute("objectID").Value);
            var titleLinkText = $"<a href=\"{titleLink}\"><span style='font-style:italic'>{Resx.InsertTocCommand_Top}</span></a>";

            var dark      = page.GetPageColor(out _, out _).GetBrightness() < 0.5;
            var textColor = dark ? "#FFFFFF" : "#000000";

            var toc = new List <XElement>
            {
                // "Table of Contents"
                new XElement(ns + "OE",
                             new XAttribute("style", $"font-size:16.0pt;color:{textColor}"),
                             new XElement(ns + "T",
                                          new XCData($"<span style='font-weight:bold'>{Resx.InsertTocCommand_TOC}</span>")
                                          )
                             )
            };

            // use the minimum intent level
            var minlevel = headings.Min(e => e.Style.Index);

            foreach (var heading in headings)
            {
                var text  = new StringBuilder();
                var count = minlevel;
                while (count < heading.Style.Index)
                {
                    text.Append(". . ");
                    count++;
                }

                if (!string.IsNullOrEmpty(heading.Link))
                {
                    var linkColor = dark ? " style='color:#5B9BD5'" : string.Empty;
                    text.Append($"<a href=\"{heading.Link}\"{linkColor}>{heading.Text}</a>");
                }
                else
                {
                    text.Append(heading.Text);
                }

                toc.Add(new XElement(ns + "OE",
                                     new XAttribute("style", $"color:{textColor}"),
                                     new XElement(ns + "T", new XCData(text.ToString()))
                                     ));

                if (addTopLinks)
                {
                    var table = new Table(ns);
                    table.AddColumn(400, true);
                    table.AddColumn(100, true);
                    var row = table.AddRow();
                    row.Cells.ElementAt(0).SetContent(heading.Root);
                    row.Cells.ElementAt(1).SetContent(
                        new XElement(ns + "OE",
                                     new XAttribute("alignment", "right"),
                                     new XElement(ns + "T", new XCData(titleLinkText)))
                        );

                    // heading.Root is the OE
                    heading.Root.ReplaceNodes(table.Root);
                }
            }

            // empty line after the TOC
            toc.Add(new XElement(ns + "OE", new XElement(ns + "T", new XCData(string.Empty))));
            top.AddFirst(toc);

            one.Update(page);
        }
Example #4
0
        private void BuildSectionTable(
            OneNote one, XNamespace ns, XElement container,
            IEnumerable <XElement> elements, bool includePages, int level)
        {
            foreach (var element in elements)
            {
                var notBin = element.Attribute("isRecycleBin") == null;

                if (element.Name.LocalName == "SectionGroup" && notBin)
                {
                    // SectionGroup

                    var name = element.Attribute("name").Value;

                    var indent = new XElement(ns + "OEChildren");

                    indent.Add(new XElement(ns + "OE",
                                            new XElement(ns + "T",
                                                         new XCData($"<span style='font-weight:bold'>{name}</span>"))
                                            ));

                    BuildSectionTable(
                        one, ns, indent, element.Elements(), includePages, level + 1);

                    container.Add(
                        new XElement(ns + "OE", new XElement(ns + "T", new XCData(string.Empty))),
                        new XElement(ns + "OE", indent)
                        );
                }
                else if (element.Name.LocalName == "Section" && notBin)
                {
                    // Section

                    var link  = one.GetHyperlink(element.Attribute("ID").Value, string.Empty);
                    var name  = element.Attribute("name").Value;
                    var pages = element.Elements(ns + "Page");

                    if (includePages && pages.Any())
                    {
                        var indent = new XElement(ns + "OEChildren");

                        foreach (var page in pages)
                        {
                            var text   = new StringBuilder();
                            var plevel = int.Parse(page.Attribute("pageLevel").Value);
                            while (plevel > 0)
                            {
                                text.Append(". . ");
                                plevel--;
                            }

                            var plink = one.GetHyperlink(element.Attribute("ID").Value, string.Empty);

                            var pname = page.Attribute("name").Value;
                            text.Append($"<a href=\"{plink}\">{pname}</a>");

                            indent.Add(new XElement(ns + "OE",
                                                    new XElement(ns + "T", new XCData(text.ToString())
                                                                 )));
                        }

                        container.Add(new XElement(ns + "OE",
                                                   new XElement(ns + "T", new XCData($"<a href=\"{link}\">{name}</a>")),
                                                   indent
                                                   ));
                    }
                    else
                    {
                        container.Add(new XElement(ns + "OE",
                                                   new XElement(ns + "T", new XCData($"<a href=\"{link}\">{name}</a>")
                                                                )));
                    }
                }
            }
        }