I am trying to write HTML contents to OneNote. What I'm trying to do is get the contents of a word file that is written in html and write that html to OneNote to successfully convert all elements of that page to OneNote. Here is what I have for converting the word document to HTML:
public static void convertWordToHtml()
{
byte[] byteArray = File.ReadAllBytes("GreetingFile2.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
File.WriteAllText("Something.html", html.ToStringNewLineOnAttributes());
}
}
}
And this is the code I have for writing content to OneNote programmatically.
static Application onenoteApp = new Application();
static XNamespace ns = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
GetNamespace();
string notebookId = GetObjectId(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks, "Aaron @ Microsoft");
string sectionId = GetObjectId(notebookId, Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections, "New Section 1");
string firstPageId = GetObjectId(sectionId, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, "My Page");
GetPageContent(firstPageId);
Console.Read();
}
static void GetNamespace()
{
string xml;
onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks, out xml);
var doc = XDocument.Parse(xml);
ns = doc.Root.Name.Namespace;
}
static string GetObjectId(string parentId, Microsoft.Office.Interop.OneNote.HierarchyScope scope, string objectName)
{
string xml;
onenoteApp.GetHierarchy(parentId, scope, out xml);
var doc = XDocument.Parse(xml);
var nodeName = "";
switch(scope)
{
case (Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
case (Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages): nodeName = "Page"; break;
case (Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections): nodeName = "Section"; break;
default:
return null;
}
var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;
}
static string GetPageContent(string pageId)
{
string xml;
onenoteApp.GetPageContent(pageId, out xml, Microsoft.Office.Interop.OneNote.PageInfo.piAll);
var doc = XDocument.Parse(xml);
var outline = doc.Descendants(ns + "Outline").First();
var content = outline.Descendants(ns + "T").First();
string contentVal = content.Value;
content.Value = "Modified something cool";
onenoteApp.UpdatePageContent(doc.ToString());
return null;
}
Is there a way I can write the HTML I've gotten to OneNote? I have tried the OneNote API but that hasn't been working to well for me because setting it up has been a bit difficult to understand.