Example #1
0
 /// <summary>
 ///     Export the capture to the specified page
 /// </summary>
 /// <param name="oneNoteApplication">IOneNoteApplication</param>
 /// <param name="surfaceToUpload">ISurface</param>
 /// <param name="page">OneNotePage</param>
 /// <returns>bool true if everything worked</returns>
 private static bool ExportToPage(IOneNoteApplication oneNoteApplication, ISurface surfaceToUpload, OneNotePage page)
 {
     if (oneNoteApplication == null)
     {
         return(false);
     }
     using (var pngStream = new MemoryStream())
     {
         var pngOutputSettings = new SurfaceOutputSettings(OutputFormats.png, 100, false);
         ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
         var base64String   = Convert.ToBase64String(pngStream.GetBuffer());
         var imageXmlStr    = string.Format(XmlImageContent, base64String, surfaceToUpload.Screenshot.Width, surfaceToUpload.Screenshot.Height);
         var pageChangesXml = string.Format(XmlOutline, imageXmlStr, page.ID, OnenoteNamespace2010, page.Name);
         Log.Info().WriteLine("Sending XML: {0}", pageChangesXml);
         oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
         try
         {
             oneNoteApplication.NavigateTo(page.ID, null, false);
         }
         catch (Exception ex)
         {
             Log.Warn().WriteLine(ex, "Unable to navigate to the target page");
         }
         return(true);
     }
 }
Example #2
0
 public static void ExportToPage(Bitmap imageToExport, OneNotePage page)
 {
     using (MemoryStream pngStream = new MemoryStream()) {
         imageToExport.Save(pngStream, ImageFormat.Png);
         string base64String   = Convert.ToBase64String(pngStream.GetBuffer());
         string imageXmlStr    = string.Format(XML_IMAGE_CONTENT, base64String, imageToExport.Width, imageToExport.Height);
         string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName });
         using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
             LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
             oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
         }
     }
 }
Example #3
0
        /// <summary>
        ///     Retrieve the Section ID for the specified special location
        /// </summary>
        /// <param name="oneNoteApplication"></param>
        /// <param name="specialLocation">SpecialLocation</param>
        /// <returns>string with section ID</returns>
        private static string GetSectionId(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation)
        {
            if (oneNoteApplication == null)
            {
                return(null);
            }
            // ReSharper disable once RedundantAssignment
            var unfiledNotesPath = "";

            oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath);

            // ReSharper disable once RedundantAssignment
            var notebookXml = "";

            oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
            if (string.IsNullOrEmpty(notebookXml))
            {
                return(null);
            }

            Log.Debug().WriteLine(notebookXml);
            StringReader reader = null;

            try
            {
                reader = new StringReader(notebookXml);
                using (var xmlReader = new XmlTextReader(reader))
                {
                    while (xmlReader.Read())
                    {
                        if (!"one:Section".Equals(xmlReader.Name))
                        {
                            continue;
                        }

                        var id   = xmlReader.GetAttribute("ID");
                        var path = xmlReader.GetAttribute("path");
                        if (unfiledNotesPath.Equals(path))
                        {
                            return(id);
                        }
                    }
                }
            }
            finally
            {
                reader?.Dispose();
            }
            return(null);
        }
Example #4
0
        /// <summary>
        /// Get the captions of all the open word documents
        /// </summary>
        /// <returns></returns>
        public static List <OneNotePage> GetPages()
        {
            List <OneNotePage> pages = new List <OneNotePage>();

            try {
                using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
                    if (oneNoteApplication != null)
                    {
                        string notebookXml = "";
                        oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
                        if (!string.IsNullOrEmpty(notebookXml))
                        {
                            LOG.Debug(notebookXml);
                            StringReader reader = null;
                            try {
                                reader = new StringReader(notebookXml);
                                using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
                                    reader = null;
                                    while (xmlReader.Read())
                                    {
                                        if ("one:Page".Equals(xmlReader.Name))
                                        {
                                            if ("true".Equals(xmlReader.GetAttribute("isCurrentlyViewed")))
                                            {
                                                OneNotePage page = new OneNotePage();
                                                page.PageName = xmlReader.GetAttribute("name");
                                                page.PageID   = xmlReader.GetAttribute("ID");
                                                pages.Add(page);
                                                // For debugging
                                                //string pageXml = "";
                                                //oneNoteApplication.GetPageContent(page.PageID, out pageXml, PageInfo.piAll, XMLSchema.xs2010);
                                                //LOG.DebugFormat("Page XML: {0}", pageXml);
                                            }
                                        }
                                    }
                                }
                            } finally {
                                if (reader != null)
                                {
                                    reader.Dispose();
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.Warn("Problem retrieving onenote destinations, ignoring: ", ex);
            }
            return(pages);
        }
Example #5
0
 public static void ExportToPage(ISurface surfaceToUpload, OneNotePage page)
 {
     using (MemoryStream pngStream = new MemoryStream()) {
         SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
         ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
         string base64String   = Convert.ToBase64String(pngStream.GetBuffer());
         string imageXmlStr    = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
         string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName });
         using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
             LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
             oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
         }
     }
 }
Example #6
0
		/// <summary>
		/// Can be used to change the title of a page
		/// </summary>
		/// <param name="oneNoteApplication"></param>
		/// <param name="pageId"></param>
		/// <param name="title"></param>
		private static void UpdatePageTitle(IOneNoteApplication oneNoteApplication, string pageId, string title) {
			try {
				string pageXML = "";
				oneNoteApplication.GetPageContent(pageId, out pageXML, PageInfo.piAll, XMLSchema.xsCurrent);
				XmlDocument doc = new XmlDocument();
				doc.LoadXml(pageXML);
				XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
				namespaceManager.AddNamespace("one", ONENOTE_NAMESPACE_2010);

				doc.SelectSingleNode("//one:T", namespaceManager).InnerText = title;
				// Update the page
				oneNoteApplication.UpdatePageContent(doc.OuterXml, DateTime.MinValue, XMLSchema.xs2010, true);

			} catch(Exception ex) {
				LOG.Warn("Couldn't set page title.", ex);
			}
		}
        /// <summary>
        /// Can be used to change the title of a page
        /// </summary>
        /// <param name="oneNoteApplication"></param>
        /// <param name="pageId"></param>
        /// <param name="title"></param>
        private static void UpdatePageTitle(IOneNoteApplication oneNoteApplication, string pageId, string title)
        {
            try {
                string pageXML = "";
                oneNoteApplication.GetPageContent(pageId, out pageXML, PageInfo.piAll, XMLSchema.xsCurrent);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(pageXML);
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
                namespaceManager.AddNamespace("one", ONENOTE_NAMESPACE_2010);

                doc.SelectSingleNode("//one:T", namespaceManager).InnerText = title;
                // Update the page
                oneNoteApplication.UpdatePageContent(doc.OuterXml, DateTime.MinValue, XMLSchema.xs2010, true);
            } catch (Exception ex) {
                LOG.Warn("Couldn't set page title.", ex);
            }
        }
 /// <summary>
 /// Create a new page in the "unfiled notes section", with the title of the capture, and export the capture there.
 /// </summary>
 /// <param name="surfaceToUpload">ISurface</param>
 /// <returns>bool true if export worked</returns>
 public static bool ExportToNewPage(ISurface surfaceToUpload)
 {
     using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
         OneNotePage newPage = new OneNotePage();
         string      unfiledNotesSectionID = GetSectionID(oneNoteApplication, SpecialLocation.slUnfiledNotesSection);
         if (unfiledNotesSectionID != null)
         {
             string pageId = "";
             oneNoteApplication.CreateNewPage(unfiledNotesSectionID, out pageId, NewPageStyle.npsDefault);
             newPage.ID = pageId;
             // Set the new name, this is automatically done in the export to page
             newPage.Name = surfaceToUpload.CaptureDetails.Title;
             return(ExportToPage(oneNoteApplication, surfaceToUpload, newPage));
         }
     }
     return(false);
 }
        /// <summary>
        /// Retrieve the Section ID for the specified special location
        /// </summary>
        /// <param name="oneNoteApplication"></param>
        /// <param name="specialLocation">SpecialLocation</param>
        /// <returns>string with section ID</returns>
        private static string GetSectionID(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation)
        {
            if (oneNoteApplication == null)
            {
                return(null);
            }
            string unfiledNotesPath = "";

            oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath);

            string notebookXml = "";

            oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
            if (!string.IsNullOrEmpty(notebookXml))
            {
                LOG.Debug(notebookXml);
                StringReader reader = null;
                try {
                    reader = new StringReader(notebookXml);
                    using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
                        while (xmlReader.Read())
                        {
                            if ("one:Section".Equals(xmlReader.Name))
                            {
                                string id   = xmlReader.GetAttribute("ID");
                                string path = xmlReader.GetAttribute("path");
                                if (unfiledNotesPath.Equals(path))
                                {
                                    return(id);
                                }
                            }
                        }
                    }
                } finally {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }
            return(null);
        }
 /// <summary>
 /// Export the capture to the specified page
 /// </summary>
 /// <param name="oneNoteApplication">IOneNoteApplication</param>
 /// <param name="surfaceToUpload">ISurface</param>
 /// <param name="page">OneNotePage</param>
 /// <returns>bool true if everything worked</returns>
 private static bool ExportToPage(IOneNoteApplication oneNoteApplication, ISurface surfaceToUpload, OneNotePage page)
 {
     if (oneNoteApplication == null)
     {
         return(false);
     }
     using (MemoryStream pngStream = new MemoryStream()) {
         SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
         ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
         string base64String   = Convert.ToBase64String(pngStream.GetBuffer());
         string imageXmlStr    = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
         string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.ID, ONENOTE_NAMESPACE_2010, page.Name });
         LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
         oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
         try {
             oneNoteApplication.NavigateTo(page.ID, null, false);
         } catch (Exception ex) {
             LOG.Warn("Unable to navigate to the target page", ex);
         }
         return(true);
     }
 }
