Doc转swf的片段代码

2015-11-03 18:48:48  访问(2730) 赞(0) 踩(0)


        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected string ToSwf(FileInfo info)
        {
            if (info.Extension == null)
                return null;

            string strExtension = info.Extension.Trim().ToLower();

            if (strExtension == ".doc" || strExtension == ".docx")
            {
                return DocToSwf(info);
            }
            else if (strExtension == ".pdf")
            {

                string swfFile = FileChangeExtension(info.FullName, ".swf");

                pdfToSwfClick(info.FullName, swfFile);

                return swfFile;

            }
            else
            {
                throw new Exception(info.FullName + "暂无法转成swf");
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected string DocToSwf(FileInfo upFile)
        {
            FileInfo pdfFile = null;

            string fileName = upFile.FullName;
            string lowerFileName = fileName.ToLower();

            if (lowerFileName.EndsWith(".doc") || lowerFileName.EndsWith(".docx"))
            {

                string onlyName = upFile.Name;

                int idx = onlyName.LastIndexOf('.');

                if (idx != -1)
                    onlyName = onlyName.Substring(0, idx);

                pdfFile = new FileInfo(upFile.Directory.FullName + "\\" + onlyName + ".pdf");

                ToPdf(fileName, pdfFile.FullName);
            }
            else if (lowerFileName.EndsWith(".pdf"))
            {
                pdfFile = upFile;
            }
 
            string swfFile = FileChangeExtension(pdfFile.FullName, ".swf");

            pdfToSwfClick(pdfFile.FullName, swfFile);

            return swfFile;

        }

        /// <summary>
        /// <para>文件更改后缀</para>
        /// <para>比如:C:\\one\\test.doc 更改成 C:\\one\\test.pdf</para>
        /// </summary>
        /// <param name="fileName">文件名,如C:\\one\\test.doc</param>
        /// <param name="newExtension">后缀名,带.号,如.pdf</param>
        /// <returns></returns>
        public string FileChangeExtension(string fileName, string newExtension)
        {
            if (fileName == null || fileName.Length == 0)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "fileName == null || fileName.Length == 0"
                    );
            }

            int idx = fileName.LastIndexOf('.');

            if (idx == -1)
                return fileName + newExtension;
            else
                return fileName.Substring(0, idx) + newExtension;
        }



        /// <summary>
        /// pdf转成swf
        /// </summary>
        /// <param name="pdfFullName"></param>
        /// <param name="swfFullName"></param>
        protected void pdfToSwfClick(string pdfFullName, string swfFullName)
        {
            try
            {



                //切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败 
                string cmdStr = Application.StartupPath + "\\pdf2swf.exe";

                string sourcePath = @"""" + pdfFullName + @"""";
                string targetPath = @"""" + swfFullName + @"""";

                //@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功 
                // -t 源文件的路径 
                // -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略)) 
                string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;

                ExcutedCmd(cmdStr, argsStr); 

                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="args"></param>
        private static void ExcutedCmd(string cmd, string args)
        {
            using (Process p = new Process())
            {
                ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo = psi;
                p.Start();

                p.WaitForExit();
            }
        }

        /// <summary>
        /// Word转成Pdf
        /// </summary>
        /// <param name="wordFile"></param>
        /// <param name="pdfFile"></param>
        public void ToPdf(string wordFile, string pdfFile)
        {
            // 需要 using Aspose.Words; //

            if (!File.Exists(wordFile))
            {
                throw new Exception(wordFile + "文件不存在。");
            }

            FileInfo pdfInfo = new FileInfo(pdfFile);

            if (pdfInfo.Exists)
                pdfInfo.Delete();

            if (!pdfInfo.Directory.Exists)
                Directory.CreateDirectory(pdfInfo.Directory.FullName);

            FileInfo wordInfo = new FileInfo(wordFile);

            Document doc = new Document(wordInfo.FullName);

            doc.Save(pdfInfo.FullName);
        }


标签:Doc转swf的片段代码 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)