Example #1
0
        public override bool transform()
        {
            FileStream    fs          = null;
            XmlTextReader txtReader   = null;
            string        extractPath = Path.GetDirectoryName(outputFile) + "\\";
            ZipReader     archive     = ZipFactory.OpenArchive(inputFile);

            archive.ExtractOfficeDocument(inputFile, extractPath);
            string   prefix = this.GetType().Namespace + "." + TranslatorConstants.RESOURCE_LOCATION + ".Powerpoint.oox2uof";//路径
            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();
                }
            }
            bool isSuccess = true;

            try
            {
                // add theme rels
                AddThemeRels(extractPath);

                File.Copy(extractPath + "pre1.xsl", extractPath + @"\ppt\pre1.xsl", true);
                XPathDocument        doc       = new XPathDocument(extractPath + @"\ppt\presentation.xml");
                XslCompiledTransform transFrom = new XslCompiledTransform();
                XsltSettings         setting   = new XsltSettings(true, false);
                XmlUrlResolver       xur       = new XmlUrlResolver();
                transFrom.Load(extractPath + @"\ppt\pre1.xsl", setting, xur);
                XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
                fs = new FileStream(extractPath + "pre1tmp.xml", FileMode.Create);
                transFrom.Transform(nav, null, fs);
                fs.Close();
                doc = new XPathDocument(extractPath + "pre1tmp.xml");
                nav = ((IXPathNavigable)doc).CreateNavigator();
                fs  = new FileStream(extractPath + "pre2tmp.xml", FileMode.Create);
                Assembly ass = Assembly.Load("ppt_oox2uof");//使用预编译后的xslt
                Type     t   = ass.GetType("pre2");
                transFrom.Load(t);

                transFrom.Transform(nav, null, fs);
                fs.Close();
                xmlDoc    = new XmlDocument();
                txtReader = new XmlTextReader(extractPath + "pre2tmp.xml");
                xmlDoc.Load(txtReader);
                txtReader.Close();
                setNSManager();            //
                string currentPath = "//p:sp";
                purifyMethod(currentPath); //

                //2011-01-12罗文甜:阴影预处理
                XmlNodeList shapeShadeList = xmlDoc.SelectNodes("//a:outerShdw", nm);
                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(outputFile, System.Text.Encoding.UTF8);
                xmlDoc.Save(resultWriter);
                resultWriter.Close();

                string tmpFile = extractPath + "tmpID.xml";
                ChangeIDVal(outputFile, tmpFile);//
            }
            catch (Exception e)
            {
                logger.Error("Fail in OoxToUofPreProcessorOnePresentation: " + e.Message);
                logger.Error(e.StackTrace);
                isSuccess = false;
                throw new Exception("Fail in OoxToUofPreProcessorOnePresentation");
            }
            finally
            {
                if (resultWriter != null)
                {
                    resultWriter.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                if (txtReader != null)
                {
                    txtReader.Close();
                }
            }

            return(isSuccess);
        }
Example #2
0
        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);
        }