private async Task <string> ImportTemplate(Page template) { try { logger.Start(); logger.WriteLine("importing template"); one.CreatePage(one.CurrentSectionId, out var pageId); // remove any objectID values and let OneNote generate new IDs template.Root.Descendants().Attributes("objectID").Remove(); // set the page ID to the new page's ID template.Root.Attribute("ID").Value = pageId; if (string.IsNullOrEmpty(template.Title)) { template.Title = "OneNote Keyboard Shortcuts"; } await one.Update(template); return(pageId); } catch (Exception exc) { logger.WriteLine("error importing template", exc); return(null); } }
private Page GetTargetPage(Heading header) { // first test if heading is a link to a page in this section var cdata = header.Root.GetCData(); if (header.IsHyper) { var matches = Regex.Match(cdata.Value, IDPattern); if (matches.Success) { var sid = matches.Groups["sid"].Value; var pid = matches.Groups["pid"].Value; var key = $"{sid}{pid}"; if (hyperlinks.ContainsKey(key)) { return(one.GetPage(hyperlinks[key], OneNote.PageDetail.Basic)); } } } // create a page in this section to capture heading content one.CreatePage(one.CurrentSectionId, out var pageId); var target = one.GetPage(pageId); target.Title = header.Text; return(target); }
private async Task CopyPages(OneNote one, XElement root, XNamespace ns) { // remove the existing ID so OneNote can create its own for the new folder/section root.Attributes("ID").Remove(); foreach (var element in root.Elements(ns + "Page")) { // get the page to copy var page = one.GetPage(element.Attribute("ID").Value); logger.WriteLine($"copy page {page.Title}"); // create a new page to get a new ID one.CreatePage(root.Attribute("ID").Value, out var newPageId); // set the page ID to the new page's ID page.Root.Attribute("ID").Value = newPageId; // remove all objectID values and let OneNote generate new IDs page.Root.Descendants().Attributes("objectID").Remove(); await one.Update(page); // recurse... foreach (var section in element.Elements(ns + "SectionGroup").Elements(ns + "Section")) { await CopyPages(one, section, ns); } foreach (var section in element.Elements(ns + "Section")) { await CopyPages(one, section, ns); } } }
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // XML... private async Task ImportXml(string filepath) { try { // load page-from-file var template = new Page(XElement.Load(filepath)); using (var one = new OneNote()) { one.CreatePage(one.CurrentSectionId, out var pageId); // remove any objectID values and let OneNote generate new IDs template.Root.Descendants().Attributes("objectID").Remove(); // set the page ID to the new page's ID template.Root.Attribute("ID").Value = pageId; if (string.IsNullOrEmpty(template.Title)) { template.Title = Path.GetFileNameWithoutExtension(filepath); } await one.Update(template); await one.NavigateTo(pageId); } } catch (Exception exc) { logger.WriteLine(exc); UIHelper.ShowMessage("Could not import. See log file for details"); } }
private async Task WordImporter(string filepath, bool append) { using (var word = new Word()) { var html = word.ConvertFileToHtml(filepath); if (append) { using (var one = new OneNote(out var page, out _)) { page.AddHtmlContent(html); await one.Update(page); } } else { using (var one = new OneNote()) { one.CreatePage(one.CurrentSectionId, out var pageId); var page = one.GetPage(pageId); page.Title = Path.GetFileName(filepath); page.AddHtmlContent(html); await one.Update(page); await one.NavigateTo(page.PageId); } } } }
private async Task <Page> CreateTemplatePage() { // use a temporary scratch page to convert the HTML into OneNote XML by pasting the // HTML contents of the clipboard and let OneNote do its magic... var currentPageId = one.CurrentPageId; one.CreatePage(one.CurrentSectionId, out var pageId); await one.NavigateTo(pageId); // 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); new InputSimulator().Keyboard .ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V); // yeah this is dumb but have to wait for paste to complete await Task.Delay(200); var page = one.GetPage(pageId); one.DeleteHierarchy(pageId); await one.NavigateTo(currentPageId); return(page); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #region InsertPagesTables private async Task 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)); await 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); await one.NavigateTo(pageId); }
private async Task CreatePage(OneNote one, XElement page, Page parent) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); // set the page ID to the new page's ID page.Attribute("ID").Value = pageId; // set the page name to user-entered name if (!string.IsNullOrEmpty(plugin.PageName.Trim())) { var name = plugin.PageName; if (name.Contains("$name")) { // grab only text from parent's title name = name.Replace("$name", XElement.Parse($"<w>{parent.Title}</w>").Value); } new Page(page).Title = name; } // remove all objectID values and let OneNote generate new IDs page.Descendants().Attributes("objectID").Remove(); await one.Update(page); if (plugin.AsChildPage) { // get current section again after new page is created section = one.GetSection(); var parentElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == parent.PageId); var childElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); if (childElement != parentElement.NextNode) { // move new page immediately after its original in the section childElement.Remove(); parentElement.AddAfterSelf(childElement); } var level = int.Parse(parentElement.Attribute("pageLevel").Value); childElement.Attribute("pageLevel").Value = (level + 1).ToString(); one.UpdateHierarchy(section); } await one.NavigateTo(pageId); }
public async Task CopyPages(List <string> pageIds) { string lastId = null; using (var progress = new UI.ProgressDialog()) { progress.SetMaximum(pageIds.Count); progress.Show(owner); foreach (var pageId in pageIds) { if (one.GetParent(pageId) == sectionId) { continue; } // get the page to copy var page = one.GetPage(pageId); progress.SetMessage(page.Title); // create a new page to get a new ID one.CreatePage(sectionId, out var newPageId); // set the page ID to the new page's ID page.Root.Attribute("ID").Value = newPageId; // remove all objectID values and let OneNote generate new IDs page.Root.Descendants().Attributes("objectID").Remove(); await one.Update(page); lastId = newPageId; progress.Increment(); } } // navigate after progress dialog is closed otherwise it will hang! if (lastId != null) { await one.NavigateTo(lastId); } }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #region InsertPagesTables private async Task InsertPagesTable(bool withPreviews) { 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; PageNamespace.Set(ns); page.Title = string.Format(Resx.InsertTocCommand_TOCSections, section.Attribute("name").Value); cite = page.GetQuickStyle(StandardStyles.Citation); var container = new XElement(ns + "OEChildren"); var elements = section.Elements(ns + "Page"); var index = 0; BuildSectionToc(container, elements.ToArray(), ref index, 1, withPreviews); var title = page.Root.Elements(ns + "Title").FirstOrDefault(); title.AddAfterSelf(new XElement(ns + "Outline", container)); await 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); await one.NavigateTo(pageId); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - private async Task BuildMapPage(XElement hierarchy) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); var page = one.GetPage(pageId); page.SetMeta(Page.PageMapMetaName, "true"); switch (scope) { case OneNote.Scope.Notebooks: page.Title = "Page Map of All Notebooks"; break; case OneNote.Scope.Sections: page.Title = "Page Map of This Notebook"; break; default: page.Title = "Page Map of This Section"; break; } var container = page.EnsureContentContainer(); container.Add( new XElement(ns + "OE", new XElement(ns + "T", new XCData( "<span style=\"font-weight:bold\">Key</span><br>\n" + "Heading 1 = Notebooks<br>\n" + "Heading 2 = Section<br>\n" + "Heading 3 = Section Groups (italic)") )), new XElement(ns + "OE", new XElement(ns + "T", new XCData(string.Empty))) ); if (hierarchy.Name.LocalName == "Notebooks") { hierarchy.Elements().ToList().ForEach((node) => { container.Add(BuildMapPage(node, page)); }); } else { container.Add(BuildMapPage(hierarchy, page)); } await one.Update(page); await one.NavigateTo(pageId); }
private async Task CopyPages(XElement root, XElement clone, OneNote one, XNamespace ns) { var cloneID = clone.Attribute("ID").Value; foreach (var element in root.Elements(ns + "Page")) { // get the page to copy var page = one.GetPage(element.Attribute("ID").Value); progress.SetMessage(page.Title); // create a new page to get a new ID one.CreatePage(cloneID, out var newPageId); // set the page ID to the new page's ID page.Root.Attribute("ID").Value = newPageId; // remove all objectID values and let OneNote generate new IDs page.Root.Descendants().Attributes("objectID").Remove(); await one.Update(page); progress.Increment(); } // recurse... // NOTE that these find target sections by name, so the names must be unique otherwise // this will copy all pages into the first occurance with a matching name! foreach (var section in root.Elements(ns + "SectionGroup").Elements(ns + "Section")) { var cloneSection = clone.Elements(ns + "SectionGroup").Elements(ns + "Section") .FirstOrDefault(e => e.Attribute("name").Value == section.Attribute("name").Value); await CopyPages(section, cloneSection, one, ns); } foreach (var section in root.Elements(ns + "Section")) { var cloneSection = clone.Elements(ns + "Section") .FirstOrDefault(e => e.Attribute("name").Value == section.Attribute("name").Value); await CopyPages(section, cloneSection, one, ns); } }
private void CreatePage(OneNote one, XElement page, Page parent) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); // set the page ID to the new page's ID page.Attribute("ID").Value = pageId; // set the page name to user-entered name new Page(page).Title = pageName; // remove all objectID values and let OneNote generate new IDs page.Descendants().Attributes("objectID").Remove(); one.Update(page); if (asChildPage) { // get current section again after new page is created section = one.GetSection(); var parentElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == parent.PageId); var childElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); if (childElement != parentElement.NextNode) { // move new page immediately after its original in the section childElement.Remove(); parentElement.AddAfterSelf(childElement); } var level = int.Parse(parentElement.Attribute("pageLevel").Value); childElement.Attribute("pageLevel").Value = (level + 1).ToString(); one.UpdateHierarchy(section); } one.NavigateTo(pageId); }
private async Task <Page> CreatePage(OneNote one, Page parent, string title) { var section = one.GetSection(); var sectionId = section.Attribute("ID").Value; one.CreatePage(sectionId, out var pageId); var page = one.GetPage(pageId); if (parent != null) { // get current section again after new page is created section = one.GetSection(); var parentElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == parent.PageId); var childElement = section.Elements(parent.Namespace + "Page") .FirstOrDefault(e => e.Attribute("ID").Value == pageId); if (childElement != parentElement.NextNode) { // move new page immediately after its original in the section childElement.Remove(); parentElement.AddAfterSelf(childElement); } parentElement.GetAttributeValue("pageLevel", out var level, 1); var pageLevel = (level + 1).ToString(); // must set level on the hierarchy entry and on the page itself childElement.SetAttributeValue("pageLevel", pageLevel); page.Root.SetAttributeValue("pageLevel", pageLevel); one.UpdateHierarchy(section); } await one.NavigateTo(pageId); page.Title = title; return(page); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #region InsertSectionsTable private async Task InsertSectionsTable(OneNote one, bool includePages) { 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; var scope = includePages ? OneNote.Scope.Pages : OneNote.Scope.Sections; var notebook = one.GetNotebook(scope); page.Title = string.Format(Resx.InsertTocCommand_TOCNotebook, notebook.Attribute("name").Value); var container = new XElement(ns + "OEChildren"); BuildSectionTable(one, ns, container, notebook.Elements(), includePages, 1); var title = page.Root.Elements(ns + "Title").FirstOrDefault(); title.AddAfterSelf(new XElement(ns + "Outline", container)); await 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); await one.NavigateTo(pageId); }
private async Task PowerPointImporter(string filepath, bool append, bool split) { string outpath; using (var powerpoint = new PowerPoint()) { outpath = powerpoint.ConvertFileToImages(filepath); } if (outpath == null) { logger.WriteLine($"failed to create output path"); return; } if (split) { using (var one = new OneNote()) { var section = await one.CreateSection(Path.GetFileNameWithoutExtension(filepath)); var sectionId = section.Attribute("ID").Value; var ns = one.GetNamespace(section); await one.NavigateTo(sectionId); int i = 1; foreach (var file in Directory.GetFiles(outpath, "*.jpg")) { one.CreatePage(sectionId, out var pageId); var page = one.GetPage(pageId); page.Title = $"Slide {i}"; var container = page.EnsureContentContainer(); LoadImage(container, ns, file); await one.Update(page); i++; } logger.WriteLine("created section"); } } else { using (var one = new OneNote()) { Page page; if (append) { page = one.GetPage(); } else { one.CreatePage(one.CurrentSectionId, out var pageId); page = one.GetPage(pageId); page.Title = Path.GetFileName(filepath); } var container = page.EnsureContentContainer(); foreach (var file in Directory.GetFiles(outpath, "*.jpg")) { using (var image = Image.FromFile(file)) { LoadImage(container, page.Namespace, file); } } await one.Update(page); if (!append) { await one.NavigateTo(page.PageId); } } } try { Directory.Delete(outpath, true); } catch (Exception exc) { logger.WriteLine($"error cleaning up {outpath}", exc); } }
public override async Task Execute(params object[] args) { using (one = new OneNote()) { (backupPath, defaultPath, _) = one.GetFolders(); if (!Directory.Exists(backupPath)) { UIHelper.ShowError(owner, Resx.AnalyzeCommand_NoBackups); return; } using (var dialog = new AnalyzeDialog()) { if (dialog.ShowDialog(owner) != DialogResult.OK) { return; } showNotebookSummary = dialog.IncludeNotebookSummary; showSectionSummary = dialog.IncludeSectionSummary; pageDetail = dialog.Detail; thumbnailSize = dialog.ThumbnailSize; } one.CreatePage(one.CurrentSectionId, out var pageId); var page = one.GetPage(pageId); page.Title = Resx.AnalyzeCommand_Title; page.SetMeta(MetaNames.AnalysisReport, "true"); ns = page.Namespace; PageNamespace.Set(ns); heading1Index = page.GetQuickStyle(Styles.StandardStyles.Heading1).Index; heading2Index = page.GetQuickStyle(Styles.StandardStyles.Heading2).Index; using (progress = new UI.ProgressDialog()) { progress.SetMaximum(5); progress.Show(owner); var container = page.EnsureContentContainer(); var notebooks = await one.GetNotebooks(); var prev = false; if (showNotebookSummary) { ReportNotebooks(container, notebooks); ReportOrphans(container, notebooks); ReportCache(container); prev = true; } if (showSectionSummary) { if (prev) { WriteHorizontalLine(page, container); } await ReportSections(container, notebooks); prev = true; } if (pageDetail == AnalysisDetail.Current) { if (prev) { WriteHorizontalLine(page, container); } ReportPages(container, one.GetSection(), null, pageId); } else if (pageDetail == AnalysisDetail.All) { if (prev) { WriteHorizontalLine(page, container); } ReportAllPages(container, await one.GetNotebook(), null, pageId); } progress.SetMessage("Updating report..."); await one.Update(page); } await one.NavigateTo(pageId); } }