none
Problem programmatically inserting image in onenote 2010 using C#

    Question

  • Hi all, I really know nothing about C# and office automation so please don't mind my stupid questions.

    I am trying to insert an image in onenote programatically but having some problems

    first, from http://social.msdn.microsoft.com/Forums/en-US/officegeneral/thread/b972aaf8-6870-4007-ab3d-41c4bd773559, i got this method to format image data into xml node

    //Get the bytes of the image
    byte[] fileBytes = File.ReadAllBytes(@"image directory");
    
    //Convert them to a base64 string
    string fileString = Convert.ToBase64String(fileBytes);
    
    //XML string to add
    string toAdd = @"<one:Image>
                        <one:Data>" + fileString + @"</one:Data>
                    </one:Image>";

    then i read onenote page xml using GetPageContent method of OneNote COM API

     

    oneNoteApp.GetPageContent(pageId, out noteBookXml, PageInfo.piBinaryData);
                xmlDoc.LoadXml(noteBookXml);
    
    and tried to insert the image node using this method

     

    XmlDocumentFragment xdf = xmlDoc.CreateDocumentFragment();
                xdf.InnerXml = toAdd;
                xmlDoc.DocumentElement.AppendChild(xdf);
    

     


    but i get an Xml exception "'one' is an undeclared prefix."

     

    Second problem is related to UpdatePageContent() function ... even if i try to call this function with xmlDoc.ToString() without any changes( i mean without changing the xml), i get COMException "Exception from HRESULT: 0x8004200B"

    Please help!!!

     


    • Edited by AtlasV Sunday, January 22, 2012 5:35 PM
    Saturday, January 21, 2012 9:15 PM

Answers

  • Hi AtlasV,

    Thank you for posting.

    I wrote the following code which can achieve your goal:

    using System;
    using System.Linq;
    using OneNote = Microsoft.Office.Interop.OneNote;
    using Microsoft.Office.Interop.OneNote;
    using System.Xml.Linq;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
     
    namespace ConsoleApplicationOneNoteTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strNamespace = "http://schemas.microsoft.com/office/onenote/2010/onenote";
                string m_xmlImageContent =
    "<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
                string m_xmlNewOutline =
                   "<?xml version=\"1.0\"?><one:Page xmlns:one=\"{2}\" ID=\"{1}\"><one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>{0}</one:Page>";
                string pageToBeChange = "MyPage";
     
                Bitmap bitmap = new Bitmap(@"C:\Users\v-bpeng\Desktop\1.jpg");
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Jpeg);
                string fileString = Convert.ToBase64String(stream.ToArray());
     
                ////Get the bytes of the image
                //byte[] fileBytes = File.ReadAllBytes(@"C:\Users\v-bpeng\Desktop\1.jpg");
     
                ////Convert them to a base64 string
                //string fileString = Convert.ToBase64String(fileBytes);
     
                string notebookXml;
                OneNote.Application onenoteApp = new OneNote.Application();
                onenoteApp.GetHierarchy(nullHierarchyScope.hsPages, out notebookXml);
                var doc = XDocument.Parse(notebookXml);
                var ns = doc.Root.Name.Namespace;
                var pageNode = doc.Descendants(ns + "Page").Where(n => n.Attribute("name").Value == pageToBeChange).FirstOrDefault();
                var existingPageId = pageNode.Attribute("ID").Value;
     
                if (pageNode != null)
                {
                    //var page = new XDocument(new XElement(ns + "Page",
                    //                           new XElement(ns + "Outline",
                    //                             new XElement(ns + "OEChildren",
                    //                               new XElement(ns + "OE",
                    //                                 new XElement(ns + "T",
                    //                                   new XCData("Current date: " +
                    //                                     DateTime.Now.
                    //                                       ToLongDateString())))))));
     
                    //page.Root.SetAttributeValue("ID", existingPageId);
                    //onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
                    string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
                    string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageToBeChange});
                    onenoteApp.UpdatePageContent(pageChangesXml.ToString(), DateTime.MinValue);
                }
            }
        }
    }
    

    Note: There must be a page named MyPage in the OneNote application.

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by AtlasV Friday, January 27, 2012 8:19 PM
    Thursday, January 26, 2012 4:52 AM

