您的位置:首页 > 编程语言 > ASP

ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)

2015-05-18 14:39 387 查看
之前写过一次ASP.NET在线浏览Word文档的文章,里面主要通过调用office word组件把word文档转换成html文档,然后间接实现浏览文档。

自己来测试的时候,本地调试基本没什么问题,发布到IIS后出现调用组件失败等等问题,后来百度谷歌了很久终于找到解决方案,问题居然是因为权限不够,需要在web.config里面给当前网站加入管理员权限才允许调用office组件。看到这个我也是醉了.....

直接把服务器管理员账号密码甩web.config总觉得不安心,遂放弃。继续寻找各种解决方案,最后通过第三方dll完成文件转换过程。

预备工作:ASP.NET 通过jquery.media.js实现在线浏览PDF文档。

前台代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery.media.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(function ($) {
            $('#mo2g').click();//模拟自动点击a标签
        });
        window.onload = jQuery();
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <a runat="server" id="PDF" class="media"><span id="mo2g">PDF</span></a>
    </div>

   <div style="margin:0 auto; width:450px;">
   <asp:Label ID="lblMsg" runat="server" Text="您使用的浏览器不支持PPT浏览,推荐使用火狐浏览器、Google Chrome浏览器等现代浏览器。" ForeColor="Red"></asp:Label>
   <br />
   <a href="BrowserDown/Chrome.exe">谷歌浏览器下载</a>
   <a href="BrowserDown/Firefox.exe">火狐浏览器下载</a>
   <br />
   <br />
   </div>
    </form>
</body>
</html>

说明:

引用jquery-1.4.1.js和jquery.media.js文件(文章最后有),然后通过后台给a标签赋值(PDF文档URL),jquery模拟点击a标签实现自动跳转,完成加载PDF显示效果。

后台代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                string relativePath = Request.QueryString["FilePath"]; //相对路径
                if (relativePath == "" || relativePath == null) return;
                string browsertype = Page.Request.Browser.Type;
                if (browsertype != "IE6" && browsertype!="IE7")
                {
                    PDF.HRef = relativePath;
                }
               
            }
           

        }
说明:FilePath 文件相对路径,PDF a标签ID。

如此实现了自动加载PDF文档。

效果如下:



接着现在要解决的问题就是,如何把word文档转换成PDF文档?

强大的第三方dll要出场了。

这里,我使用的是Aspose.Word.dll,收费组件。

(正规商用请自行购买商业版,我们这种闹着玩一下的话,直接上破解版了。)

不扯太多,直接贴后台代码:

引用说明:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;
using System.IO;
using System.Text;
protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string relativePath = Request.QueryString["FilePath"]; //相对路径,从url获取
                if (relativePath == "" || relativePath == null)
                    return;
                string serverPath = Server.MapPath(relativePath);
                string pdfPath = serverPath.Replace(".doc", ".pdf");
                if (!File.Exists(@pdfPath))//存在PDF,不需要继续转换
                {
                    if (!CreatePDF(serverPath, pdfPath)) //函数在底下
                    {
                        return;
                    }
                    
                }
                relativePath = relativePath.Replace(".doc", ".pdf");
                relativePath = relativePath.Remove(0, 2);
                string browsertype = Page.Request.Browser.Type;//浏览器类型判断
                if (browsertype != "IE6" && browsertype != "IE7")
                {
                    PDF.HRef = relativePath;
                }
               

            }

           // Aspose.Words.Document doc = new Aspose.Words.Document(被转换文件的路径+名字); 
            //doc.Save(保存pdf的路径+名字, SaveFormat.Pdf);
        }
  public bool CreatePDF(string soursePath, string savePath)
        {

            try
            {
                Document doc = new Document(soursePath);
                //保存PDF文件
                doc.Save(savePath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            } 
        }

下载链接:http://pan.baidu.com/s/1kTosEnt

注:CSDN里面很多Aspose.Word下载,有一些版本的Aspose.Word 由Word转成PDF的话中文会发生乱码,链接里面的实测完美转换。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: