仿百度文档豆丁在线阅读文档

仿百度文档在线阅读文档

 

     最近在做一个在线培训的项目有一个功能需要在线阅服务器上的文档,自己想做一个类似百度问库一样的在线阅读器,找了很多资料最后找到一个很好决绝方案,很多牛人都在博客园里发表了些了一些教程,我按照教程自己做了一个测试能够确实很强大。写的很简单希望能够给能给还在为实现类似百度文库的同学有所帮助。

 四步走:

第一步:选择文件上传源文件到服务器

      注意:支持office 系列文件 图片文件 图片除了*.BPM 格式的都能够很好的转换

 

第二部:将上传的文件转换为PDF 文件

       注意:在转换为PDF 文件名称中不能够存在空格,存在空格会转换出错

        我用的方法是通过正则验证文件名称

          Regex reg = newRegex(@"\s");

                //取消空格后文件名称

                uploadFileName =reg.Replace(uploadFileName, "");

 

第三部:关键的一步将PDF 转换为SWF文件

第四步:将转换后的SWF 文件路径给要显示的控件

 

网站的基本结构

 网站根目录下准备三个文件夹

   UploadFiles:用于保存上传文件

   UploadFiles>OFFICE:上传的Office 系列文件

UploadFiles>PICTURE:上传的图片文件(再次提示:不支持*.BMP文件)

   UploadFiles>PDF:保存第二步转换的PDF 文件

   UploadFiles>SWF:保存第四部转后的SWF 文件

 

  JS:保存javascript文件

   文件夹内包含两个基本的js文件

   Jquery 文件

   Flexpape.js 文件

  

  Flexpape 文件

FlexPaperViewer.swf  ---界面控件插件

gif2swf.exe         ----gif转换器

jpeg2swf.exe        ----jpge转换器

pdf2swf.exe         ----Pdf转换器

playerProductInstall.swf

png2swf.exe         ---png转换器

 

 

Flexpape 资源下载地址

   支持word、excel、ppt转化为pdf,以及pdf转化为swf文件格式。同时要考虑关于图片文件处理

   该类的引用地址:http://www.2cto.com/kf/201301/186201.html

     pdf转化为swf地址:http://outofmemory.cn/code-snippet/1930/c-jiang-pdf-file-zhuanhuancheng-flash-swf

 

 创建一个界面

  Defaule.aspx界面

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

    <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>

    <script type="text/javascript" src="Scripts/flexpaper_flash.js"></script>

</head>

<body style="text-align: center;">

    <form id="form1" runat="server">

    <div>

        <asp:FileUpload ID="FileUpload1" runat="server" />

        <br />

        <br />

        <asp:Button ID="Button1" runat="server" Text="上传转化" OnClick="Button1_Click" />

    </div>

    <div>

        显示区域<br />

        <div style="text-align: center; width: auto; height: auto;

                <a id="viewerPlaceHolder" style="text-align: center; width: 660px; height: 480px; display: block"></a>

         </div>

            <script type="text/javascript">

                           varfp = new FlexPaperViewer(

                         'FlexPaper/FlexPaperViewer',

                         'viewerPlaceHolder',{ config: {

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

                             Scale: 0.6,

                             ZoomTransition: 'easeOut',

                             ZoomTime: 0.5,

                             ZoomInterval: 0.2,

                             FitPageOnLoad: true,

                             FitWidthOnLoad: false,

                             PrintEnabled: true,

                             FullScreenAsMaxWindow: false,

                             ProgressiveLoading: false,

                             MinZoomSize: 0.2,

                             MaxZoomSize: 5,

                             SearchMatchAll: false,

                             InitViewMode: 'Portrait',

 

                             ViewModeToolsVisible: true,

                             ZoomToolsVisible: true,

                             NavToolsVisible: true,

                             CursorToolsVisible: true,

                             SearchToolsVisible: true,

 

                             localeChain: 'en_US'

                         }

                         });

            </script>

        </div>

    </div>

    </form>

</body>

</html>

注意:escape($("#HiddenField1").val())控件绑定的swf文件路径

 

引用 office 组件

Microsoft.Office.Interop.Excel.dll

Microsoft.Office.Interop.PowerPoint.dll

Microsoft.Office.Interop.Word.dll

Office.dll

 

右键属性

修改嵌入操作类型改为false

 

 

 

ConvertUtility.cs

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using Microsoft.Office.Core;

using System.Web;

 

namespace Components

{

  /// <summary>

  /// 图片转换 office 文件转换

  /// </summary>

   public static  class ConvertUtility

    {

        /// <summary> 

        /// 把Word文件转换成为PDF格式文件 

        /// </summary> 

        /// <paramname="sourcePath">源文件路径</param> 

        /// <paramname="targetPath">目标文件路径</param>  