All replies

  • Hi AtlasV,

    Thank you for posting.

    I wrote the following code which can achieve your goal:

    using System;
    using System.Linq;
    using OneNote = Microsoft.Office.Interop.OneNote;
    using Microsoft.Office.Interop.OneNote;
    using System.Xml.Linq;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
     
    namespace ConsoleApplicationOneNoteTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strNamespace = "http://schemas.microsoft.com/office/onenote/2010/onenote";
                string m_xmlImageContent =
    "<one:Image><one:Size width=\"{1}\" height=\"{2}\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
                string m_xmlNewOutline =
                   "<?xml version=\"1.0\"?><one:Page xmlns:one=\"{2}\" ID=\"{1}\"><one:Title><one:OE><one:T><![CDATA[{3}]]></one:T></one:OE></one:Title>{0}</one:Page>";
                string pageToBeChange = "MyPage";
     
                Bitmap bitmap = new Bitmap(@"C:\Users\v-bpeng\Desktop\1.jpg");
                MemoryStream stream = new MemoryStream();
                bitmap.Save(stream, ImageFormat.Jpeg);
                string fileString = Convert.ToBase64String(stream.ToArray());
     
                ////Get the bytes of the image
                //byte[] fileBytes = File.ReadAllBytes(@"C:\Users\v-bpeng\Desktop\1.jpg");
     
                ////Convert them to a base64 string
                //string fileString = Convert.ToBase64String(fileBytes);
     
                string notebookXml;
                OneNote.Application onenoteApp = new OneNote.Application();
                onenoteApp.GetHierarchy(nullHierarchyScope.hsPages, out notebookXml);
                var doc = XDocument.Parse(notebookXml);
                var ns = doc.Root.Name.Namespace;
                var pageNode = doc.Descendants(ns + "Page").Where(n => n.Attribute("name").Value == pageToBeChange).FirstOrDefault();
                var existingPageId = pageNode.Attribute("ID").Value;
     
                if (pageNode != null)
                {
                    //var page = new XDocument(new XElement(ns + "Page",
                    //                           new XElement(ns + "Outline",
                    //                             new XElement(ns + "OEChildren",
                    //                               new XElement(ns + "OE",
                    //                                 new XElement(ns + "T",
                    //                                   new XCData("Current date: " +
                    //                                     DateTime.Now.
                    //                                       ToLongDateString())))))));
     
                    //page.Root.SetAttributeValue("ID", existingPageId);
                    //onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
                    string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
                    string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageToBeChange});
                    onenoteApp.UpdatePageContent(pageChangesXml.ToString(), DateTime.MinValue);
                }
            }
        }
    }
    

    Note: There must be a page named MyPage in the OneNote application.

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by AtlasV Friday, January 27, 2012 8:19 PM
    Thursday, January 26, 2012 4:52 AM
  • Hi Bruce,

    Thank you very much for your time and effort. I really appreciate that.

    I tried your code and it beautifully solved my first problem. For the second one, I had to Add reference to office 14 object library (instead of office 12 type library .. which I was doing earlier) and set it's "Embed Interop Types" property to false.

     

    Thanks again man!

    Best Regards,

    Atlas


    • Edited by AtlasV Friday, January 27, 2012 8:20 PM
    Friday, January 27, 2012 8:19 PM
  • I wrote a powertoy to import images that may be interesting to you.  the code is here: http://blogs.msdn.com/b/johnguin/archive/2010/12/13/source-code-for-the-bulk-ink-eraser-powertoy.aspx and has links to the EXE.

     


    John Guin [msft]
    OneNote Test Team
    http://blogs.msdn.com/johnguin
    • Edited by John Guin Saturday, January 28, 2012 7:28 AM
    Saturday, January 28, 2012 7:28 AM
  • Hi,

     I am inserting an image within OneNote using the <one:Image> object. Additionally, I want to set an Alt text for the image.Manually, we can do this by right clicking on the image and selecting "Edit Alt Text" option. How can we do this programatically? Is there a property for the <one:Image> object to which we can set the desired Alt text value?

    Thank you in advance for any help/suggestions.

    Thanks,

    Pallavi

    Tuesday, August 28, 2012 9:46 AM
  • Hi I am looking for the code that can create a jpg file from the image stored in one note. Pretty much reverse of what the above code does. Can someone help?
    Monday, December 10, 2012 5:36 PM
  • Hi Bruce,

    Thanks for this example; its exactly what I was looking for to get started. There is a problem with this version when used with OneNote 2013. UpdatePageContent throws an exception for mismatched update times (0x80042010, http://msdn.microsoft.com/en-us/library/office/ff966472(v=office.14).aspx) when called with DateTime.Min. It appears to be better to call UpdatePageContent omitting the second argument.

    if (pageNode != null)
    {
        string imageXmlStr = string.Format(m_xmlImageContent, fileString, bitmap.Width, bitmap.Height);
        string pageChangesXml = string.Format(m_xmlNewOutline, new object[] { imageXmlStr, existingPageId, strNamespace, pageNode.Attribute("name") });
        app.UpdatePageContent(pageChangesXml.ToString());
    }
    

    Cheers,

    -Nels

    Saturday, March 29, 2014 7:02 PM