1. 程式人生 > >FlexPaper+SWFTool+操作類=線上預覽PDF

FlexPaper+SWFTool+操作類=線上預覽PDF

引言

由於客戶有線上預覽PDF格式的需求,在網上找了一下解決方案,覺得FlexPaper用起來還是挺方便的,flexpaper是將pdf轉換為swf格式的檔案預覽的,所以flexpaper一般和swftool配合使用,在程式執行時將pdf檔案轉換為swf格式的檔案。

如果flexpaper不滿足你的要求,也可以對其進行二次開發,這裡推薦兩篇文章,希望對您有所幫助:

如何使用flexpaper

測試demo專案結構如圖

使用的頁面程式碼:

複製程式碼
 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
> 2 <!-- saved from url=(0014)about:internet --> 3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 4 <head> 5 <title></title> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 7 <style type="text/css" media="screen"
> 8 html, body { 9 height: 100%; 10 } 11 12 body { 13 margin: 0; 14 padding: 0; 15 overflow: auto; 16 } 17 18 #flashContent { 19 display: none; 20 } 21 </style> 22 <!--首先引入相關的js檔案
--> 23 <script type="text/javascript" src="js/swfobject/swfobject.js"></script> 24 <script type="text/javascript" src="js/flexpaper_flash.js"></script> 25 <!--配置--> 26 <script type="text/javascript"> 27 <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 28 var swfVersionStr = "10.0.0"; 29 <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. --> 30 var xiSwfUrlStr = "playerProductInstall.swf"; 31 32 var flashvars = { 33 SwfFile: escape("Paper.swf"), 34 Scale: 0.6, 35 ZoomTransition: "easeOut", 36 ZoomTime: 0.5, 37 ZoomInterval: 0.1, 38 FitPageOnLoad: false, 39 FitWidthOnLoad: true, 40 PrintEnabled: true, 41 FullScreenAsMaxWindow: false, 42 ProgressiveLoading: true, 43 PrintToolsVisible: true, 44 ViewModeToolsVisible: true, 45 ZoomToolsVisible: true, 46 FullScreenVisible: true, 47 NavToolsVisible: true, 48 CursorToolsVisible: true, 49 SearchToolsVisible: true, 50 localeChain: "en_US" 51 }; 52 var params = { 53 } 54 params.quality = "high"; 55 params.bgcolor = "#ffffff"; 56 params.allowscriptaccess = "sameDomain"; 57 params.allowfullscreen = "true"; 58 var attributes = {}; 59 attributes.id = "FlexPaperViewer"; 60 attributes.name = "FlexPaperViewer"; 61 swfobject.embedSWF( 62 "FlexPaperViewer.swf", "flashContent", 63 "650", "500", 64 swfVersionStr, xiSwfUrlStr, 65 flashvars, params, attributes); 66 swfobject.createCSS("#flashContent", "display:block;text-align:left;"); 67 </script> 68 69 </head> 70 <body> 71 <div style="position:absolute;left:10px;top:10px;"> 72 <div id="flashContent"> 73 <p> 74 To view this page ensure that Adobe Flash Player version 75 10.0.0 or greater is installed. 76 </p> 77 <script type="text/javascript"> 78 var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); 79 document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" 80 + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>"); 81 </script> 82 </div> 83 <div id="errNoDocument" style="padding-top:10px;"> 84 Can't see the document? Running FlexPaper from your local directory? Make sure you have added FlexPaper as trusted. You can do that at <a href="http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065">Adobe's website</a>. 85 </div> 86 </div> 87 </body> 88 </html>
複製程式碼

效果圖

上面的工具欄:列印,全屏等功能可配置。

swftool工具

 操作類(本類來自常用類庫,從網上下載的,一搜一大把)

複製程式碼
 1 using System.Web;
 2 using System.Text;
 3 
 4 public static class PSD2swfHelper
 5 {
 6     /// <summary>
 7     /// 轉換所有的頁,圖片質量80%
 8     /// </summary>
 9     /// <param name="pdfPath">PDF檔案地址</param>
10     /// <param name="swfPath">生成後的SWF檔案地址</param>
11     public static bool PDF2SWF(string pdfPath, string swfPath)
12     {
13         return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), 80);
14     }
15 
16     /// <summary>
17     /// 轉換前N頁,圖片質量80%
18     /// </summary>
19     /// <param name="pdfPath">PDF檔案地址</param>
20     /// <param name="swfPath">生成後的SWF檔案地址</param>
21     /// <param name="page">頁數</param>
22     public static bool PDF2SWF(string pdfPath, string swfPath, int page)
23     {
24         return PDF2SWF(pdfPath, swfPath, 1, page, 80);
25     }
26 
27     /// <summary>
28     /// PDF格式轉為SWF
29     /// </summary>
30     /// <param name="pdfPath">PDF檔案地址</param>
31     /// <param name="swfPath">生成後的SWF檔案地址</param>
32     /// <param name="beginpage">轉換開始頁</param>
33     /// <param name="endpage">轉換結束頁</param>
34     private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
35     {
36         //swftool,首先先安裝,然後將安裝目錄下的東西拷貝到tools目錄下
37         string exe = HttpContext.Current.Server.MapPath("~/Bin/tools/pdf2swf.exe");
38         pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
39         swfPath = HttpContext.Current.Server.MapPath(swfPath);
40         if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
41         {
42             return false;
43         }
44         StringBuilder sb = new StringBuilder();
45         sb.Append(" \"" + pdfPath + "\"");
46         sb.Append(" -o \"" + swfPath + "\"");
47         sb.Append(" -s flashversion=9");
48         if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
49         sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
50         sb.Append(" -j " + photoQuality);
51         string Command = sb.ToString();
52         System.Diagnostics.Process p = new System.Diagnostics.Process();
53         p.StartInfo.FileName = exe;
54         p.StartInfo.Arguments = Command;
55         p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
56         p.StartInfo.UseShellExecute = false;
57         p.StartInfo.RedirectStandardError = true;
58         p.StartInfo.CreateNoWindow = false;
59         p.Start();
60         p.BeginErrorReadLine();
61         p.WaitForExit();
62         p.Close();
63         p.Dispose();
64         return true;
65     }
66 
67     /// <summary>
68     /// 返回頁數
69     /// </summary>
70     /// <param name="pdfPath">PDF檔案地址</param>
71     private static int GetPageCount(string pdfPath)
72     {
73         byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
74         int length = buffer.Length;
75         if (buffer == null)
76             return -1;
77         if (buffer.Length <= 0)
78             return -1;
79         string pdfText = Encoding.Default.GetString(buffer);
80         System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
81         System.Text.RegularExpressions.MatchCollection matches =