        /// <returns>true=转换成功</returns> 

        public static boolWordToPDF(string sourcePath, string targetPath)

        {

            boolresult = 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();

            }

            returnresult;

        }

 

        /// <summary> 

        /// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件 

        /// </summary> 

        /// <paramname="sourcePath">源文件路径</param> 

        /// <paramname="targetPath">目标文件路径</param>  

        /// <returns>true=转换成功</returns> 

        public static boolExcelToPDF(string sourcePath, string targetPath)

        {

            boolresult = false;

            Microsoft.Office.Interop.Excel.XlFixedFormatType targetType =Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;

            objectmissing = Type.Missing;

            Microsoft.Office.Interop.Excel.ApplicationClass application = null;

            Microsoft.Office.Interop.Excel.Workbook workBook = null;

            try

            {

                application = new Microsoft.Office.Interop.Excel.ApplicationClass();

                application.Visible = false;

                workBook =application.Workbooks.Open(sourcePath);

                workBook.SaveAs();

               workBook.ExportAsFixedFormat(targetType, targetPath);

                result = true;

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

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

            }

            returnresult;

        }

        /// <summary> 

        /// 把PowerPoint文件转换成PDF格式文件 

        /// </summary> 

        /// <paramname="sourcePath">源文件路径</param> 

        /// <paramname="targetPath">目标文件路径</param>  

        /// <returns>true=转换成功</returns> 

        public static boolPowerPointToPDF(string sourcePath, string targetPath)

        {

            boolresult;

           Microsoft.Office.Interop.PowerPoint.PpSaveAsFileTypetargetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;

            objectmissing = Type.Missing;

           Microsoft.Office.Interop.PowerPoint.ApplicationClassapplication = null;

           Microsoft.Office.Interop.PowerPoint.Presentationpersentation = null;

            try

            {

                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

                //application.Visible= MsoTriState.msoFalse; 

                persentation =application.Presentations.Open(sourcePath, MsoTriState.msoTrue,MsoTriState.msoFalse,MsoTriState.msoFalse);

                persentation.SaveAs(targetPath,targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

 

                result = true;

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

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

            }

            returnresult;

        }

        /// <summary>

        /// 把PDF文件转化为SWF文件

        /// </summary>

        /// <paramname="toolPah">pdf2swf工具路径</param>

        /// <paramname="sourcePath">源文件路径</param>

        /// <paramname="targetPath">目标文件路径</param>

        /// <returns>true=转化成功</returns>

        public static bool PDFToSWF(string toolPah, stringsourcePath, string targetPath)

        {

            Processpc = new Process();

            boolreturnValue = true;

 

            stringcmd = toolPah;

            stringargs = " -t " + sourcePath + " -s flashversion=9 -o " + targetPath;

            try

            {

                ProcessStartInfopsi = new ProcessStartInfo(cmd,args);

                psi.WindowStyle = ProcessWindowStyle.Hidden;

                pc.StartInfo = psi;

                pc.Start();

                pc.WaitForExit();

            }

            catch(Exception ex)

            {

                returnValue = false;

                thrownew Exception(ex.Message);

            }

            finally

            {

                pc.Close();

                pc.Dispose();

            }

            returnreturnValue;

        }

 

        /// <summary>

        /// png、jpg和jpeg文件的转化

        /// </summary>

        /// <paramname="toolPah"></param>

        /// <paramname="sourcePath"></param>

        /// <paramname="targetPath"></param>

        /// <returns></returns>

        public static boolPicturesToSwf(string toolPah, string sourcePath, stringtargetPath)

        {

            Processpc = new Process();

            boolreturnValue = true;

 

            stringcmd = toolPah;

            stringargs = " " + sourcePath + " -o " + targetPath + " -T 9";

            //如果是多个图片转化为swf 格式为 ..jpeg2swf.exe C:\1.jpg C:\2.jpg -oC:\swf1.swf

            try

            {

                ProcessStartInfopsi = new ProcessStartInfo(cmd,args);

                psi.WindowStyle = ProcessWindowStyle.Hidden;

                pc.StartInfo = psi;

                pc.Start();

                pc.WaitForExit();

            }

            catch(Exception ex)

            {

                returnValue = false;

                thrownew Exception(ex.Message);

            }

            finally

            {

                pc.Close();

                pc.Dispose();

            }

            returnreturnValue;

        }

        /// <summary>

        /// Gif文件转化为swf

        /// </summary>

        /// <paramname="toolPah"></param>

        /// <paramname="sourcePath"></param>

        /// <paramname="targetPath"></param>

        /// <returns></returns>

        public static boolGifPicturesToSwf(string toolPah, string sourcePath, stringtargetPath)

        {

            Processpc = new Process();

            boolreturnValue = true;

 

            stringcmd = toolPah;

            stringargs = " " + sourcePath + " -o " + targetPath;

            try

            {

                ProcessStartInfopsi = new ProcessStartInfo(cmd,args);

                psi.WindowStyle = ProcessWindowStyle.Hidden;

                pc.StartInfo = psi;

                pc.Start();

                pc.WaitForExit();

            }

            catch(Exception ex)

            {

                returnValue = false;

                thrownew Exception(ex.Message);

            }

            finally

            {

                pc.Close();

                pc.Dispose();

            }

            returnreturnValue;

        }

 

    }

 

 

 

 

}

 

 

 

 

 

