zoukankan      html  css  js  c++  java
  • office文件的预览

    需求很简单,用户上传office文件(word、excel、ppt)后,可以预览上传的这些文件。搜索的相关的资料后。整理如下:

    Step1.用户上传office文件。

    Step2.把Office文件转化为pdf文件

    Step3.把pdf文件转化为swf文件

    Step4.使用flexpaper插件预览swf文件

    根据这四步,我们逐步分析:

    Step1.上传文件,在此不做赘述。

    Step2.把Office文件转化为pdf文件。

        必须保证你的office版本在2007之上。我第一次使用office2003,不报错,但是也没用生成相关的pdf文件。果断使用了最新的office2013即可完美运行。

        在次贴出将word转化为pdf文件的代码,完整实例,请看附件。

       

    复制代码
            /// <summary>  
            /// 把Word文件转换成为PDF格式文件  
            /// </summary>  
            /// <param name="sourcePath">源文件路径</param>  
            /// <param name="targetPath">目标文件路径</param>   
            /// <returns>true=转换成功</returns>  
            public static bool WordToPDF(string sourcePath, string targetPath)
            {
                bool result = false;
                Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
                Microsoft.Office.Interop.Word.ApplicationClass application = null;
    
                Microsoft.Office.Interop.Word.Document document = null;
                try
                {
                    application = new Microsoft.Office.Interop.Word.ApplicationClass();
                    application.Visible = false;
                    document = application.Documents.Open(sourcePath);
                    document.SaveAs();
                    document.ExportAsFixedFormat(targetPath, exportFormat);
                    result = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    result = false;
                }
                finally
                {
                    if (document != null)
                    {
                        document.Close();
                        document = null;
                    }
                    if (application != null)
                    {
                        application.Quit();
                        application = null;
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                return result;
            }
    复制代码

        (考虑到有些和我一样的新手,在此赘述几句吧:如果你使用vs2010开发,在添加office引用的时候,直接选择.net栏目下的office、Microsoft.Office.Interop.Word,Microsoft.Office.Interop.PowerPoint和Microsoft.Office.Interop.Excel,并且保证office的版本高于后三者就行啦。不用再在com栏目下引入相关office的com组件)

    Step3.把pdf文件转化为swf文件。

        首先,我们可以下载swftools,完成安装后会在安装目录下面有多个小工具。选择pdf2swf.exe 拷贝到我们的项目相关目录下面,使用如下代码,便可完成调用。

    复制代码
            /// <summary>
            /// 把PDF文件转化为SWF文件
            /// </summary>
            /// <param name="toolPah">pdf2swf工具路径</param>
            /// <param name="sourcePath">源文件路径</param>
            /// <param name="targetPath">目标文件路径</param>
            /// <returns>true=转化成功</returns>
            public static bool PDFToSWF(string toolPah, string sourcePath, string targetPath)
            {
                Process pc = new Process();
                bool returnValue = true;
    
                string cmd = toolPah;
                string args = " -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    pc.StartInfo = psi;
                    pc.Start();
                    pc.WaitForExit();
                }
                catch (Exception ex)
                {
                    returnValue = false;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    pc.Close();
                    pc.Dispose();
                }
                return returnValue;
            }
    复制代码

         此处也有两处要重点提醒。首先呢,就是所有的文件路径都不能有空格,这是因为pdf2swf对含有空格的文件路径不支持。其次,就是flashversion=9这个关键的命名也要加上,否则会出现在flexpaper无法预览该swf文档的情况。

    Step4.使用flexpaper插件预览swf文件

        刚开始我们可以下载一个官方的用例,选择-》download web server package。然后直接打开index.html发现不能预览docs中的Paper.pdf.swf这个swf文件。于是乎网上出现了n多关于通过通过将adobe flash player将flexpaper添加信任啥的,这个哥试了,但是发现不行!有种被坑的感觉!最后我的解决方案是:直接将这个官网的例子放在IIS服务下面就行了。或者你通过vs2010建立项目的时候把这个插件添加进去(就是把官网的那些代码粘过去,我这样说你明白吧,但是注意一下路径问题)也是可以的。

        有些童鞋该说了,我想把swf文件动态加载,也就是FlexPaperViewer中的SwfFile动态加载。so easy。

        比如一个http://localhost:12345/mypage.aspx?swf=123.swf这样的请求,

        我们在CS端通过request.QueryString["swf"]获取123.swf文件之后可以将其值赋给mypage.aspx页面的HiddenField1,然后在

        mypage.aspx中使用

    SwfFile: escape($("#HiddenField1").val()),

        即可动态获取swf文件。

        最后贴出最终的显示结果图:

       

    图1.初始页面

    图2.选择office文件


    图3.上传后预览

    ================================正常人类分割线=================================

      【客户需求】做完了之后,我闲着蛋疼想着如果用户要上传图片格式的文件,我们要预览图片的话呢?

    很简单,用jQuery图片的相关插件就能美好的实现了。

        当然用swftools也能将图片转化为swf,和这个页面集成到一起去。

        swftools中支持jpg、jpeg、gif和png,不支持bmp文件格式。

    为了能让转化成的swf能在flexpaper中能够正确的显示,这几种图片的swftool命令也不一样,直接贴代码了哈

    复制代码
    /// <summary>
            /// png、jpg和jpeg文件的转化
            /// </summary>
            /// <param name="toolPah"></param>
            /// <param name="sourcePath"></param>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public static bool PicturesToSwf(string toolPah, string sourcePath, string targetPath)
            {
                Process pc = new Process();
                bool returnValue = true;
    
                string cmd = toolPah;
                string args = " " + sourcePath + " -o " + targetPath+" -T 9";
                //如果是多个图片转化为swf 格式为 ..jpeg2swf.exe C:1.jpg C:2.jpg -o C:swf1.swf
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    pc.StartInfo = psi;
                    pc.Start();
                    pc.WaitForExit();
                }
                catch (Exception ex)
                {
                    returnValue = false;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    pc.Close();
                    pc.Dispose();
                }
                return returnValue;
            }
            /// <summary>
            /// Gif文件转化为swf
            /// </summary>
            /// <param name="toolPah"></param>
            /// <param name="sourcePath"></param>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public static bool GifPicturesToSwf(string toolPah, string sourcePath, string targetPath)
            {
                Process pc = new Process();
                bool returnValue = true;
    
                string cmd = toolPah;
                string args = " " + sourcePath + " -o " + targetPath;
                try
                {
                    ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    pc.StartInfo = psi;
                    pc.Start();
                    pc.WaitForExit();
                }
                catch (Exception ex)
                {
                    returnValue = false;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    pc.Close();
                    pc.Dispose();
                }
                return returnValue;
            }
    复制代码

        这几个命令虽然网上有,但是很多都是有问题的,有的在flexpaper当中不能用。本人的这个是亲测可用的,欢迎拍砖!

     

    PostScript:

       最后附上本人的项目附件,第一次这么仔细的写博客,大家多多关照。 

       附件下载

  • 相关阅读:
    HTTP 常见状态码
    SpringMVC 入门
    Maven 整合SSH框架
    SSH 框架整合总结
    Maven 整合SSH框架之pom.xml
    Maven 入门
    WebService 综合案例
    CXF 框架
    jQuery高级
    JavaScript补充:BOM(浏览器对象模型)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3380295.html
Copyright © 2011-2022 走看看