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并显示的功能,效果图如下:
相关文章
- .NET 同步与异步之锁(Lock、Monitor)(七)
- 简单的Asp.net core管道模拟
- Sandcastle方法生成c#.net帮助类帮助文档chm
- ASP.NET Core Module overview模块概述
- .NET开源Protobuf-net组件葵花手册
- mvc.net分页查询案例——控制器(HomeController.cs)
- You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
- [Wap]自定义asp.net mobile control
- .NET(C#) Internals: as a developer, .net framework in my eyes
- ASP.NET Core 中间件详解及项目实战
- 集成Swagger接口文档分组配置.net core
- 重新整理 .net core 实践篇——— UseEndpoints中间件[四十八]
- 重新整理 .net core 实践篇————polly失败重试[三十四]
- 集成Swagger接口文档分组配置.net core
- 基于chrome内核的.NET开发资源
- 新作《ASP.NET Web API 2框架揭秘》正式出版
- Atitit ..Net Framework sdk 3.0 3.5 4.04.5 4.6 4.7 .net core版本新特性 v2 s22 1. 新特性来源于 down ms 官方网站1
- RasterEdge Document:文档成像 SDK .NET
- ASP.NET MVC - 参考手册
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- asp.net mvc5 安装
- 行人重识别0-11:DG-Net(ReID)-代码无死角解读(7)-全局loss讲解-注释篇
- 英文拼写检查:TX Spell .NET for .NET 10.0 Crack
- TVideoGrabber SDK 15.2.4.6 for .NET/ACTIVEX/OCX
- ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
- 【.Net实用方法总结】 整理并总结.NET 中的 System.IO.Pipelines(管道)