private void importouttemp(object obj) { Word.Document doc = (Word.Document)obj; Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); string session = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now); if (doc.FullName.IndexOf(@"\") <= 0) { MessageBox.Show(ePUBee.getLang.getString("filehasnotsavedpleasesavethefileatfirst")); groupProcessing.Visible = false; btnPublish.Enabled = true; timer1.Enabled = false; return; } groupProcessing.Visible = true; btnPublish.Enabled = false; timer1.Enabled = true; EPUB mEpub = new EPUB(); string Filepath = System.IO.Path.GetTempPath();; String SaveName = System.IO.Path.GetTempPath() + session + ".html"; string SavePath = System.IO.Path.GetTempPath() + session + ".files"; String chapterName = "content"; object type = WdBreakType.wdSectionBreakContinuous; object missing = System.Reflection.Missing.Value; object confirmConversion = false; object link = false; object attachment = false; mEpub.FilePath = Filepath; mEpub.NcxFileName = "toc"; mEpub.OpsFileName = "content"; mEpub.chapterName = chapterName; mEpub.Init(); XmlNode manifest = mEpub.getopfxml().SelectSingleNode("package/manifest"); try { object start = 0; Word.Application sword = new Word.Application(); Word.Document sdoc = sword.Documents.Add(); sword.Selection.InsertFile(doc.FullName, ref missing, ref confirmConversion, ref link, ref attachment); importoutxml(sdoc, SaveName, mEpub); sdoc.Close(false, ref missing, ref missing); sword.Quit(false, ref missing, ref missing); if (System.IO.Directory.Exists(SavePath + "\\") == false) { if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + session + "_files\\") == true) { SavePath = System.IO.Path.GetTempPath() + session + "_files"; } } XHtmlTools XHtml = new XHtmlTools(); string str = (new StreamReader(SaveName, Encoding.Default)).ReadToEnd(); string tpath = SavePath.Substring(SavePath.LastIndexOf(@"\") + 1); str = str.Replace(tpath, "../Images"); byte[] bt = System.Text.Encoding.UTF8.GetBytes(str); string htmlstr = XHtml.GetXmlDocument(bt); string HtmlFileName = mEpub.InsertHtml(htmlstr, ""); mEpub.InsertCss(str); XmlElement CPoint = mEpub.getopfxml().CreateElement("item"); CPoint.SetAttribute("href", "Text/" + HtmlFileName); CPoint.SetAttribute("id", HtmlFileName); CPoint.SetAttribute("media-type", "application/xhtml+xml"); manifest.AppendChild(CPoint); MatchCollection matches = regImg.Matches(htmlstr); string imgurl = null; foreach (Match match in matches) { imgurl = match.Groups["imgUrl"].Value; XmlElement ConPoint = mEpub.getopfxml().CreateElement("item"); ConPoint.SetAttribute("href", imgurl.Replace("../", "")); ConPoint.SetAttribute("id", imgurl.Substring(imgurl.LastIndexOf(@"/") + 1)); if (imgurl.ToLower().IndexOf(".jpg") > 1) { ConPoint.SetAttribute("media-type", "image/jpeg"); } else if (imgurl.ToLower().IndexOf(".png") > 1) { ConPoint.SetAttribute("media-type", "image/png"); } else if (imgurl.ToLower().IndexOf(".gif") > 1) { ConPoint.SetAttribute("media-type", "image/gif"); } else { ConPoint.SetAttribute("media-type", "image/jpeg"); } manifest.AppendChild(ConPoint); } if (System.IO.Directory.Exists(SavePath + "\\")) { CopyFolder(SavePath, mEpub.ImagesPath); } } catch (Exception) { mEpub.Clear(); groupProcessing.Visible = false; btnPublish.Enabled = true; timer1.Enabled = false; return; } SaveHtmlForm f = new SaveHtmlForm(); f.htmlpath = mEpub.htmlpath; f.ShowDialog(); if (f.isok == false) { mEpub.Clear(); groupProcessing.Visible = false; btnPublish.Enabled = true; timer1.Enabled = false; return; } if (f.SelectFile != string.Empty || f.SelectFile.Length > 0) { mEpub.setcover(f.SelectFile); } MetaEdit mEdit = new MetaEdit(); mEdit.xml = mEpub.getopfxml(); mEdit.ShowDialog(); if (mEdit.isok == false) { mEpub.Clear(); groupProcessing.Visible = false; btnPublish.Enabled = true; timer1.Enabled = false; return; } BuildPath blp = new BuildPath(); blp.xmldoc = mEpub.getncxml(); blp.ShowDialog(); if (blp.isok == true) { mEpub.WriteNcx(); mEpub.WriteOpfContent(); mEpub.Buildfile(blp.txtPath.Text); } groupProcessing.Visible = false; btnPublish.Enabled = true; timer1.Enabled = false; }
private void importxml(object obj) { Word.Document doc = (Word.Document)obj; //throw new Exception("publish start, " + ePUBee.getLang.getString("publish")); if (doc.FullName.IndexOf(@"\") <= 0) { MessageBox.Show(ePUBee.getLang.getString("filehasnotsavedpleasesavethefileatfirst")); return; } string lanstr = doc.Content.LanguageIDOther.ToString(); if (Regex.Matches(lanstr, "[a-zA-Z]").Count <= 0) { lanstr = "MultiLanguzge"; } else { lanstr = lanstr.Remove(0, 2); } Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); string session = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now); //string tempwordfile = System.IO.Path.GetTempPath() + session + doc.FullName.Substring(doc.FullName.LastIndexOf(@".")); string outfile = ""; string SaveName = System.IO.Path.GetTempPath() + session + ".html"; string SavePath = System.IO.Path.GetTempPath() + session + ".files"; object type = WdBreakType.wdSectionBreakContinuous; object missing = System.Reflection.Missing.Value; object confirmConversion = false; object link = false; object attachment = false; object start = 0; groupProcessing.Visible = true; btnQuickPublish.Enabled = false; timer1.Enabled = true; if (saveFileDialog2.ShowDialog() == DialogResult.Cancel) { groupProcessing.Visible = false; btnQuickPublish.Enabled = true; timer1.Enabled = false; return; } string outpath = saveFileDialog2.FileName; //File.Copy(doc.FullName, tempwordfile); EPUB mEpub = new EPUB(); String chapterName = "content"; mEpub.FilePath = System.IO.Path.GetTempPath(); mEpub.Language = lanstr; mEpub.NcxFileName = "toc"; mEpub.OpsFileName = "content"; mEpub.chapterName = chapterName; mEpub.Init(); try { XmlNode manifest = mEpub.getopfxml().SelectSingleNode("package/manifest"); Word.Application sword = new Word.Application(); Word.Document sdoc = sword.Documents.Add(); sword.Visible = false; sword.Selection.InsertFile(doc.FullName, ref missing, ref confirmConversion, ref link, ref attachment); importoutxml(sdoc, SaveName, mEpub); sdoc.Close(false, ref missing, ref missing); sword.Quit(false, ref missing, ref missing); if (System.IO.Directory.Exists(SavePath + "\\") == false) { if (System.IO.Directory.Exists(System.IO.Path.GetTempPath() + session + "_files\\") == true) { SavePath = System.IO.Path.GetTempPath() + session + "_files"; } } XHtmlTools XHtml = new XHtmlTools(); StreamReader sr = new StreamReader(SaveName, Encoding.Default); string str = sr.ReadToEnd(); sr.Close(); string tpath = SavePath.Substring(SavePath.LastIndexOf(@"\") + 1); str = str.Replace(tpath, "../Images"); byte[] bt = System.Text.Encoding.UTF8.GetBytes(str); string htmlstr = XHtml.GetXmlDocument(bt); string HtmlFileName = mEpub.InsertHtml(htmlstr, ""); mEpub.InsertCss(str); XmlElement CPoint = mEpub.getopfxml().CreateElement("item"); CPoint.SetAttribute("href", "Text/" + HtmlFileName); CPoint.SetAttribute("id", HtmlFileName); CPoint.SetAttribute("media-type", "application/xhtml+xml"); manifest.AppendChild(CPoint); MatchCollection matches = regImg.Matches(htmlstr); string imgurl = null; foreach (Match match in matches) { imgurl = match.Groups["imgUrl"].Value; XmlElement ConPoint = mEpub.getopfxml().CreateElement("item"); ConPoint.SetAttribute("href", imgurl.Replace("../", "")); ConPoint.SetAttribute("id", imgurl.Substring(imgurl.LastIndexOf(@"/") + 1)); if (imgurl.ToLower().IndexOf(".jpg") > 1) { ConPoint.SetAttribute("media-type", "image/jpeg"); } else if (imgurl.ToLower().IndexOf(".png") > 1) { ConPoint.SetAttribute("media-type", "image/png"); } else if (imgurl.ToLower().IndexOf(".gif") > 1) { ConPoint.SetAttribute("media-type", "image/gif"); } else { ConPoint.SetAttribute("media-type", "image/jpeg"); } manifest.AppendChild(ConPoint); } if (System.IO.Directory.Exists(SavePath + "\\")) { CopyFolder(SavePath, mEpub.ImagesPath); } mEpub.WriteNcx(); mEpub.WriteOpfContent(); outfile = mEpub.Buildfile(outpath); File.Delete(SaveName); if (System.IO.Directory.Exists(SavePath + "\\")) { System.IO.Directory.Delete(SavePath + "\\", true); } mEpub.Clear(); } catch (Exception err) { groupProcessing.Visible = false; btnQuickPublish.Enabled = true; timer1.Enabled = false; MessageBox.Show(err.Message); return; } groupProcessing.Visible = false; btnQuickPublish.Enabled = true; timer1.Enabled = false; BuildOk bok = new BuildOk(); bok.linkLabel1.Text = ePUBee.getLang.getString("openfile"); bok.linkLabel1.Tag = outfile; bok.ShowDialog(); }