Commons.cs 文件

 

  #region 转换路径

       /// <summary>

       /// pdf转换为wsf文件处理程序

       /// </summary>

        public static stringpdf2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/pdf2swf.exe");

        /// <summary>

        /// png 转换为wsf 文件处理程序

        /// </summary>

        public static stringpng2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/png2swf.exe");

        /// <summary>

        /// jpge 文件转换为swf文件处理程序

        /// </summary>

        public static stringjpeg2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/jpeg2swf.exe");

        /// <summary>

        /// gif转换为wsf文件处理程序

        /// </summary>

        public static stringgif2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/gif2swf.exe");

        #endregion

 

     #region 转换方法

        /// <summary>

        /// 将office文件转化为pdf文件,文件名称不变

        /// </summary>

        /// <paramname="pdf2swfPath">pdf2swfPath工具所在路径</param>

        /// <paramname="OfficePath">office存储路径</param>

        /// <paramname="OfficeName">office文件名称</param>

        /// <paramname="destPath">pdf存储路径</param>

        /// <returns>返回生成pdf的文件名,无效则返回空</returns>

        public static stringOfficeToPdf(string OfficePath, string OfficeName, stringdestPath)

        {

            stringfullPathName = OfficePath + OfficeName;//包含 路径 的全称

            stringfileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(OfficeName);//不包含路径,不包含扩展名

            stringextendName = System.IO.Path.GetExtension(OfficeName).ToLower();//文件扩展名

            stringsaveName = destPath + fileNameWithoutEx + ".pdf";

            stringreturnValue = fileNameWithoutEx + ".pdf";

 

            switch(extendName)

            {

                case".doc":

                    Components.ConvertUtility.WordToPDF(fullPathName, saveName);

                    break;

                case".docx":

                    Components.ConvertUtility.WordToPDF(fullPathName, saveName);

                    break;

                case".ppt":

                    Components.ConvertUtility.PowerPointToPDF(fullPathName,saveName);

                    break;

                case".pptx":

                    Components.ConvertUtility.PowerPointToPDF(fullPathName,saveName);

                    break;

                case".xls":

                    Components.ConvertUtility.ExcelToPDF(fullPathName, saveName);

                    break;

                case".xlsx":

                    Components.ConvertUtility.ExcelToPDF(fullPathName, saveName);

                    break;

                default:

                    returnValue = "";

                    break;

            }

            returnreturnValue;

        }

        /// <summary>

        /// 将pdf文件转化为swf文件,文件名称不变

        /// </summary>

        /// <paramname="pdf2swfPath">pdf2swfPath工具所在路径</param>

        /// <paramname="PdfPath">pdf存储路径</param>

        /// <paramname="PdfName">pdf文件名称</param>

        /// <paramname="destPath">swf存储路径</param>

        /// <returns></returns>

         publicstatic stringPdfToSwf(string pdf2swfPath, string PdfPath, stringPdfName, string destPath)

        {

            stringfullPathName = PdfPath + PdfName;//包含 路径 的全称

            stringfileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(PdfName);//不包含路径,不包含扩展名

            stringextendName = System.IO.Path.GetExtension(PdfName).ToLower();//文件扩展名

            stringsaveName = destPath + fileNameWithoutEx + ".swf";

            stringreturnValue = fileNameWithoutEx + ".swf";;

            if(extendName != ".pdf")

            {

                returnValue = "";

            }

            else

            {

                Components.ConvertUtility.PDFToSWF(pdf2swfPath, fullPathName,saveName);

            }

            returnreturnValue;

        }

        public static  string PictureToSwf(stringtoolpath, string PicturePath, string PictureName, stringdestPath)

        {

            stringfullPathName = PicturePath + PictureName;//包含 路径 的全称

            stringfileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(PictureName);//不包含路径,不包含扩展名

            stringextendName = System.IO.Path.GetExtension(PictureName).ToLower();//文件扩展名

            stringsaveName = destPath + fileNameWithoutEx + ".swf";

            stringreturnValue = fileNameWithoutEx + ".swf";

            if(extendName == ".gif")

            {

                Components.ConvertUtility.GifPicturesToSwf(toolpath,fullPathName, saveName);

            }

            else

            {

                Components.ConvertUtility.PicturesToSwf(toolpath,fullPathName, saveName);

            }

            returnreturnValue;

        }

 

        #endregion

 

 

 

 

 

