doc转成pdf,再转成swf

2015-10-25 09:31:47  访问(1518) 赞(0) 踩(0)


        /// <summary>
        /// doc转成pdf,再转成swf
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_生成_Click(object sender, EventArgs e)
        {

            string fileName = comboBox_文件.Text.Trim();

            if (fileName.Length == 0)
            {
                MessageBox.Show("请选择文件。");
                comboBox_文件.Select();
                comboBox_文件.Focus();
                return;
            }



            FileInfo upFile = new FileInfo(fileName);
            FileInfo pdfFile = null;

            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;

            if (pdfFile == null)
            {
                MessageBox.Show("请选择有效的文件。");
                comboBox_文件.Select();
                comboBox_文件.Focus();
                return;
            }

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

            pdfToSwfClick(pdfFile.FullName, 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>
        /// 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);
        }


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



                //切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败 
                string cmdStr = comboBox_pdf2swf.Text.Trim();

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

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

                ExcutedCmd(cmdStr, argsStr);

                MessageBoxShowFileNameYesNoCancel(swfFullName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 保存成功后的提示操作
        /// </summary>
        /// <param name="fileName"></param>
        public void MessageBoxShowFileNameYesNoCancel
            (
                string fileName
            )
        {
            DialogResult dg = MessageBox.Show
                (
                       "保存成功,是否复制文件或打开文件(点No)?",
                       "选择操作",
                       MessageBoxButtons.YesNoCancel
                );

            if (dg == DialogResult.Cancel)
                return;

            if (dg == DialogResult.No)
            {
                System.Diagnostics.Process.Start("explorer.exe", fileName);

                return;
            }

            if (dg == DialogResult.Yes)
            {

                System.Collections.Specialized.StringCollection stringCollectionValue
                    = new System.Collections.Specialized.StringCollection();

                stringCollectionValue.Add(fileName);

                Clipboard.SetFileDropList(stringCollectionValue);
            }
        }


        /// <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();
            }
        }


标签:doc转成pdf,再转成swf 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

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