web页面实现flash播放pdf,word文档等(asp.net c#+flash Paper+SWFTools)

来源:互联网 发布:excel03版数据分析表 编辑:程序博客网 时间:2024/05/23 17:53

因最近一个工作项目的需要,也做了一个像百度文库那种在web页的flash中能播放pdf文档、word文档,其实原理比较简单。
只在上传时还是在启动播放时,把自动pdf转换为swf,然后调用flash Paper播放就可。
要用到的工具
1、 SWFTools  ,安装完成后通过WEB编程调用其实现对文档的转换。
2、 flash pager 一个swf播放插件,集成有缩小放大,拖动,全屏等功能,不一定要用这个, 也可以自己用AS写一个。

效果如下:

代码如下:
asp.net c#

view source
print?
01public string execStr = "";
02        public static bool PDF2SWF(string pdfPath, string swfPath, int page)
03        {
04            string exe = HttpContext.Current.Server.MapPath("~/Bin/pdf2swf.exe");
05            pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
06            swfPath = HttpContext.Current.Server.MapPath(swfPath);
07            if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
08            {
09                return false;
10            }
11            StringBuilder sb = new StringBuilder();
12            sb.Append(" /"" + pdfPath + "/"");//input
13            sb.Append(" -o /"" + swfPath + "/"");//output
14            //sb.Append(" -z");
15            sb.Append(" -s flashversion=9");//flash version
16            //sb.Append(" -s disablelinks");//禁止PDF里面的链接
17            sb.Append(" -p " + "/"1" + "-" + page + "/"");//page range
18            sb.Append(" -j 100");//SWF中的图片质量
19            string Command = sb.ToString();
20            System.Diagnostics.Process p = new System.Diagnostics.Process();
21  
22            p.StartInfo.FileName = exe;
23            p.StartInfo.Arguments = Command;
24            p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
25            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序 启动 线程
26            //p.StartInfo.RedirectStandardInput = true;
27            //p.StartInfo.RedirectStandardOutput = true;
28            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,pdf2swf.exe的所有输出信息,都为错误输出流,用 StandardOutput是捕获不到任何消息的...
29            p.StartInfo.CreateNoWindow = true;//不创建进程窗口
30            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
31            p.Start();//启动线程
32            p.BeginErrorReadLine();//开始异步读取
33            p.WaitForExit();//等待完成
34            //p.StandardError.ReadToEnd();//开始同步读取
35            p.Close();//关闭进程
36            p.Dispose();//释放资源
37            return true;
38        }
39        public static bool PdfToSwf(string pdfPath, string swfPath)
40        {
41            return PDF2SWF(pdfPath, swfPath, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)));
42        }
43  
44        public static int GetPageCount(string pdfPath)
45        {
46            //try
47            //{
48            byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
49            int length = buffer.Length;
50            if (buffer == null)
51                return -1;
52            if (buffer.Length <= 0)
53                return -1;
54            string pdfText = Encoding.Default.GetString(buffer);
55            System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type/s*/Page[^s]");
56            System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
57            return matches.Count;
58            //}
59            //catch (Exception ex)
60            //{
61            //    throw ex;
62            //}
63        }
64    public string getSwfPath(string filePath){//返回一个以swf扩展名结尾的新路径
65        return "swf/" + filePath.Replace(System.IO.Path.GetExtension(filePath), "").Replace("shwpoppdf/","pdf") + ".swf";
66    }
67    protected void Page_Load(object sender, EventArgs e)
68    {
69        PdfToSwf("shwpoppdf/sitemap.pdf", getSwfPath("shwpoppdf/sitemap.pdf"));//调用函数做文件转换
70        execStr = "<script>sSwf(/"" + getSwfPath("shwpoppdf/sitemap.pdf") + "/")</script>";//调用前台Js flashpaper实现播放
71    }

页面代码:

view source
print?
01function sSwf(swf){
02         var swfVersionStr = "10.0.0";  
03         var xiSwfUrlStr = "playerProductInstall.swf";  
04         var flashvars = {  
05               SwfFile : escape(swf),  
06   Scale : 0.6,  
07   ZoomTransition : "easeOut",  
08   ZoomTime : 0.5,  
09     ZoomInterval : 0.1,  
10     FitPageOnLoad : false,  
11     FitWidthOnLoad : true,  
12     PrintEnabled : true,  
13     FullScreenAsMaxWindow : false,  
14     ProgressiveLoading : true,  
15     PrintToolsVisible : true,  
16     ViewModeToolsVisible : true,  
17     ZoomToolsVisible : true,  
18     FullScreenVisible : true,  
19     NavToolsVisible : true,  
20     CursorToolsVisible : true,  
21   SearchToolsVisible : true,  
22     localeChain: "zh_CN" 
23   };  
24  
25 var params = {  
26  
27    }  
28         params.quality = "high";  
29         params.bgcolor = "#ffffff";  
30         params.allowscriptaccess = "sameDomain";  
31         params.allowfullscreen = "true";  
32         var attributes = {};  
33         attributes.id = "FlexPaperViewer";  
34         attributes.name = "FlexPaperViewer";  
35         swfobject.embedSWF(  
36             "FlexPaperViewer.swf", "flashContent",  
37             "650", "500",  
38             swfVersionStr, xiSwfUrlStr,  
39             flashvars, params, attributes);  
40swfobject.createCSS("#flashContent", "display:block;text-align:left;");  
41}

调用:
<div id=”flashContent”>
      <%=execStr%> 
</div>
</p>

补上的源文件下载:http://www.52-ni.cn/wp-content/uploads/2011/06/Deom.rar