public override bool transform() { XmlReader source = null; XmlWriter writer = null; bool isSuccess = true; // string pre2OutputFileName = Path.GetDirectoryName(inputFile).ToString() +"\\"+ "tmpDoc2.xml"; try { XPathDocument xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.UOFToOOX_PRETREAT_STEP2_XSL, TranslatorConstants.UOFToOOX_WORD_LOCATION); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(xpdoc); source = XmlReader.Create(inputFile); writer = new XmlTextWriter(outputFile, Encoding.UTF8); // writer = new XmlTextWriter(pre2OutputFileName, Encoding.UTF8); xslt.Transform(source, writer); // OutputFilename = pre2OutputFileName; } catch (Exception e) { logger.Error(e.Message); logger.Error(e.StackTrace); isSuccess = false; } finally { if (writer != null) { writer.Close(); } if (source != null) { source.Close(); } } return(isSuccess); }
public override bool transform() { FileStream fs = null; //XmlUrlResolver resourceResolver = null; XmlReader xr = null; string extractPath = Path.GetDirectoryName(outputFile) + Path.AltDirectorySeparatorChar; string prefix = this.GetType().Namespace + "." + TranslatorConstants.RESOURCE_LOCATION + "." + TranslatorConstants.UOFToOOX_WORD_LOCATION; string uof2ooxpre = extractPath + "uof2ooxpre.xml"; string extend = extractPath + "extend.xml"; string extendPre = extractPath + "extendPre.xml"; string tmpSect1 = extractPath + "tmpSect1.xml"; string tmpSect2 = extractPath + "tmpSect2.xml"; string tblVtcl = extractPath + "tblVertical.xml"; string picture_xml = "data"; try { UOFToOoxTableProcessing tableProcessing2 = new UOFToOoxTableProcessing(extractPath + "content.xml"); tableProcessing2.Processing(tblVtcl); // copy XSLT templates Assembly asm = Assembly.GetExecutingAssembly(); foreach (string name in asm.GetManifestResourceNames()) { if (name.StartsWith(prefix)) { string filename = name.Substring(prefix.Length + 1); FileStream writer = new FileStream(extractPath + filename, FileMode.Create); Stream baseStream = asm.GetManifestResourceStream(name); int Length = 10240; Byte[] buffer = new Byte[Length]; int bytesRead = baseStream.Read(buffer, 0, Length); while (bytesRead > 0) { writer.Write(buffer, 0, bytesRead); bytesRead = baseStream.Read(buffer, 0, Length); } baseStream.Close(); writer.Close(); } } //resourceResolver = new ResourceResolver(Assembly.GetExecutingAssembly(), prefix); //xr = XmlReader.Create(((ResourceResolver)resourceResolver).GetInnerStream("uof2oopre.xslt")); xr = XmlReader.Create(extractPath + "uof2ooxpre.xsl"); // XPathDocument doc = new XPathDocument(extractPath + @"content.xml"); XPathDocument doc = new XPathDocument(tblVtcl); XslCompiledTransform transFrom = new XslCompiledTransform(); XsltSettings setting = new XsltSettings(true, false); XmlUrlResolver xur = new XmlUrlResolver(); transFrom.Load(xr, setting, xur); XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator(); fs = new FileStream(outputFile, FileMode.Create); // fs = new FileStream(uof2ooxpre, FileMode.Create); transFrom.Transform(nav, null, fs); fs.Close(); if (File.Exists(extend)) { XmlDocument xdoc1 = new XmlDocument(); xdoc1.Load(outputFile); XmlNamespaceManager nm = new XmlNamespaceManager(xdoc1.NameTable); nm.AddNamespace("uof", TranslatorConstants.XMLNS_UOF); XmlDocument xdoc2 = new XmlDocument(); xdoc2.Load(extend); XmlNode extendNode = xdoc1.CreateElement("uof", "扩展区", TranslatorConstants.XMLNS_UOF); extendNode.InnerXml = xdoc2.LastChild.InnerXml; xdoc1.SelectSingleNode("//uof:UOF", nm).AppendChild(extendNode); xdoc1.Save(extendPre); } else { extendPre = outputFile; } if (!Directory.Exists(extractPath + picture_xml)) { Directory.CreateDirectory(extractPath + picture_xml); } string[] patternFiles = Directory.GetFiles(extractPath, "Image*", SearchOption.TopDirectoryOnly); foreach (string patternFile in patternFiles) { File.Copy(patternFile, extractPath + picture_xml + Path.AltDirectorySeparatorChar + patternFile.Substring(patternFile.LastIndexOf("/") + 1), true); } if (Directory.Exists(extractPath + picture_xml)) { string tmpPic = extractPath + Path.AltDirectorySeparatorChar + "tmpPic.xml"; XmlDocument xdoc = new XmlDocument(); xdoc.Load(extendPre); XmlNameTable nt = xdoc.NameTable; XmlNamespaceManager nm = new XmlNamespaceManager(nt); nm.AddNamespace("w", TranslatorConstants.XMLNS_W); nm.AddNamespace("uof", TranslatorConstants.XMLNS_UOF); xdoc = PicPretreatment(xdoc, "uof:UOF", extractPath + picture_xml, nm); xdoc.Save(tmpPic); // OutputFilename = tmpPic; extendPre = tmpPic; } //第二步预处理 XPathDocument xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.UOFToOOX_PRETREAT_STEP2_XSL, TranslatorConstants.UOFToOOX_WORD_LOCATION); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(xpdoc); XmlWriter xw = new XmlTextWriter(tmpSect1, Encoding.UTF8); //sndtemDoc.xml xslt.Transform(extendPre, xw); //tmpDoc--经过预处理2.xsl--sndtemDoc.xml if (xw != null) { xw.Close(); } //第三步预处理 xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.UOFToOOX_PRETREAT_SETP3_XSL, TranslatorConstants.UOFToOOX_WORD_LOCATION); xslt.Load(xpdoc); xw = new XmlTextWriter(tmpSect2, Encoding.UTF8); xslt.Transform(tmpSect1, xw); if (xw != null) { xw.Close(); } XmlDocument shdXdoc = new XmlDocument(); shdXdoc.Load(tmpSect2); XmlNameTable nameTable = shdXdoc.NameTable; XmlNamespaceManager snm = new XmlNamespaceManager(nameTable); snm.AddNamespace("图", TranslatorConstants.XMLNS_UOFGRAPH); snm.AddNamespace("uof", TranslatorConstants.XMLNS_UOF); XmlNodeList shapeShades = shdXdoc.SelectNodes("//图:阴影_8051/uof:偏移量_C61B", snm); if (shapeShades != null) { foreach (XmlNode shapeShade in shapeShades) { if (((XmlElement)shapeShade).HasAttribute("x_C606") && ((XmlElement)shapeShade).HasAttribute("y_C607")) { if (shapeShade.Attributes.GetNamedItem("x_C606").Value != "NaN" && shapeShade.Attributes.GetNamedItem("y_C607").Value != "NaN") { double x = Convert.ToDouble(shapeShade.Attributes.GetNamedItem("x_C606").Value); double y = Convert.ToDouble(shapeShade.Attributes.GetNamedItem("y_C607").Value); double angValueTe = Math.Atan2(Math.Abs(y), Math.Abs(x)) * 180 / 3.1415926;//x,y位置调换,修复阴影-角度转换BUG double distanceValue = Math.Sqrt((x * x) + (y * y)) * 12700; double angValue = 0; if (x >= 0 && y >= 0) { angValue = angValueTe * 60000; } if (x >= 0 && y < 0) { angValue = (angValueTe + 270) * 60000; } if (x < 0 && y >= 0) { angValue = (angValueTe + 90) * 60000; } if (x < 0 && y < 0) { angValue = (angValueTe + 180) * 60000; } XmlElement ang = shdXdoc.CreateElement("dir"); ang.InnerText = Convert.ToString(angValue); shapeShade.AppendChild(ang); XmlElement distance = shdXdoc.CreateElement("dist"); distance.InnerText = Convert.ToString(distanceValue); shapeShade.AppendChild(distance); } } } } shdXdoc.Save(outputFile); //公式 if (File.Exists(extractPath + "equations.xml")) { string tmpEqu = extractPath + Path.AltDirectorySeparatorChar + "tmpEqu.xml"; string equXML = extractPath + "equations.xml"; XmlDocument equDoc = new XmlDocument(); equDoc.Load(equXML); XmlDocument xdoc = new XmlDocument(); xdoc.Load(outputFile); XmlNode equ = xdoc.ImportNode(equDoc.LastChild, true); xdoc.LastChild.AppendChild(equ); xdoc.Save(tmpEqu); outputFile = tmpEqu; } //outputFile = tmpSect2; // preMethod(uof2ooxpre); return(true); } catch (Exception ex) { logger.Error("Fail in Uof2.0 to OOX pretreatment1: " + ex.Message); logger.Error(ex.StackTrace); throw new Exception("Fail in Uof2.0 to OOX pretreatment1 of WORD"); } finally { if (fs != null) { fs.Close(); } } }
public override bool transform() { //string Document_xml = "word/document.xml";//word下的doucument.xml文档 string picture_xml = "word\\media"; // string tmpFile = Path.GetTempPath() + "tmpDoc.xml"; // string tmpFile2 = Path.GetTempPath() + "sndtmpDoc.xml"; // Guid gPath = Guid.NewGuid(); // string wordPrePath = Path.GetTempPath() + gPath.ToString() + "\\"; // if (!Directory.Exists(wordPrePath)) // Directory.CreateDirectory(wordPrePath); string wordPrePath = Path.GetDirectoryName(outputFile) + "\\";//输出文档路径 string mediaPath = wordPrePath + picture_xml; string tblVtcl = wordPrePath + "tblVertical.xml"; string tmpFile = wordPrePath + "tmpDoc.xml"; string tmpFile2 = wordPrePath + "sndtmpDoc.xml"; //预处理中的中间文档 string tmpFile3 = wordPrePath + "thrtmpDoc.xml"; //预处理中的中间文档 string preOutputFileName = wordPrePath + "tempdoc.xml"; //预处理出来的中间文档 XmlReader source = null; XmlWriter writer = null; ZipReader archive = ZipFactory.OpenArchive(inputFile); archive.ExtractOfficeDocument(inputFile, wordPrePath); //archive.ExtractUOFDocument(inputFile, wordPrePath); bool isSuccess = true; try { OoxToUofTableProcessing tableProcessing2 = new OoxToUofTableProcessing(wordPrePath + "word" + Path.AltDirectorySeparatorChar + "document.xml"); tableProcessing2.Processing(tblVtcl); //找到预处理第一步的式样单pretreatmentStep1.xsl XPathDocument xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP1_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(xpdoc); // source = XmlReader.Create(archive.GetEntry(Document_xml));//解压,得到document.xml文档 source = XmlReader.Create(tblVtcl); writer = new XmlTextWriter(tmpFile, Encoding.UTF8); //tmpDoc.xml xslt.Transform(source, writer); //document.xml--经过预处理.xsl--tmpDoc.xml if (writer != null) { writer.Close(); } if (source != null) { source.Close(); } //第二步预处理 xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP2_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION); xslt = new XslCompiledTransform(); xslt.Load(xpdoc); writer = new XmlTextWriter(tmpFile2, Encoding.UTF8); //sndtemDoc.xml xslt.Transform(tmpFile, writer); //tmpDoc--经过预处理2.xsl--sndtemDoc.xml if (writer != null) { writer.Close(); } //第三步预处理 xpdoc = UOFTranslator.GetXPathDoc(TranslatorConstants.OOXToUOF_PRETREAT_STEP3_XSL, TranslatorConstants.OOXToUOF_WORD_LOCATION); xslt = new XslCompiledTransform(); xslt.Load(xpdoc); // xslt.Transform(tmpFile2, outputFile); xslt.Transform(tmpFile2, tmpFile3);//经过预处理3--tempdoc.xml //2011/6/17zhaobj:阴影预处理 XmlNamespaceManager nms = null; XmlDocument xmlDoc; XmlTextWriter resultWriter = null; xmlDoc = new XmlDocument(); xmlDoc.Load(tmpFile3); nms = new XmlNamespaceManager(xmlDoc.NameTable); nms.AddNamespace("a", TranslatorConstants.XMLNS_A); nms.AddNamespace("w", TranslatorConstants.XMLNS_W); XmlNodeList shapeShadeList = xmlDoc.SelectNodes("//a:outerShdw", nms); if (shapeShadeList != null) { foreach (XmlNode shapeShade in shapeShadeList) { double dist, dir; if (((XmlElement)shapeShade).HasAttribute("dist")) { dist = Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dist").Value) / 12700; } else { dist = 0.0; } if (((XmlElement)shapeShade).HasAttribute("dir")) { dir = (Convert.ToDouble(shapeShade.Attributes.GetNamedItem("dir").Value) * 3.1415) / (180 * 60000); } else { dir = 0.0; } double xValue = dist * Math.Cos(dir); double yValue = dist * Math.Sin(dir); XmlElement x = xmlDoc.CreateElement("x"); x.InnerText = Convert.ToString(xValue); shapeShade.AppendChild(x); XmlElement y = xmlDoc.CreateElement("y"); y.InnerText = Convert.ToString(yValue); shapeShade.AppendChild(y); } } resultWriter = new XmlTextWriter(preOutputFileName, System.Text.Encoding.UTF8); xmlDoc.Save(resultWriter); resultWriter.Close(); /* * * * */ OutputFilename = preOutputFileName; //图片预处理 //if (Directory.Exists(wordPrePath + picture_xml)) //{ // xmlDoc.Load(preOutputFileName); // DirectoryInfo mediaInfo = new DirectoryInfo(wordPrePath + picture_xml); // FileInfo[] medias = mediaInfo.GetFiles(); // XmlNode root = xmlDoc.SelectSingleNode("w:document", nms); // XmlElement mediaNode=xmlDoc.CreateElement("w","media",TranslatorConstants.XMLNS_W); // foreach (FileInfo media in medias) // { // XmlElement mediaFileNode = xmlDoc.CreateElement("u2opic", "picture", "urn:u2opic:xmlns:post-processings:special"); // mediaFileNode.SetAttribute("target", "urn:u2opic:xmlns:post-processings:special", media.FullName); // mediaNode.AppendChild(mediaFileNode); // } // root.AppendChild(mediaNode); // xmlDoc.Save(tmpPic); // OutputFilename = tmpPic; //} } catch (Exception e) { logger.Error("Fail in OoxToUofPreProcessorOneWord: " + e.Message); logger.Error(e.StackTrace); isSuccess = false; } finally { if (writer != null) { writer.Close(); } if (source != null) { source.Close(); } if (archive != null) { archive.Close(); } if (File.Exists(tmpFile2)) { File.Delete((tmpFile2)); } } return(isSuccess); }