zoukankan      html  css  js  c++  java
  • asp.net 实现pdf、swf等文档的浏览

    一、pdf的浏览

    可以借助于pdf.js插件完成,使用pdf.js的好处是不需要安装额外的插件(比如flash),是纯web的解决方案。插件的下载链接:http://mozilla.github.io/pdf.js/。

    通过pdf.js里面的示例代码可以看出,实际上浏览pdf文档是通过viewer.html实现的。在链接后面通过传入file参数,实现不同文档的浏览。比如如下的链接:

      <a href="#" onclick="show('../../Js/pdf.js/web/viewer.html?file=/damis/PDFFiles/{SFZH}.pdf')">pdf</a>
    

    其中show方法是我定义的一个js方法,用于弹出窗口,定义如下:

    <script type="text/javascript">
        function show(openUrl) {
            var iWidth = 820; //弹出窗口的宽度;
            var iHeight = 680; //弹出窗口的高度;
            var iTop = (window.screen.availHeight - 30 - iHeight) / 2; //获得窗口的垂直位置;
            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; //获得窗口的水平位置;
            window.open(openUrl, "", "height=" + iHeight + ", width=" + iWidth + ", top=" + iTop + ", left=" + iLeft + ",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no,alwaysRaised=yes,depended=yes");
        }
    </script>
    

    {SFZH}是EXT.NET里面传入参数的方式,不是重点。

    效果图:

    二、swf的浏览

    有时候为了追求更好的浏览体验,可能需要借助于flash。有现成的flexpaper可以使用。

    flexpaper只接收swf文件,所有在使用之前必须准备好swf文件。由于我们如上已经通过itextsharp生成了pdf文档,所以只需要将pdf转换为swf即可。这个功能我们可以使用pdf2swf.exe来完成,可以去http://www.swftools.org/下载。

    实现pdf到swf的转换代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            string sfzh = Request.QueryString["sfzh"] as string;
            string exe = Server.MapPath("~/Base/pdf2swf.exe");
            string source = CommonUtil.GetLocalPathByIdentitycard(sfzh) + sfzh + ".pdf";
            string dest = Path.Combine(Server.MapPath("~"), "Swf\" + sfzh + ".swf");
    
            if (!File.Exists(dest))
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = exe;
                process.StartInfo.Arguments = source + " -o " + dest + " -T 9 -f";
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.Start();
                process.WaitForExit();
                process.Close();
            }
    
            string swfFile = "../../Swf/" + sfzh + ".swf";
            this.Literal1.Text = "var swfFile = '" + swfFile + " '";
        }
    }
    

    这里是通过Process进程实现转换。

    前台代码:

    <a href="#" onclick="show('DASwfView.aspx?sfzh={SFZH}')">swf</a>
    

    show方法跟如上pdf弹出文档的js方法是一样的。

    弹出的DASwfView.aspx页面中的html代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DASwfView.aspx.cs" Inherits="DAMIS.Web.Web.DAQueryStatistics.DASwfView" %>
    
    <!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>
        <script src="../../Js/swfobject/swfobject.js" type="text/javascript"></script>
        <script src="../../Js/flexpaper_flash_debug.js" type="text/javascript"></script>
        <script src="../../Js/jquery-1.4.2.js" type="text/javascript"></script>
        <script type="text/javascript">  
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </script>
        <script src="../../Js/DocumentView.js" type="text/javascript"></script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="position: absolute; left: 3px; top: 3px;" 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>
        </form>
    </body>
    </html>
    

    如上需要引用的几个js文件可以下载到。DocumentView.js这个文件中有一段代码需要注意:

    swfobject.embedSWF(
                    "../../Swf/DocumentViewer.swf", "flashContent",
                    "800", "675", "10.0.0", "playerProductInstall.swf",
                    flashvars, params, attributes);
    

    这里的DocumentViewer.swf文件借用了Insus的InsusDocumentViewer.swf文件(改了个名)。而这部分实现也是参考了Insus的解决方案,具体的可以参考:http://www.cnblogs.com/insus/p/3574298.html这篇博文。

    如下的代码就实现在线将pdf转换为swf并显示的功能,效果图如下:

  • 相关阅读:
    hiho 1483 区间计数问题+二分答案
    Educational Codeforces Round 17 D dp
    Educational Codeforces Round 17 C 二分
    hiho 1461 暴力+随机
    (转)重置Mac OS X管理员密码
    解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题
    SVN权限修复
    MAC安装SVNServer
    设置UIScrollView只可以水平或者竖直滚动
    (转)android之Fragment(官网资料翻译)
  • 原文地址:https://www.cnblogs.com/guwei4037/p/6687754.html
Copyright © 2011-2022 走看看