因最近一个工作项目的需要,也做了一个像百度文库那种在web页的flash中能播放pdf文档、word文档,其实原理比较简单。
只在上传时还是在启动播放时,把自动pdf转换为swf,然后调用flash Paper播放就可。
要用到的工具
1、 SWFTools ,安装完成后通过WEB编程调用其实现对文档的转换。
2、 flash pager 一个swf播放插件,集成有缩小放大,拖动,全屏等功能,不一定要用这个, 也可以自己用AS写一个。
效果如下:
代码如下:
asp.net c#
01 | public string execStr = "" ; |
02 | public static bool PDF2SWF(string pdfPath, string swfPath, int page) |
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)) |
11 | StringBuilder sb = new StringBuilder(); |
12 | sb.Append( " /"" + pdfPath + "/"" ); |
13 | sb.Append( " -o /"" + swfPath + "/"" ); |
15 | sb.Append( " -s flashversion=9" ); |
17 | sb.Append( " -p " + "/"1" + "-" + page + "/"" ); |
19 | string Command = sb.ToString(); |
20 | System.Diagnostics.Process p = new System.Diagnostics.Process(); |
22 | p.StartInfo.FileName = exe; |
23 | p.StartInfo.Arguments = Command; |
24 | p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath( "~/Bin/" ); |
25 | p.StartInfo.UseShellExecute = false ; |
28 | p.StartInfo.RedirectStandardError = true ; |
29 | p.StartInfo.CreateNoWindow = true ; |
30 | p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; |
32 | p.BeginErrorReadLine(); |
39 | public static bool PdfToSwf(string pdfPath, string swfPath) |
41 | return PDF2SWF(pdfPath, swfPath, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath))); |
44 | public static int GetPageCount(string pdfPath) |
48 | byte [] buffer = System.IO.File.ReadAllBytes(pdfPath); |
49 | int length = buffer.Length; |
52 | if (buffer.Length <= 0 ) |
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); |
64 | public string getSwfPath(string filePath){ |
65 | return "swf/" + filePath.Replace(System.IO.Path.GetExtension(filePath), "" ).Replace( "shwpoppdf/" , "pdf" ) + ".swf" ; |
67 | protected void Page_Load(object sender, EventArgs e) |
69 | PdfToSwf( "shwpoppdf/sitemap.pdf" , getSwfPath( "shwpoppdf/sitemap.pdf" )); |
70 | execStr = "<script>sSwf(/"" + getSwfPath( "shwpoppdf/sitemap.pdf" ) + "/")</script>" ; |
页面代码:
02 | var swfVersionStr = "10.0.0" ; |
03 | var xiSwfUrlStr = "playerProductInstall.swf" ; |
05 | SwfFile : escape(swf), |
07 | ZoomTransition : "easeOut" , |
10 | FitPageOnLoad : false , |
11 | FitWidthOnLoad : 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 , |
28 | params.quality = "high" ; |
29 | params.bgcolor = "#ffffff" ; |
30 | params.allowscriptaccess = "sameDomain" ; |
31 | params.allowfullscreen = "true" ; |
33 | attributes.id = "FlexPaperViewer" ; |
34 | attributes.name = "FlexPaperViewer" ; |
36 | "FlexPaperViewer.swf" , "flashContent" , |
38 | swfVersionStr, xiSwfUrlStr, |
39 | flashvars, params, attributes); |
40 | swfobject.createCSS( "#flashContent" , "display:block;text-align:left;" ); |
调用:
<div id=”flashContent”>
<%=execStr%>
</div>
</p>
补上的源文件下载:http://www.52-ni.cn/wp-content/uploads/2011/06/Deom.rar