OneNote, inserting elements with UpdatePageContent

2019-09-08 17:55发布

问题:

I tried to find if it was possible to add content to a oneNote page via UpdatePageContent in the Microsoft.Office.Interop.OneNote reference.

I want to create a page with a default template that I made in XML, but the documation of msdn said me that this function only allow the structure :

msdn doc :

The only objects that you must include in the XML code that you pass to the UpdatePageContent method are page-level objects (such as outlines, images on the page, or ink on the page) that have changed. This method does not modify or remove page-level objects that you do not specify in the bstrPageChangesXmlIn parameter. The method entirely replaces page-level objects, such as outlines, whose IDs match those of the objects you pass. Consequently, you must fully specify all page-level objects in your code, including their existing content and changes you want to make to them.

my question is : can I, with this library, add element in a page? if so, how?

thanks

回答1:

You can use OMSpy tool to investigate Page content structure. Following are some samples to help you get started:

To Set page title when you have pageId

        private static void SePageTitle(string pageId, string pageTitle)
    {
        Microsoft.Office.Interop.OneNote.Application m_app = new Microsoft.Office.Interop.OneNote.Application();
        string strPagTitle = "<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2010/onenote\" ID=\"{0}\" >" +
              "<one:Title selected=\"partial\" lang=\"en-US\">" +
                "<one:OE style=\"font-family:Calibri;font-size:17.0pt\">" +
                  "<one:T><![CDATA[{1}]]></one:T> " +
                "</one:OE>" +
              "</one:Title>" +
            "</one:Page>";
        strPagTitle = string.Format(strPagTitle, pageId, pageTitle);
        m_app.UpdatePageContent(strPagTitle);
    }

Add element to page when you have pageId:

        private static void SetElementInPage(string pageId)
    {
        Microsoft.Office.Interop.OneNote.Application m_app = new Microsoft.Office.Interop.OneNote.Application();
        string strPageContent = "<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2010/onenote\" ID=\"{0}\" >" +
            "<one:Outline>" +
                "<one:Position x=\"36.0\" y=\"86.4000015258789\" z=\"0\" />" + 
                "<one:Size width=\"117.001953125\" height=\"40.28314971923828\" />" + 
                "<one:OEChildren>" + 
                    "<one:OE>" +
                        "<one:T><![CDATA[This is a sample data added to test out OneNote API functionality.  Following is a list item.]]></one:T>" + 
                    "</one:OE>" +
                "</one:OEChildren>" +
                "<one:OEChildren indent=\"2\">" +
                    "<one:OE  alignment=\"left\">" +
                        "<one:List>" +
                            "<one:Bullet bullet=\"2\" fontSize=\"11.0\" />" +
                        "</one:List>" + 
                        "<one:T><![CDATA[A for Apple]]></one:T>" +
                    "</one:OE>" +
                    "<one:OE  alignment=\"left\">" +
                        "<one:List>" +
                            "<one:Bullet bullet=\"2\" fontSize=\"11.0\" />" +
                        "</one:List>" + 
                        "<one:T><![CDATA[B for Ball]]></one:T>" +
                    "</one:OE>" +
                    "<one:OE  alignment=\"left\">" +
                        "<one:List>" +
                            "<one:Bullet bullet=\"2\" fontSize=\"11.0\" />" +
                        "</one:List>" + 
                        "<one:T><![CDATA[C for Cat]]></one:T>" +
                    "</one:OE>" +
               "</one:OEChildren>" +
            "</one:Outline>" +
            "</one:Page>";
        strPageContent = string.Format(strPageContent, pageId);
        m_app.UpdatePageContent(strPageContent);
    }


回答2:

Pl post post part of the for the next when you are asking, it will be easy for other SOF Members to understand the problem properly.

for the syntax and addressing your issue to use Onepagenote, go through the below links.

http://msdn.microsoft.com/en-us/magazine/ff796230.aspx

http://social.msdn.microsoft.com/Forums/pl-PL/officegeneral/thread/4596510a-6509-4e3a-be08-c11131fa4663



回答3:

Why make it yourself so hard ? You can simply create your template in OneNote and than use the interop to get the Hierarchy. So afterwards you can take a sectiongroup (which will be your template) so you can get the content of the page(s) inside every section of a sectiongroup.

I struggled about copying elements from one notebook to the other one but every element has an ObjectID. This indicates that the element is known by that specific page. Remove this attribute (so not only the value of the attribute) and OneNote will see the element as a new one and will assign an ObjectID to the element.

A code sample how I removed ID's and objectID's from a page:

string xml;
                    OneApplication.GetPageContent(page.Attributes["ID"].Value, out xml, PageInfo.piAll);
                    var doc = XDocument.Parse(xml);
                    var nodes = doc.Descendants();

                    foreach (var node in nodes)
                    {
                        if (node.Attribute("objectID") != null)
                        {
                            node.Attributes("objectID").Remove();
                        } else if (node.Attribute("ID") != null)
                        {
                            node.Attribute("ID").Value = "";
                        }
                    }

When you than add all the elements to another page. You will see them. This works 100% because I've done it myself :)

Hopefully this was helpful.