Browse Source

基本功能实现。

liuyuqi-dellpc 3 years ago
parent
commit
ce564f1d78

+ 35 - 0
OneNote2PDF/App.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace OneNote2PDF
+{
+    /// <summary>
+    /// 全局 app 对象
+    /// </summary>
+    class App
+    {
+        public string inputDir = "";
+        public string outputDir = "";
+
+        private static App app;
+        private static readonly object locker = new object(); // 对象锁标志
+
+        public static App getInstance()
+        {
+            if (app == null)
+            {
+                lock (locker) //标志对象加锁,多线程内部代码会挂起
+                {
+                    if (app == null)
+                    {
+                        app = new App();
+                    }
+                }
+            }
+            return app;
+        }
+    }
+}

+ 76 - 58
OneNote2PDF/MainForm.Designer.cs

@@ -30,16 +30,17 @@
         {
             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
             this.panel1 = new System.Windows.Forms.Panel();
+            this.btnHome = new System.Windows.Forms.Button();
+            this.btnAbout = new System.Windows.Forms.Button();
             this.btnConvert = new System.Windows.Forms.Button();
-            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
             this.label1 = new System.Windows.Forms.Label();
             this.label2 = new System.Windows.Forms.Label();
             this.button1 = new System.Windows.Forms.Button();
             this.label3 = new System.Windows.Forms.Label();
-            this.textBox1 = new System.Windows.Forms.TextBox();
-            this.button2 = new System.Windows.Forms.Button();
-            this.button3 = new System.Windows.Forms.Button();
-            this.label4 = new System.Windows.Forms.Label();
+            this.tbOutputDir = new System.Windows.Forms.TextBox();
+            this.lbConvertResult = new System.Windows.Forms.Label();
+            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
+            this.tbInputDir = new System.Windows.Forms.TextBox();
             this.panel1.SuspendLayout();
             this.SuspendLayout();
             // 
@@ -47,16 +48,37 @@
             // 
             this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
-            this.panel1.Controls.Add(this.button3);
-            this.panel1.Controls.Add(this.button2);
-            this.panel1.Location = new System.Drawing.Point(0, 12);
+            this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
+            this.panel1.Controls.Add(this.btnHome);
+            this.panel1.Controls.Add(this.btnAbout);
+            this.panel1.Location = new System.Drawing.Point(0, 0);
             this.panel1.Name = "panel1";
-            this.panel1.Size = new System.Drawing.Size(156, 362);
+            this.panel1.Size = new System.Drawing.Size(165, 459);
             this.panel1.TabIndex = 0;
             // 
+            // btnHome
+            // 
+            this.btnHome.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
+            this.btnHome.Location = new System.Drawing.Point(6, 120);
+            this.btnHome.Name = "btnHome";
+            this.btnHome.Size = new System.Drawing.Size(152, 37);
+            this.btnHome.TabIndex = 8;
+            this.btnHome.Text = "关于我们";
+            this.btnHome.UseVisualStyleBackColor = false;
+            // 
+            // btnAbout
+            // 
+            this.btnAbout.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
+            this.btnAbout.Location = new System.Drawing.Point(6, 77);
+            this.btnAbout.Name = "btnAbout";
+            this.btnAbout.Size = new System.Drawing.Size(152, 37);
+            this.btnAbout.TabIndex = 7;
+            this.btnAbout.Text = "首页";
+            this.btnAbout.UseVisualStyleBackColor = false;
+            // 
             // btnConvert
             // 
-            this.btnConvert.Location = new System.Drawing.Point(337, 275);
+            this.btnConvert.Location = new System.Drawing.Point(425, 278);
             this.btnConvert.Name = "btnConvert";
             this.btnConvert.Size = new System.Drawing.Size(152, 37);
             this.btnConvert.TabIndex = 1;
@@ -64,15 +86,11 @@
             this.btnConvert.UseVisualStyleBackColor = true;
             this.btnConvert.Click += new System.EventHandler(this.btnConvert_Click);
             // 
-            // openFileDialog1
-            // 
-            this.openFileDialog1.FileName = "openFileDialog1";
-            // 
             // label1
             // 
             this.label1.AutoSize = true;
             this.label1.Font = new System.Drawing.Font("SimSun", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label1.Location = new System.Drawing.Point(263, 56);
+            this.label1.Location = new System.Drawing.Point(332, 62);
             this.label1.Name = "label1";
             this.label1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
             this.label1.Size = new System.Drawing.Size(274, 21);
@@ -82,81 +100,80 @@
             // label2
             // 
             this.label2.AutoSize = true;
-            this.label2.Location = new System.Drawing.Point(210, 179);
+            this.label2.Location = new System.Drawing.Point(256, 162);
             this.label2.Name = "label2";
-            this.label2.Size = new System.Drawing.Size(107, 12);
+            this.label2.Size = new System.Drawing.Size(119, 12);
             this.label2.TabIndex = 3;
-            this.label2.Text = "选择文件/文件夹:";
+            this.label2.Text = "OneNote笔记文件夹:";
             // 
             // button1
             // 
-            this.button1.Location = new System.Drawing.Point(323, 174);
+            this.button1.Font = new System.Drawing.Font("SimSun", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.button1.Location = new System.Drawing.Point(630, 157);
             this.button1.Name = "button1";
-            this.button1.Size = new System.Drawing.Size(241, 23);
+            this.button1.Size = new System.Drawing.Size(80, 23);
             this.button1.TabIndex = 4;
-            this.button1.Text = "转换";
+            this.button1.Text = "选择文件夹";
             this.button1.UseVisualStyleBackColor = true;
             this.button1.Click += new System.EventHandler(this.button1_Click);
             // 
             // label3
             // 
             this.label3.AutoSize = true;
-            this.label3.Location = new System.Drawing.Point(252, 225);
+            this.label3.Location = new System.Drawing.Point(310, 230);
             this.label3.Name = "label3";
             this.label3.Size = new System.Drawing.Size(65, 12);
             this.label3.TabIndex = 5;
             this.label3.Text = "输出目录:";
             // 
-            // textBox1
-            // 
-            this.textBox1.Location = new System.Drawing.Point(323, 221);
-            this.textBox1.Name = "textBox1";
-            this.textBox1.Size = new System.Drawing.Size(241, 21);
-            this.textBox1.TabIndex = 6;
-            // 
-            // button2
+            // tbOutputDir
             // 
-            this.button2.Location = new System.Drawing.Point(0, 77);
-            this.button2.Name = "button2";
-            this.button2.Size = new System.Drawing.Size(152, 37);
-            this.button2.TabIndex = 7;
-            this.button2.Text = "首页";
-            this.button2.UseVisualStyleBackColor = true;
+            this.tbOutputDir.BorderStyle = System.Windows.Forms.BorderStyle.None;
+            this.tbOutputDir.Font = new System.Drawing.Font("SimSun", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.tbOutputDir.Location = new System.Drawing.Point(381, 229);
+            this.tbOutputDir.Name = "tbOutputDir";
+            this.tbOutputDir.Size = new System.Drawing.Size(241, 15);
+            this.tbOutputDir.TabIndex = 6;
             // 
-            // button3
+            // lbConvertResult
             // 
-            this.button3.Location = new System.Drawing.Point(0, 120);
-            this.button3.Name = "button3";
-            this.button3.Size = new System.Drawing.Size(152, 37);
-            this.button3.TabIndex = 8;
-            this.button3.Text = "关于我们";
-            this.button3.UseVisualStyleBackColor = true;
+            this.lbConvertResult.AutoSize = true;
+            this.lbConvertResult.Location = new System.Drawing.Point(457, 332);
+            this.lbConvertResult.Name = "lbConvertResult";
+            this.lbConvertResult.Size = new System.Drawing.Size(0, 12);
+            this.lbConvertResult.TabIndex = 7;
             // 
-            // label4
+            // tbInputDir
             // 
-            this.label4.AutoSize = true;
-            this.label4.Location = new System.Drawing.Point(377, 335);
-            this.label4.Name = "label4";
-            this.label4.Size = new System.Drawing.Size(89, 12);
-            this.label4.TabIndex = 7;
-            this.label4.Text = "全部转换成功。";
+            this.tbInputDir.BorderStyle = System.Windows.Forms.BorderStyle.None;
+            this.tbInputDir.Font = new System.Drawing.Font("SimSun", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.tbInputDir.Location = new System.Drawing.Point(381, 161);
+            this.tbInputDir.Name = "tbInputDir";
+            this.tbInputDir.Size = new System.Drawing.Size(241, 15);
+            this.tbInputDir.TabIndex = 9;
             // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
+            this.BackColor = System.Drawing.SystemColors.Control;
             this.ClientSize = new System.Drawing.Size(800, 450);
-            this.Controls.Add(this.label4);
-            this.Controls.Add(this.textBox1);
+            this.Controls.Add(this.tbInputDir);
+            this.Controls.Add(this.lbConvertResult);
+            this.Controls.Add(this.tbOutputDir);
             this.Controls.Add(this.panel1);
             this.Controls.Add(this.label3);
             this.Controls.Add(this.label1);
             this.Controls.Add(this.button1);
             this.Controls.Add(this.btnConvert);
             this.Controls.Add(this.label2);
+            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
             this.Name = "MainForm";
-            this.Text = "Form1";
+            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
+            this.Text = "OneNote转换工具";
+            this.Load += new System.EventHandler(this.MainForm_Load);
             this.panel1.ResumeLayout(false);
             this.ResumeLayout(false);
             this.PerformLayout();
@@ -167,15 +184,16 @@
 
         private System.Windows.Forms.Panel panel1;
         private System.Windows.Forms.Button btnConvert;
-        private System.Windows.Forms.OpenFileDialog openFileDialog1;
         private System.Windows.Forms.Label label2;
         private System.Windows.Forms.Label label1;
         private System.Windows.Forms.Button button1;
-        private System.Windows.Forms.TextBox textBox1;
+        private System.Windows.Forms.TextBox tbOutputDir;
         private System.Windows.Forms.Label label3;
-        private System.Windows.Forms.Button button3;
-        private System.Windows.Forms.Button button2;
-        private System.Windows.Forms.Label label4;
+        private System.Windows.Forms.Button btnHome;
+        private System.Windows.Forms.Button btnAbout;
+        private System.Windows.Forms.Label lbConvertResult;
+        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
+        private System.Windows.Forms.TextBox tbInputDir;
     }
 }
 

+ 60 - 2
OneNote2PDF/MainForm.cs

@@ -1,10 +1,15 @@
-using System;
+using Aspose.Note;
+using Aspose.Note.Saving;
+using OneNote2PDF.Utils;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
@@ -12,6 +17,8 @@ namespace OneNote2PDF
 {
     public partial class MainForm : Form
     {
+        App app;
+
         public MainForm()
         {
             InitializeComponent();
@@ -19,12 +26,63 @@ namespace OneNote2PDF
 
         private void btnConvert_Click(object sender, EventArgs e)
         {
-
+            // 判断输入 OneNote 笔记目录是否正确
+            if (app.inputDir!=null || app.inputDir!="")
+            {
+                lbConvertResult.Text = "正在转换中,请稍耐心等待...";
+                foreach (DirectoryInfo subDirInfo in (new DirectoryInfo(app.inputDir)).GetDirectories())
+                {
+                    string subDir = Path.Combine(app.outputDir, subDirInfo.Name); //输出目录
+                    if (!Directory.Exists(subDir))
+                    {
+                        Directory.CreateDirectory(subDir);
+                    }
+                    foreach (FileInfo file in subDirInfo.GetFiles())
+                    {
+                        //只有后缀为one的文件才处理
+                        if (file.Name.Substring(file.Name.Length - 3, 3) == "one")
+                        {
+                            // 异步执行
+                            ThreadStart starter = delegate {
+                                OneNoteUtils.One2PDF(file.FullName, Path.Combine(app.outputDir, subDirInfo.Name, file.Name.Substring(0, file.Name.Length - 4) + ".pdf"));
+                            };
+                            new Thread(starter).Start();
+                        }
+                        return;
+                    }
+                    lbConvertResult.Text = "全部转换成功";
+                }
+            }
+            else
+            {
+                MessageBox.Show("请先选择正确的 OneNote 笔记目录再执行转换。");
+            }
         }
 
         private void button1_Click(object sender, EventArgs e)
         {
+            folderBrowserDialog1.Description = "请选择文件夹";
+            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            {
+                if (string.IsNullOrEmpty(folderBrowserDialog1.SelectedPath))
+                {
+                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
+                    return;
+                }
+                else
+                {
+                    app.inputDir = folderBrowserDialog1.SelectedPath;
+                    tbInputDir.Text = app.inputDir;
+                }
+            }
 
         }
+
+        private void MainForm_Load(object sender, EventArgs e)
+        {
+            app = App.getInstance();//初始化app对象
+            tbOutputDir.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),"Output");
+            app.outputDir = tbOutputDir.Text;
+        }
     }
 }

+ 2 - 2
OneNote2PDF/MainForm.resx

@@ -117,8 +117,8 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
-  <metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
-    <value>17, 17</value>
+  <metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>25, 10</value>
   </metadata>
   <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
   <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">

+ 5 - 0
OneNote2PDF/OneNote2PDF.csproj

@@ -32,6 +32,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationIcon>Resources\logo.ico</ApplicationIcon>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="Aspose.Note, Version=18.11.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
       <HintPath>..\packages\Aspose.Note.18.11.0\lib\net4.0\Aspose.Note.dll</HintPath>
@@ -49,6 +52,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="App.cs" />
     <Compile Include="MainForm.cs">
       <SubType>Form</SubType>
     </Compile>
@@ -58,6 +62,7 @@
     <Compile Include="Model\Note.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Utils\AsyncTaskQueue.cs" />
     <Compile Include="Utils\OneNoteUtils.cs" />
     <Compile Include="Views\About.cs">
       <SubType>UserControl</SubType>

+ 3 - 3
OneNote2PDF/Properties/AssemblyInfo.cs

@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("")]
 [assembly: AssemblyProduct("OneNote2PDF")]
-[assembly: AssemblyCopyright("Copyright ©  2021")]
+[assembly: AssemblyCopyright("Copyright ©  ZhiZhou Technology 2021")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.1.0")]
+[assembly: AssemblyFileVersion("1.0.1.0")]

+ 18 - 0
OneNote2PDF/Utils/AsyncTaskQueue.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace OneNote2PDF.Utils
+{
+    class AsyncTaskQueue
+    {
+
+        static void multiThread(string select,long num)
+        {
+            ThreadPool.SetMinThreads(500, 10);
+        }
+    }
+}

+ 26 - 4
OneNote2PDF/Utils/OneNoteUtils.cs

@@ -1,4 +1,5 @@
 using Aspose.Note;
+using Aspose.Note.Saving;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -12,13 +13,34 @@ namespace OneNote2PDF.Utils
 
         string dataDir = "";
 
-        public bool One2PDF(string filePath)
+        /// <summary>
+        /// one 文件转化为 pdf
+        /// </summary>
+        /// <param name="inputFile"></param>
+        /// <param name="outputFile"></param>
+        /// <returns></returns>
+        public static bool One2PDF(string inputFile, string outputFile)
         {
-            Document oneFile = new Document(dataDir + "Aspose.one");
-            dataDir = dataDir + "SaveWithDefaultSettings_out.pdf";
-            oneFile.Save(dataDir, SaveFormat.Pdf);
+            //异步执行
+            
+
+
+            Document oneFile = new Document(inputFile);
+            oneFile.Save(outputFile, SaveFormat.Pdf);
             return true;
         }
 
+        /// <summary>
+        /// one文件转化为html ,注意每个one文件分别存一个目录
+        /// </summary>
+        /// <param name="inputFile"></param>
+        /// <param name="outputFile"></param>
+        /// <returns></returns>
+        public static bool One2Html(string inputFile, string outputFile)
+        {
+            Document oneFile = new Document(inputFile);
+            oneFile.Save(outputFile, SaveFormat.Html);
+            return true;
+        }
     }
 }

+ 7 - 0
README.md

@@ -2,9 +2,16 @@
 
 一直使用 Onenote 2016 做笔记,由于国内“众所周知”的原因, OneNote 网络同步很不好用,跨平台体验一直很差,有时候下班在地铁希望通过手机看看 OneNote 笔记,这些愿望都无法实现,遂开发这个工具实现 Onenote 文件 .one 批量转换为 pdf 等通用格式。这样就可以在其他平台方便查看了。
 
+## screenshot
 
+![](screenshot/BaiduHi_2021-1-12_19-43-11.png)
 
 
+## 遗留问题
+
+1、有用户需求,后续可以继续完善。
+
+2、采用 aspose.note 依赖,单个授权就是 399 美元。本项目开源版本未付费,所以转换后的 pdf 文件会有 aspose 网站的水印。
 
 致谢:
 

BIN
screenshot/BaiduHi_2021-1-12_19-43-11.png