using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Microsoft.Office.Interop.OneNote;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.OneNote;
using System.IO;
using System.Drawing.Imaging;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Acrobat;
using AcroPDFLib;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string v_strImgPath = string.Empty;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
v_strImgPath = this.openFileDialog1.FileName;
}
AcroPDFLib.AcroPDF acroPDF = new AcroPDFLib.AcroPDF();
//((System.ComponentModel.ISupportInitialize)(acroPDF)).BeginInit();
//acroPDF.Location = new Point(0, 24);
//acroPDF.Size = new Size(300, 300);
//acroPDF.Dock = DockStyle.Fill;
//this.Controls.Add(acroPDF);
//((System.ComponentModel.ISupportInitialize)(acroPDF)).EndInit();
acroPDF.LoadFile(v_strImgPath);
//MODI.Document doc = new MODI.Document();
//doc.Create(img_Path);
//MODI.Image image;
//MODI.Layout layout;
//doc.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, true, true); // 识别简体中文
//for (int i = 0; i < doc.Images.Count; i++)
//{
// image = (MODI.Image)doc.Images[i];
// layout = image.Layout;
// sb.Append(layout.Text);
//}
//MessageBox.Show(sb.ToString());
string v_strImgPath=string.Empty;
if(this.openFileDialog1.ShowDialog()== DialogResult.OK)
{
v_strImgPath=this.openFileDialog1.FileName;
}
//MyOcr myOcr = new MyOcr();
//string mes = string.Empty;
//myOcr.fnOCR(v_strImgPath, 2000, out mes);
//this.txtOCRed.Text = mes;
Bitmap bp = new Bitmap(v_strImgPath);
FileInfo file = new FileInfo(v_strImgPath);
var onenoteApp = new Microsoft.Office.Interop.OneNote.Application(); //onenote提供的API
string _Base64 = ImageToBase64(v_strImgPath);
/***************************************************************************************/
string sectionID;
onenoteApp.OpenHierarchy(AppDomain.CurrentDomain.BaseDirectory + @"\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
string pageID = "{0EA692EE-BB50-4E3C-AEF0-356D91732725}{1}{B0}";
onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle); // 建立新page
/********************************************************************************/
string notebookXml;
onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out notebookXml);
var doc = XDocument.Parse(notebookXml);
var ns = doc.Root.Name.Namespace;
var pageNode = doc.Descendants(ns + "Page").FirstOrDefault();
var existingPageId = pageNode.Attribute("ID").Value;
#region
if (pageNode != null)
{
//Image Type 只支持这些类型:auto|png|emf|jpg
string ImgExtension = file.Extension.ToLower().Substring(1);
switch (ImgExtension)
{
case "jpg":
ImgExtension = "jpg";
break;
case "png":
ImgExtension = "png";
break;
case "emf":
ImgExtension = "emf";
break;
default:
ImgExtension = "auto";
break;
}
#endregion
var page = new XDocument(new XElement(ns + "Page",
new XElement(ns + "Outline",
new XElement(ns + "OEChildren",
new XElement(ns + "OE",
new XElement(ns + "Image",
new XAttribute("format", ImgExtension), new XAttribute("originalPageNumber", "0"),
new XElement(ns + "Position",
new XAttribute("x", "0"), new XAttribute("y", "0"), new XAttribute("z", "0")),
new XElement(ns + "Size",
new XAttribute("width", bp.Width.ToString()), new XAttribute("height", bp.Height.ToString())),
new XElement(ns + "Data", _Base64)))))));
page.Root.SetAttributeValue("ID", existingPageId);
//保存图片进入Onenote页面
//注意以下几点:(待解决)
//1,onenote页面自动会生成一个页面,可能造成一直ocr错误。删除默认页面,新建空页
//2,图片存储在新建页面中,通过如下代码实现的,以追加存储方式进行。
onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
//线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
System.Threading.Thread.Sleep(2000);
string pageXml;
onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);
//获取OCR后的内容
FileStream tmpXml = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"\tmp.xml", FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(tmpXml);
sw.Write(pageXml);
sw.Flush();
sw.Close();
tmpXml.Close();
//加载xml中的数据
FileStream tmpOnenote = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"\tmp.xml", FileMode.Open, FileAccess.ReadWrite);
XmlReader reader = XmlReader.Create(tmpOnenote);
XElement rdlc = XElement.Load(reader);
// rdlc.Save(xpath);
/*****************************************************************************************************/
XmlNameTable nameTable = reader.NameTable;
XmlNamespaceManager mgr = new XmlNamespaceManager(nameTable);
mgr.AddNamespace("one", ns.ToString());
StringReader sr = new StringReader(pageXml);
XElement onenote = XElement.Load(sr);
读取xml中数据,并保存到文本中。
//XDocument doc1 = XDocument.Load(xpath);
var xml = from o in onenote.XPathSelectElements("//one:Image", mgr)
select o.XPathSelectElement("//one:OCRText", mgr).Value;
this.txtOCRed.Text = xml.First().ToString();
/**********************************************************************/
sr.Close();
reader.Close();
tmpOnenote.Close();
//DeleteXML(xpath);
onenoteApp.DeleteHierarchy(existingPageId);
//onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{46}{B0}");
//onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{50}{B0}");
}
}
//onenoteApp.CreateNewPage
// onenoteApp.UpdatePageContent();// 加入图片的base64数据
// onenoteApp.GetPageContent(); //从返回的XML里面提取OCR Data文本
// onenoteApp.DeleteHierarchy(); //删除新建的page
private string ImageToBase64(string v_strImgPath)
{
FileInfo file = new FileInfo(v_strImgPath);
#region //获取图片的Base64编码
using (MemoryStream ms = new MemoryStream())
{
Bitmap bp = new Bitmap(v_strImgPath);
switch (file.Extension.ToLower())
{
case ".jpg":
bp.Save(ms, ImageFormat.Jpeg);
break;
case ".jpeg":
bp.Save(ms, ImageFormat.Jpeg);
break;
case ".gif":
bp.Save(ms, ImageFormat.Gif);
break;
case ".bmp":
bp.Save(ms, ImageFormat.Bmp);
break;
case ".tiff":
bp.Save(ms, ImageFormat.Tiff);
break;
case ".png":
bp.Save(ms, ImageFormat.Png);
break;
case ".emf":
bp.Save(ms, ImageFormat.Emf);
break;
default:
//this.labMsg.Content = "不支持的图片格式。";
return string.Empty;
}
byte[] buffer = ms.GetBuffer();
string _Base64 = Convert.ToBase64String(buffer);
return _Base64;
}
#endregion
return string.Empty;
}
}
}