类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)

来源:互联网 发布:国外体育直播软件 编辑:程序博客网 时间:2024/04/30 02:04
在网上搜索如何和将office文档的pdf文档转换为flash,实现在线预览,你会发现有好几种解决方案

    a.使用flashpaper将需要的文档通过简单的设置转换为SWF格式的Flash,扩展阅读:http://baike.baidu.com/view/917746.htm,不过由于我的电脑室win7,而flashpaper又不支持win7,所以只好

    放弃此种方案。

    b.在网上发现可以使用swftools(http://www.swftools.org/谢天谢地啊,它支持win7)将pdf格式的文件转换为flash,但是不能讲office文档转换为flash,那怎么办呢,难道这种方法又不行,可是仔

    细想一下如果我们能将office文档先转换为pdf文件不就解决问题了吗,突然之间隐隐约约觉得使用office软件就可实现这一功能,于是打开word看一下究竟

    哈哈果然有,不过要先装一个插件                  

    一切准备工作结束以后我们就可以进行编码了

    c.首先说一下swftools使用:在命令行中运行pdf2swf src.pdf des.swf一般能满足需求。然后就行主要的代码的编写(即将office文档转换为pdf文档)

    编写Office2Pdf.cs类

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Word = Microsoft.Office.Interop.Word;using Excel = Microsoft.Office.Interop.Excel;using PowerPoint = Microsoft.Office.Interop.PowerPoint;using Microsoft.Office.Core;/// <summary>/// Office2Pdf 将Office文档转化为pdf/// </summary>public class Office2Pdf{    public Office2Pdf()    {        //// TODO: 在此处添加构造函数逻辑//    }    /// <summary>/// Word转换成pdf/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param>/// <returns>true=转换成功</returns>    public bool DOCConvertToPDF(string sourcePath, string targetPath)    {        bool result = false;        Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;        object paramMissing = Type.Missing;        Word.ApplicationClass wordApplication = new Word.ApplicationClass();        Word.Document wordDocument = null;        try        {            object paramSourceDocPath = sourcePath;            string paramExportFilePath = targetPath;            Word.WdExportFormat paramExportFormat = exportFormat;            bool paramOpenAfterExport = false;            Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;            int paramStartPage = 0;            int paramEndPage = 0;            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;            bool paramIncludeDocProps = true;            bool paramKeepIRM = true;            Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;            bool paramDocStructureTags = true;            bool paramBitmapMissingFonts = true;            bool paramUseISO19005_1 = false;            wordDocument = wordApplication.Documents.Open(                ref paramSourceDocPath, ref paramMissing, ref paramMissing,                ref paramMissing, ref paramMissing, ref paramMissing,                ref paramMissing, ref paramMissing, ref paramMissing,                ref paramMissing, ref paramMissing, ref paramMissing,                ref paramMissing, ref paramMissing, ref paramMissing,                ref paramMissing);            if (wordDocument != null)                wordDocument.ExportAsFixedFormat(paramExportFilePath,                    paramExportFormat, paramOpenAfterExport,                    paramExportOptimizeFor, paramExportRange, paramStartPage,                    paramEndPage, paramExportItem, paramIncludeDocProps,                    paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,                    paramBitmapMissingFonts, paramUseISO19005_1,                    ref paramMissing);            result = true;        }        catch        {            result = false;        }        finally        {            if (wordDocument != null)            {                wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);                wordDocument = null;            }            if (wordApplication != null)            {                wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);                wordApplication = null;            }            GC.Collect();            GC.WaitForPendingFinalizers();            GC.Collect();            GC.WaitForPendingFinalizers();        }        return result;    }    /// <summary>/// 把Excel文件转换成PDF格式文件  /// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param>/// <returns>true=转换成功</returns>    public bool XLSConvertToPDF(string sourcePath, string targetPath)    {        bool result = false;        Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;        object missing = Type.Missing;        Excel.ApplicationClass application = null;        Excel.Workbook workBook = null;        try        {            application = new Excel.ApplicationClass();            object target = targetPath;            object type = targetType;            workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,                missing, missing, missing, missing, missing, missing, missing, missing, missing);            workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);            result = true;        }        catch        {            result = false;        }        finally        {            if (workBook != null)            {                workBook.Close(true, missing, missing);                workBook = null;            }            if (application != null)            {                application.Quit();                application = null;            }            GC.Collect();            GC.WaitForPendingFinalizers();            GC.Collect();            GC.WaitForPendingFinalizers();        }        return result;    }    ///<summary>        /// 把PowerPoint文件转换成PDF格式文件       ///</summary>        ///<param name="sourcePath">源文件路径</param>     ///<param name="targetPath">目标文件路径</param> ///<returns>true=转换成功</returns>     public bool PPTConvertToPDF(string sourcePath, string targetPath)    {        bool result;        PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;        object missing = Type.Missing;        PowerPoint.ApplicationClass application = null;        PowerPoint.Presentation persentation = null;        try        {            application = new PowerPoint.ApplicationClass();            persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);            result = true;        }        catch        {            result = false;        }        finally        {            if (persentation != null)            {                persentation.Close();                persentation = null;            }            if (application != null)            {                application.Quit();                application = null;            }            GC.Collect();            GC.WaitForPendingFinalizers();            GC.Collect();            GC.WaitForPendingFinalizers();        }        return result;    }}


然后编写Pdf2Swf.cs类调用命令行将pdf转换为swf格式的flash

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Diagnostics;using System.IO;/// <summary>/// Pdf2Swf 将pdf转化为swf/// </summary>public class Pdf2Swf{    public Pdf2Swf()    {        //// TODO: 在此处添加构造函数逻辑//    }    public void PDFConvertToSWF(string sourcePath, string targetPath)    {        Process p = new Process();        p.StartInfo.FileName = "cmd.exe ";        p.StartInfo.UseShellExecute = false;        p.StartInfo.RedirectStandardInput = true;        p.StartInfo.RedirectStandardOutput = true;        p.StartInfo.RedirectStandardError = true;        p.StartInfo.CreateNoWindow = true;        p.Start();        string cmd = "pdf2swf.exe" + " " + sourcePath + " -o " + targetPath;        p.StandardInput.WriteLine(cmd);        p.Close();    }}


然后只需要调用这两个类就可以实现将office转换为pdf再转换为flash的功能了

 (3)最后就是实现flash的显示:这时我们还要借助工具来实现功能,那就是flexpaper,首先在网上下载所需要的flash文件、javascript文件,然后编写html代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0014)about:internet --> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">        <head>         <title>中勤文库-Flash版</title>                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         <style type="text/css" media="screen">             html, body    { height:100%; }            body { margin:0; padding:0; overflow:auto; }               #flashContent { display:none; }</style>                 <script type="text/javascript" src="js/swfobject/swfobject.js"></script>        <script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>        <script type="text/javascript" src="js/jquery.js"></script>        <script>//获得参数的方法var request =        {        QueryString : function(val)        {var uri = window.location.search;var re = new RegExp("" +val+ "=([^&?]*)", "ig");return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);        }        }</script>         <script type="text/javascript"> <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> var swfVersionStr = "10.0.0";<!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->var xiSwfUrlStr = "playerProductInstall.swf";var swfFile = emotion;//这填写文档转换成的flash文件的路径            var flashvars = {                   SwfFile : escape(swfFile),                  Scale : 0.6,                   ZoomTransition : "easeOut",                  ZoomTime : 0.5,                    ZoomInterval : 0.2,                    FitPageOnLoad : true,                    FitWidthOnLoad : false,                    PrintEnabled : true,                    FullScreenAsMaxWindow : false,                    ProgressiveLoading : true,                                        PrintToolsVisible : true,                    ViewModeToolsVisible : true,                    ZoomToolsVisible : true,                    FullScreenVisible : true,                    NavToolsVisible : true,                    CursorToolsVisible : true,                    SearchToolsVisible : true,                                        localeChain: "en_US"                  };                  var params = {                                }            params.quality = "high";            params.bgcolor = "#ffffff";            params.allowscriptaccess = "sameDomain";            params.allowfullscreen = "true";var attributes = {};            attributes.id = "FlexPaperViewer";            attributes.name = "FlexPaperViewer";            swfobject.embedSWF("FlexPaperViewer.swf", "flashContent", "730", "580",                swfVersionStr, xiSwfUrlStr,                 flashvars, params, attributes);            swfobject.createCSS("#flashContent", "display:block;text-align:left;");            </script>            </head>     <body>         <div align="center">            <div id="flashContent">                 <p>                     To view this page ensure that Adobe Flash Player version                     10.0.0 or greater is installed.                 </p>                 <script type="text/javascript"> var pageHost = ((document.location.protocol == "https:") ? "https://" :    "http://");                     document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" );                     </script>             </div>        </div>           </body> </html>


效果就这样:

还不错吧,上面的工具栏也可以实现各种功能

至此为止,所有工作全部结束,大家只需要将这些代码进行整合,配合数据库使用,编写管理后台,就可以实现百度文库的效果,甚至可以投入实际使用。

源码下载:http://files.cnblogs.com/expectszc/Web.zip

原创粉丝点击