kkamegawa's weblog

Visual Studio,TFS,ALM,VSTS,DevOps関係のことについていろいろと書いていきます。Google Analyticsで解析を行っています

OneNoteの落書き帳にあるpngをフォルダに保存する

大分間が空いたけど、こんな感じになります。

using System;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using Microsoft.Office.Interop.OneNote;

namespace OneNoteTest1
{
    class Program
    {
        static Microsoft.Office.Interop.OneNote.Application onApplication;

        static string GetSaveFolder(string[] args)
        {
            string saveFolder = default(string);
            if(args.Length != 1){
                Console.WriteLine("OneNoteSavePng Version test");
                Console.WriteLine("OneNoteSavePng [FolderPath]");
            } else{
                 if(Directory.Exists(args[0]) == true){
                     saveFolder = args[0];
                 }
            }
            return saveFolder;
        }

        static void GetUnfiledNotes(string savedFolder)
        {
            String strXML = default(string);
            onApplication.GetHierarchy(null, HierarchyScope.hsPages, out strXML);
            var oneNoteXML = XDocument.Parse(strXML);
            XNamespace oneNoeteIns = "http://schemas.microsoft.com/office/onenote/2007/onenote";

            var noteBooks = from p in oneNoteXML.Root.Elements(oneNoeteIns + "UnfiledNotes").
                                Elements(oneNoeteIns + "Section").Elements()
                            select p;

            string pageID = default(string);
            string pageXmlOut = default(string);
            int fileCount = 0;
            FileStream outputFile;
            string outputFileName = default(string);

            foreach (var book in noteBooks)
            {
                pageID = (string)book.Attribute("ID");
                onApplication.GetPageContent(pageID, out pageXmlOut, PageInfo.piAll);
                var UnfiledNote = XDocument.Parse(pageXmlOut);
                var pngData =
                    from x in
                        UnfiledNote.Root.Elements(oneNoeteIns + "Outline").Elements(
                        oneNoeteIns + "OEChildren").Elements(
                        oneNoeteIns + "OE").Elements(oneNoeteIns + "Image")
                    where (string)x.Attribute("format") == "png"
                    select x.Element(oneNoeteIns + "Data");

                foreach (var data in pngData)
                {
                    byte[] pngBytes = Convert.FromBase64String((string) data);
                    outputFileName = string.Format(@"{0}\{1:D5}.png", savedFolder, fileCount++);
                    
                    outputFile = new System.IO.FileStream(outputFileName,	
                        System.IO.FileMode.Create,System.IO.FileAccess.Write);
                    outputFile.Write(pngBytes, 0, pngBytes.Length);
                    outputFile.Close();
                }
            }
        }

        static void Main(string[] args)
        {

            onApplication = new Microsoft.Office.Interop.OneNote.ApplicationClass();
            string saveFolder = GetSaveFolder(args);
            if(saveFolder == default(string)){
                return;
            }
            GetUnfiledNotes(saveFolder);
        }
    }
}

コンパイルにはOneNote 2007とVisual Studio 2008が必要です。Express Editionではできない…はず。まだ落書き帳にあるすべてのpngファイルを保存することができないので、たぶん何か違うところがあるのでしょう。もうちょっと調べてみます。