Search code examples
c#xmloffice-interopadd-inonenote

C# OneNote Copying Image to another page with UpdatePageContent changes XML


I'm trying to copy a OneNote page to another Notebook with a new title. That works so far with GetPageContent and UpdatePageContent. It copys all items except Images. The Image Object is created, but it only shows a red 'x'. I tried to fix that problem on my own, wrote the XML of the old page and the one of the new page in files. The only difference between the Image Objects was that the 'format' attribute was missing in the new page. Then I wrote the content of my XML string in a file before UpdatePageContent, loaded the content from my new Page and wrote it in another file. So I had my string which I used to UpdatePageContent and the XML String wich actually was shown in OneNote. Before UpdatePageContent the 'format' attribute existed and after it was missing. I even tried manually adding the attribute in the file and then loading it and updating it, but that didn't change something, it was also removed after UpdatePageContent.

Is that missing attribute causing the red 'x' or is there something else to check? And if it is the problem, how can I avoid that the 'format' attribute is removed?

Every help is appreciated.

Here is my code to copy the page and update the page ('ID' is the ID of the page I want to copy):

      onApp.CreateNewPage(ID, out A, NewPageStyle.npsBlankPageWithTitle);
        var XMLPageContent = XDocument.Parse(PageContent);
        onApp.GetPageContent(A, out xml);
        var NewXD = XDocument.Parse(xml);
        NewXD.Element(NS + "Page").Attribute("pageLevel").Value = XMLPageContent.Element(NS+ "Page").Attribute("pageLevel").Value;
        NewXD.Elements(NS + "Page").Elements().Remove();
        getChild(XMLPageContent.Element(NS + "Page"), NewXD.Element(NS + "Page"));
        var Title = NewXD.Descendants(NS + "T").First();
        Title.Value = NewPage;
        onApp.UpdatePageContent(NewXD.ToString());
        onApp.GetPageContent(A, out xml);
        doc = XDocument.Parse(xml);
        StreamWriter SW = new StreamWriter("C:\\xml.txt");
        SW.Write(doc.ToString());
        StreamWriter SW2 = new StreamWriter("C:\\xml2.txt");
        SW2.Write(XMLPageContent.ToString());
        SW.Close();
        SW2.Close();

    }



    private void getChild(XElement PXE, XElement NXE)
    {

            if (PXE.Elements() != null)
            {
                foreach (XObject XE in PXE.Nodes())
                {
                   try
                   {
                    XElement XEE = (XElement) XE;
                        XElement toAdd = new XElement(XEE.Name.ToString());
                        foreach (XAttribute XA in XEE.Attributes())
                        {
                            if (!XA.Name.LocalName.Contains("objectID"))
                            {
                                try
                                {
                                    toAdd.Add(XA);
                                }
                                catch (Exception e)
                                {
                                    MessageBox.Show(XA.Name.ToString());
                                    throw e;
                                }
                            }
                        }

                        if (toAdd.Attributes("callbackID").ToArray().Length > 0)
                        {
                            string[] hilf = toAdd.Attribute("callbackID").Value.Split('{', '}');
                            string xml;
                            onApp.GetPageContent(A, out xml);
                            var doc = XDocument.Parse(xml);
                            hilf[1] = doc.Element(NS+"Page").Attribute("ID").Value.Split('{', '}')[1];
                            toAdd.Attribute("callbackID").Value = '{' + hilf[1] + '}' + '{' + hilf[3] + '}' + '{' + hilf[5] + '}';

                        }
                        getChild(XEE, toAdd);
                        NXE.Add(toAdd);

                }
                 catch(InvalidCastException)
                {
                    NXE.Add(XE);
                }
               }
            }


            else return;
        }

Solution

  • I solved the problem on my own. For objects with an CallbackID it is necessary to use getBinaryPageContent with this CallbackID and copy the binary data with "one: data" into the object instead of copying the "OCRData".