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 importoutxml(Word.Document DOC, string HtmlFileName, EPUB mEpub) { int p = 0; XmlDocument xml = mEpub.getncxml(); XmlNode navMapNode = xml.SelectSingleNode("ncx/navMap"); string webfile = HtmlFileName.Substring(HtmlFileName.LastIndexOf(@"\") + 1); try { object x = DOC.GetCrossReferenceItems(Microsoft.Office.Interop.Word.WdReferenceType.wdRefTypeHeading); Array strs = (Array)x; int cc = 0; string pstr; foreach (Word.Paragraph Paragr in DOC.Paragraphs) { int level = (int)Paragr.OutlineLevel; cc = 0; pstr = ""; if (level < 9) { p++; try { Paragr.ID = "_ncx_toc_" + p.ToString(); string tstr = ReplaceLowOrderASCIICharacters(Paragr.Range.Text.Trim()); if (p == 1) { xml.SelectSingleNode("ncx/docTitle/text").InnerXml = tstr; } for (int i = 0; i < strs.Length; i++) { string hreadingstr = strs.GetValue(i + 1).ToString(); if (hreadingstr.IndexOf(tstr) >= 0) { tstr = hreadingstr; cc = hreadingstr.Length - hreadingstr.TrimStart().Length; for (int pi = i; pi >= 0; pi--) { string phreadingstr = strs.GetValue(pi + 1).ToString(); int pc = phreadingstr.Length - phreadingstr.TrimStart().Length; if (pc < cc) { pstr = phreadingstr; break; } } break; } } XmlElement navPointEl = xml.CreateElement("navPoint"); navPointEl.Prefix = Paragr.Range.ParagraphStyle.NameLocal.ToString(); navPointEl.SetAttribute("id", "navPoint-" + p.ToString()); navPointEl.SetAttribute("playOrder", p.ToString()); XmlElement subPoint = xml.CreateElement("navLabel"); subPoint.InnerXml = "<text>" + tstr.Trim() + "</text>"; XmlElement ConPoint = xml.CreateElement("content"); ConPoint.SetAttribute("src", "Text/" + mEpub.chapterName + ".xhtml#_ncx_toc_" + p.ToString()); navPointEl.AppendChild(subPoint); navPointEl.AppendChild(ConPoint); if (pstr.Length > 0) { for (int i = navMapNode.ChildNodes.Count - 1; i >= 0; i--) { XmlElement xe = (XmlElement)navMapNode.ChildNodes[i]; if (pstr.IndexOf(xe.SelectSingleNode("navLabel").SelectSingleNode("text").InnerXml) >= 0) { xe.AppendChild(navPointEl); } else { if (xe.ChildNodes.Count > 1) { foreachnode(xe, navPointEl, pstr); } } } } else { navMapNode.AppendChild(navPointEl); } } catch (Exception ex) { continue; } } } } catch (Exception) { return; } DOC.SaveAs(HtmlFileName, Word.WdSaveFormat.wdFormatFilteredHTML, false, "", false); }