Example #11
0
		/// <summary>
		/// Retrieve the Section ID for the specified special location
		/// </summary>
		/// <param name="oneNoteApplication"></param>
		/// <param name="specialLocation">SpecialLocation</param>
		/// <returns>string with section ID</returns>
		private static string GetSectionID(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation) {
			if(oneNoteApplication == null) {
				return null;
			}
			string unfiledNotesPath = "";
			oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath);

			string notebookXml = "";
			oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
			if(!string.IsNullOrEmpty(notebookXml)) {
				LOG.Debug(notebookXml);
				StringReader reader = null;
				try {
					reader = new StringReader(notebookXml);
					using(XmlTextReader xmlReader = new XmlTextReader(reader)) {
						while(xmlReader.Read()) {
							if("one:Section".Equals(xmlReader.Name)) {
								string id = xmlReader.GetAttribute("ID");
								string path = xmlReader.GetAttribute("path");
								if(unfiledNotesPath.Equals(path)) {
									return id;
								}
							}
						}
					}
				} finally {
					if(reader != null) {
						reader.Dispose();
					}
				}
			}
			return null;
		}
Example #12
0
		/// <summary>
		/// Export the capture to the specified page
		/// </summary>
		/// <param name="oneNoteApplication">IOneNoteApplication</param>
		/// <param name="surfaceToUpload">ISurface</param>
		/// <param name="page">OneNotePage</param>
		/// <returns>bool true if everything worked</returns>
		private static bool ExportToPage(IOneNoteApplication oneNoteApplication, ISurface surfaceToUpload, OneNotePage page) {
			if(oneNoteApplication == null) {
				return false;
			}
			using (MemoryStream pngStream = new MemoryStream()) {
				SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
				ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
				string base64String = Convert.ToBase64String(pngStream.GetBuffer());
				string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
				string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.ID, ONENOTE_NAMESPACE_2010, page.Name });
				LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
				oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
				try {
					oneNoteApplication.NavigateTo(page.ID, null, false);
				} catch(Exception ex) {
					LOG.Warn("Unable to navigate to the target page", ex);
				}
				return true;
			}
		}