Default.aspx.cs

 

protected void Page_Load(object sender, EventArgse)

        {

            if(!IsPostBack)

            {

                this.HiddenField1.Value= "UpLoadFiles/SWF/2组——Method.swf";

                HiddenField2.Value="~/UpLoadFiles/PDF/2组——Method.pdf";

            }

        }

 

 

  protected void Button1_Click(objectsender, EventArgs e)

        {

            ConvertFile(FileUpload1, null, null);

        }

 

 

 

  /// <summary>

        /// 转换方法

        /// </summary>

        /// <paramname="upfilePath">上传文件路径</param>

        /// <paramname="hiddenBIndPath">转换后sewf文件路径绑定控件</param>

        /// <paramname="downPath">绑定下载PDF路径</param>

        /// <returns></returns>

        privatestring ConvertFile(stringuploadFileName, HiddenField hiddenBIndPath, HiddenField downPath)

        {

            stringres = string.Empty;

 

            stringPictureFilePath = Server.MapPath("~/UpLoadFiles/PICTURE/");

            stringOfficeFilePath = Server.MapPath("~/UpLoadFiles/OFFICE/");

            stringPdfFilePath = Server.MapPath("~/UpLoadFiles/PDF/");

            stringSWFFilePath = Server.MapPath("~/UpLoadFiles/SWF/");

            stringSwfFileName = String.Empty;

 

            if(uploadFileName!="")

            {

                //获取上传文件名称、扩展名信息

                uploadFileName =FileUpload1.PostedFile.FileName;

                Regexreg = new Regex(@"\s");

                //取消空格后文件名称

                uploadFileName =reg.Replace(uploadFileName, "");

                stringUploadFileType = System.IO.Path.GetExtension(uploadFileName).ToLower();

                stringUploadFileNameFileFullName = String.Empty;

 

                //支持三种office文件到pdf,再到swf文件的转化

                if(UploadFileType == ".doc" ||UploadFileType == ".docx" ||UploadFileType == ".xls" ||UploadFileType ==".xlsx" ||UploadFileType == ".ppt" || UploadFileType== ".pptx")

                {

                    //根据文件类型决定其上传位置

                    UploadFileNameFileFullName= OfficeFilePath + uploadFileName;

                    //上传

                   FileUpload1.PostedFile.SaveAs(UploadFileNameFileFullName);

                    //转化为pdf

                    stringPdfFileName = OfficeToPdf(OfficeFilePath, uploadFileName, PdfFilePath);

                    downPath.Value = "~/UpLoadFiles/PDF/" +SwfFileName.Split('.')[0] + ".pdf";

                    if(PdfFileName == "")

                    {

                        res = "不支持的该Office文件类型到pdf的转化或者文件名存在空格";

                      

                    }

                    //转化为swf

                    SwfFileName =PdfToSwf(pdf2swfToolPath, PdfFilePath, PdfFileName, SWFFilePath);

                    if(SwfFileName == "")

                    {

                        res = "不支持的该文件类型到swf的转化或者文件名存在空格";

                       

                    }

                }

                elseif (UploadFileType == ".jpg"|| UploadFileType == ".jpeg" ||UploadFileType == ".png"||UploadFileType == ".gif")

                {

                    //根据文件类型决定其上传位置

                    UploadFileNameFileFullName= PictureFilePath + uploadFileName;

                    //上传

                   FileUpload1.PostedFile.SaveAs(UploadFileNameFileFullName);

                    //转化为swf

                    stringtoolpath = String.Empty;

                    downPath.Value = "~/UpLoadFiles/PICTURE/" +uploadFileName;

                    switch(UploadFileType)

                    {

                        case ".jpg":

                            toolpath =jpeg2swfToolPath;

                            break;

                        case ".jpeg":

                            toolpath =jpeg2swfToolPath;

                            break;

                        case ".png":

                            toolpath =png2swfToolPath;

                            break;

                        case ".gif":

                            toolpath =gif2swfToolPath;

                            break;

                        default:

                            break;

                    }

                    SwfFileName =PictureToSwf(toolpath, PictureFilePath, uploadFileName, SWFFilePath);

                }

                else

                {

                    res = "不支持的文件类型或者文件名存在空格->" + UploadFileType;

                  

                }

 

                res = "生成成功";

             

                //显示新生成的swf文件

                hiddenBIndPath.Value = "UpLoadFiles/SWF/" + SwfFileName;

             

            }

            else

            {

                res = "请选择上传文件";

            }

 

            returnres;

 

        }

转载于:https://www.cnblogs.com/imeiba/p/5696950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值