百度文库效果实现
最近项目中需要实现在线文档预览,和百度文库的效果类似。网上找了好久,奈何始终融合不到项目中,逼于无奈,参考网上大神的方式,自己实现了。
废话不说,上代码,留下以便后来需要。
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GenateSWF.aspx.cs" Inherits="PDFViewer.GenateSWF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style=" margin:50px auto; border:#e3e3e3 1px solid; height:350px; width:600px; padding-top:50px;">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
          
        <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" />
        <br />
        <br />
        点击下面列表框中的文件名,查看:
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Height="205px" Width="290px">
        </asp:ListBox>
        <asp:Button ID="Button2" runat="server" Text="查看" onclick="Button2_Click1" />
        <br />
        <br />
    
    </div>
    </form>
</body>
</html>
后台CS:
 protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    string fileName = FileUpload1.FileName;
                    string fileExtention = FileUpload1.FileName.Substring(fileName.LastIndexOf(".") + 1);
                    string filePath = HttpContext.Current.Server.MapPath("~/TestSWF/") + fileName;
                    if (!(Directory.Exists("~/TestSWF/")))
                    {
                        CreateFolder("~/TestSWF/");
                    }
                    FileUpload1.SaveAs(filePath);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传成功')</script>");
                    //切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败
                    string cmdStr = HttpContext.Current.Server.MapPath("~/SWFTools/pdf2swf.exe");
                    string savePath = HttpContext.Current.Server.MapPath("~/TestSWF/");
                    string sourcePath = @"" + savePath + fileName + @"";
                    string pdfPath = @"""" + savePath + fileName.Substring(0, fileName.LastIndexOf(".")) + ".pdf" + @"""";
                    int r = ConvertToPDF(sourcePath);
                    if (r == 0)
                    {
                        string targetPath = @"""" + savePath + fileName.Substring(0, fileName.LastIndexOf(".")) + ".swf" + @"""";
                        //@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功
                        // -t 源文件的路径
                        // -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略))
                        string argsStr = "  -t " + pdfPath + " -s flashversion=9 -o " + targetPath;
                        ExcutedCmd(cmdStr, argsStr);
                        ListBox1.Items.Add(new ListItem(fileName.Substring(0, fileName.LastIndexOf(".")) + ".swf", fileName.Substring(0, fileName.LastIndexOf(".")) + ".swf"));
                    }
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('转换出错')</script>");
                }
            }
        }
 /// <summary>
        /// 创建一个文件夹
        /// </summary>
        /// <param name="Fname">文件夹路径及名称</param>
        /// <returns></returns>
        public static bool CreateFolder(string Fname)
        {
            if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(Fname)))
            {
                return false;
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(Fname));
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        private static void ExcutedCmd(string cmd, string args)
        {
            using (Process p = new Process())
            {
                ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                p.StartInfo = psi;
                p.Start();
                p.WaitForExit();
            }
        }
        protected void Button2_Click1(object sender, EventArgs e)
        {
            Response.Redirect("~/Viewer.aspx?id=" + HttpUtility.HtmlEncode(ListBox1.SelectedValue));
        }
        //Office转换成pdf
        ///<summary>
        /// 把Office文件转换成为PDF格式文件
        ///</summary>
        ///<param name="sourcePath">源文件路径</param>
        ///<param name="targetPath">目标文件路径</param> 
        ///<returns>true=转换成功</returns>
        protected int ConvertToPDF(string sourcePath)
        {
            SautinSoft.UseOffice u = new SautinSoft.UseOffice();
            SautinSoft.UseOffice.eDirection direction = SautinSoft.UseOffice.eDirection.DOC_to_PDF;
            string ext = Path.GetExtension(sourcePath).ToLower();
            if (ext.IndexOf("doc") > 0)
                direction = SautinSoft.UseOffice.eDirection.DOC_to_PDF;
            else if (ext.IndexOf("rtf") > 0)
                direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF;
            else if (ext.IndexOf("txt") > 0)
                direction = SautinSoft.UseOffice.eDirection.RTF_to_PDF;
            else if (ext.IndexOf("xls") > 0)
                direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF;
            else if (ext.IndexOf("csv") > 0)
                direction = SautinSoft.UseOffice.eDirection.XLS_to_PDF;
            else if (ext.IndexOf("ppt") > 0)
                direction = SautinSoft.UseOffice.eDirection.PPT_to_PDF;
            //2. Convert to PDF      
            string outPutFile = Path.Combine(Path.GetDirectoryName(sourcePath), Path.GetFileNameWithoutExtension(sourcePath));
            outPutFile += ".pdf";
            int mes_return = u.ConvertFile(sourcePath, outPutFile, direction);
            u.CloseOffice();
            return mes_return;
        }
在项目中,需要引用pdf2swf.exe,这个网上很多。另外,在转换文档是,目前用过三种方式,一种微软原生的DLL,在引用中添加com引用,找到Microsoft Office 12.0 Object Library。这个是word文档转换的,其他的类似。一种是引用第三方DLL,aspose.word.dll,类似的还有aspose.excel.dll,Aspose.Slides.dll.第三种是useOffice。
对比了一下,我采用的第三种方式,转换后的文件比较逼真,第二种因为是免费的,有水印,去除水印需要购买付费的(个人觉得)。
                    
                
                
            
        
浙公网安备 33010602011771号