Example #13
0
 /// <summary>
 /// Export the capture to the specified page
 /// </summary>
 /// <param name="surfaceToUpload">ISurface</param>
 /// <param name="page">OneNotePage</param>
 /// <returns>bool true if everything worked</returns>
 public static bool ExportToPage(ISurface surfaceToUpload, OneNotePage page)
 {
     using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
         return(ExportToPage(oneNoteApplication, surfaceToUpload, page));
     }
 }
Example #14
0
        /// <summary>
        /// Get the captions of all the open word documents
        /// </summary>
        /// <returns></returns>
        public static List <OneNotePage> GetPages()
        {
            List <OneNotePage> pages = new List <OneNotePage>();

            try {
                using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance <IOneNoteApplication>()) {
                    if (oneNoteApplication != null)
                    {
                        // ReSharper disable once RedundantAssignment
                        string notebookXml = "";
                        oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
                        if (!string.IsNullOrEmpty(notebookXml))
                        {
                            Log.Debug(notebookXml);
                            StringReader reader = null;
                            try {
                                reader = new StringReader(notebookXml);
                                using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
                                    reader = null;
                                    OneNoteSection  currentSection  = null;
                                    OneNoteNotebook currentNotebook = null;
                                    while (xmlReader.Read())
                                    {
                                        if ("one:Notebook".Equals(xmlReader.Name))
                                        {
                                            string id = xmlReader.GetAttribute("ID");
                                            if (id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID)))
                                            {
                                                currentNotebook = new OneNoteNotebook
                                                {
                                                    ID   = xmlReader.GetAttribute("ID"),
                                                    Name = xmlReader.GetAttribute("name")
                                                };
                                            }
                                        }
                                        if ("one:Section".Equals(xmlReader.Name))
                                        {
                                            string id = xmlReader.GetAttribute("ID");
                                            if (id != null && (currentSection == null || !id.Equals(currentSection.ID)))
                                            {
                                                currentSection = new OneNoteSection
                                                {
                                                    ID     = xmlReader.GetAttribute("ID"),
                                                    Name   = xmlReader.GetAttribute("name"),
                                                    Parent = currentNotebook
                                                };
                                            }
                                        }
                                        if ("one:Page".Equals(xmlReader.Name))
                                        {
                                            // Skip deleted items
                                            if ("true".Equals(xmlReader.GetAttribute("isInRecycleBin")))
                                            {
                                                continue;
                                            }
                                            OneNotePage page = new OneNotePage
                                            {
                                                Parent = currentSection,
                                                Name   = xmlReader.GetAttribute("name"),
                                                ID     = xmlReader.GetAttribute("ID")
                                            };
                                            if (page.ID == null || page.Name == null)
                                            {
                                                continue;
                                            }
                                            page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"));
                                            pages.Add(page);
                                        }
                                    }
                                }
                            } finally
                            {
                                reader?.Dispose();
                            }
                        }
                    }
                }
            } catch (COMException cEx) {
                if (cEx.ErrorCode == unchecked ((int)0x8002801D))
                {
                    Log.Warn("Wrong registry keys, to solve this remove the OneNote key as described here: http://microsoftmercenary.com/wp/outlook-excel-interop-calls-breaking-solved/");
                }
                Log.Warn("Problem retrieving onenote destinations, ignoring: ", cEx);
            } catch (Exception ex) {
                Log.Warn("Problem retrieving onenote destinations, ignoring: ", ex);
            }
            pages.Sort(delegate(OneNotePage p1, OneNotePage p2) {
                if (p1.IsCurrentlyViewed || p2.IsCurrentlyViewed)
                {
                    return(p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed));
                }
                return(string.Compare(p1.DisplayName, p2.DisplayName, StringComparison.Ordinal));
            });
            return(pages);
        }