FlexPaper+SWFTool+操作类=在线预览PDF
引言由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swftool配合使用,在程序运行时将pdf文件转换为swf格式的文件。 如果flexpaper不满足你的要求,也可以对其进行二次开发,这里推荐两篇文章,希望对您有所帮助: http://www.cnblogs.com/xcong/archive/2013/06/20/3142155.html http://www.cnblogs.com/zamlove/archive/2013/05/07/3065079.html 如何使用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></ 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 014 padding15 overflow auto16 17 18 #flashContent 19 display none20 21 </style22 首先引入相关的js文件23 script ="text/javascript" src="js/swfobject/swfobject.js"script24 ="js/flexpaper_flash.js"25 配置26 ="text/javascript"27 <!-- For version detection,set to min. required Flash Player version,or 0 (or 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 xiSwfUrlStr playerProductInstall.swf31 32 flashvars = { 33 SwfFile: escape(Paper.swf34 Scale: 0.6,128); line-height:1.5!important">35 ZoomTransition: eaSEOut36 ZoomTime: 0.537 ZoomInterval: 0.138 FitPageOnLoad: false39 FitWidthOnLoad: true40 PrintEnabled: 41 FullScreenAsMaxWindow: 42 ProgressiveLoading: 43 PrintToolsVisible: 44 ViewModeToolsVisible: 45 ZoomToolsVisible: 46 FullScreenVisible: 47 NavToolsVisible: 48 CursorToolsVisible: 49 SearchToolsVisible: 50 localeChain: en_US" 51 }; 52 params 53 } 54 params.quality high55 params.bgcolor #ffffff56 params.allowscriptaccess sameDomain57 params.allowfullscreen true58 attributes {}; 59 attributes.id FlexPaperViewer60 attributes.name 61 swfobject.embedSWF( 62 FlexPaperViewer.swfflashContent63 65050064 swfVersionStr,xiSwfUrlStr,128); line-height:1.5!important">65 flashvars,params,attributes); 66 swfobject.createCSS(#flashContentdisplay:block;text-align:left;); 67 68 69 70 body71 div ="position:absolute;left:10px;top:10px;"72 id="flashContent"73 p74 To view this page ensure that Adobe Flash Player version 75 10.0.0 or greater is installed. 76 77 78 pageHost ((document.location.protocol == https:) ? https:// : http://79 document.write(<a href='http://www.adobe.com/go/getflashplayer'><img src='80 ++ www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>81 82 div83 ="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 websitea>. 85 86 87 88 html> 效果图
上面的工具栏:打印,全屏等功能可配置。 swftool工具?操作类(本类来自常用类库,从网上下载的,一搜一大把) using System.Web;
using System.Text;
3
public static class PSD2swfHelper
5 {
6 /// <summary>
7 /// 转换所有的页,图片质量80%
8 </summary>
9 <param name="pdfPath">PDF文件地址</param>
10 <param name="swfPath">生成后的SWF文件地址11 bool PDF2SWF(string pdfPath,string swfPath)
12 {
13 return PDF2SWF(pdfPath,swfPath,1,GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)),128); line-height:1.5!important">80);
14 }
15
16 17 转换前N页,图片质量80%
18 19 20 21 <param name="page">页数string swfPath,255); line-height:1.5!important">int page)
23 {
24 25 }
26
27 28 PDF格式转为SWF
29 30 31 32 <param name="beginpage">转换开始页33 <param name="endpage">转换结束页34 private int beginpage,255); line-height:1.5!important">int endpage,255); line-height:1.5!important">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=948 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 = 57 p.StartInfo.RedirectStandardError = true;
58 p.StartInfo.CreateNoWindow = 59 p.Start();
60 p.BeginErrorReadLine();
61 p.WaitForExit();
62 p.Close();
63 p.Dispose();
64 65 }
66
67 68 返回页数
69 70 int GetPageCount(string pdfPath)
72 {
73 byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
74 int length = buffer.Length;
75 if (buffer == null)
return -1;
77 if (buffer.Length <= 0)
78 79 string pdfText = Encoding.Default.GetString(buffer);
80 System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Types*/Page[^s]81 System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
return matches.Count;
83 }
84 }
然后安装swftool工具,将安装后的目录中的文件拷贝到tools目录下,如图 ? test.asp.cs代码 using System;
using System.Collections.Generic;
using System.Linq;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 namespace Wolfy.FlexPaperDemo
9 {
10 partial class Test : System.Web.UI.Page
11 {
12 protected void Page_Load(object sender,EventArgs e)
13 {
14 这里需要虚拟路径
15 PSD2swfHelper.PDF2SWF(PDFFile/王牌2_C#_控件查询手册.pdf",0); line-height:1.5!important">SWFFile/王牌2_C#_控件查询手册.swf16 }
17 }
18 }
结果
如何禁用右键中的打印,复制功能如果pdf保密性强,不让别人复制,打印等该如何?在上面推荐的两篇文章中,他们对其进行了二次开发,禁用了这个功能。非常感谢,那么之后只需将FlexPaperViewer.swf替换就可以了。 原图
替换后的 ? 说保密只是相对的,在互联网上,只要能看,别人想盗取还是很容易的事,大不了,一张一张的截图。 总结互联网,没有绝对安全的,想安全就别放在互联网上显摆,只要想要,总会有办法的。有时候客户的需求真他妈的让人蛋疼。 demo下载:链接:http://pan.baidu.com/s/1sjnSsyx?密码:jjbx swftools-2013-04-09-1007下载:http://pan.baidu.com/s/1jGI8H4U (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |