[转载]FlexPaper+SWFTool+操作类=在线预览PDF - wolfy - 博客园

[转载]FlexPaper+SWFTool+操作类=在线预览PDF – wolfy – 博客园.

引言

由于客户有在线预览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项目结构如图

使用的页面代码:


<!-- saved from url=(0014)about:internet -->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css" media="screen"><!--
html, body { height: 100%; } body { margin: 0; padding: 0; overflow: auto; } #flashContent { display: none; }
--></style><!--首先引入相关的js文件-->
<script type="text/javascript" src="js/swfobject/swfobject.js"></script><script type="text/javascript" src="js/flexpaper_flash.js"></script>
<!--配置-->
<script type="text/javascript">// <![CDATA[
For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. -->
        var swfVersionStr = "10.0.0";
        <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
        var xiSwfUrlStr = "playerProductInstall.swf";

        var flashvars = {
            SwfFile: escape("Paper.swf"),
            Scale: 0.6,
            ZoomTransition: "easeOut",
            ZoomTime: 0.5,
            ZoomInterval: 0.1,
            FitPageOnLoad: false,
            FitWidthOnLoad: true,
            PrintEnabled: true,
            FullScreenAsMaxWindow: false,
            ProgressiveLoading: true,
            PrintToolsVisible: true,
            ViewModeToolsVisible: true,
            ZoomToolsVisible: true,
            FullScreenVisible: true,
            NavToolsVisible: true,
            CursorToolsVisible: true,
            SearchToolsVisible: true,
            localeChain: "en_US"
        };
        var params = {
        }
        params.quality = "high";
        params.bgcolor = "#ffffff";
        params.allowscriptaccess = "sameDomain";
        params.allowfullscreen = "true";
        var attributes = {};
        attributes.id = "FlexPaperViewer";
        attributes.name = "FlexPaperViewer";
        swfobject.embedSWF(
            "FlexPaperViewer.swf", "flashContent",
            "650", "500",
            swfVersionStr, xiSwfUrlStr,
            flashvars, params, attributes);
        swfobject.createCSS("#flashContent", "display:block;text-align:left;");
// ]]></script>

&nbsp;
<div style="position: absolute; left: 10px; top: 10px;">
<div id="flashContent">

To view this page ensure that Adobe Flash Player version
10.0.0 or greater is installed.

<script type="text/javascript">// <![CDATA[
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 id="errNoDocument" style="padding-top: 10px;">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>.</div>
</div>

效果图

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

swftool工具

 操作类(本类来自常用类库,从网上下载的,一搜一大把)

using System.Web;
using System.Text;

public static class PSD2swfHelper
{
///
/// 转换所有的页,图片质量80%
///

///PDF文件地址 ///生成后的SWF文件地址 public static bool PDF2SWF(string pdfPath, string swfPath)
{
return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), 80);
}

///
/// 转换前N页,图片质量80%
///

///PDF文件地址 ///生成后的SWF文件地址 ///页数 public static bool PDF2SWF(string pdfPath, string swfPath, int page)
{
return PDF2SWF(pdfPath, swfPath, 1, page, 80);
}

///
/// PDF格式转为SWF
///

///PDF文件地址 ///生成后的SWF文件地址 ///转换开始页 ///转换结束页 private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
{
//swftool,首先先安装,然后将安装目录下的东西拷贝到tools目录下
string exe = HttpContext.Current.Server.MapPath("~/Bin/tools/pdf2swf.exe");
pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
swfPath = HttpContext.Current.Server.MapPath(swfPath);
if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
{
return false;
}
StringBuilder sb = new StringBuilder();
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -s flashversion=9");
if (endpage &gt; GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
sb.Append(" -j " + photoQuality);
string Command = sb.ToString();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = exe;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
p.Close();
p.Dispose();
return true;
}

///
/// 返回页数
///

///PDF文件地址 private static int GetPageCount(string pdfPath)
{
byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
int length = buffer.Length;
if (buffer == null)
return -1;
if (buffer.Length &lt;= 0)
return -1;
string pdfText = Encoding.Default.GetString(buffer);
System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
return matches.Count;
}
}

然后安装swftool工具,将安装后的目录中的文件拷贝到tools目录下,如图

 

test.asp.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Wolfy.FlexPaperDemo
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//这里需要虚拟路径
PSD2swfHelper.PDF2SWF("PDFFile/王牌2_C#_控件查询手册.pdf", "SWFFile/王牌2_C#_控件查询手册.swf");
}
}
}

如何禁用右键中的打印,复制功能

如果pdf保密性强,不让别人复制,打印等该如何?在上面推荐的两篇文章中,他们对其进行了二次开发,禁用了这个功能。非常感谢,那么之后只需将FlexPaperViewer.swf替换就可以了。

原图

替换后的

 

说保密只是相对的,在互联网上,只要能看,别人想盗取还是很容易的事,大不了,一张一张的截图。

总结

互联网,没有绝对安全的,想安全就别放在互联网上显摆,只要想要,总会有办法的。有时候客户的需求真他妈的让人蛋疼。

demo下载:链接:http://pan.baidu.com/s/1sjnSsyx 密码:jjbx

swftools-2013-04-09-1007下载:http://pan.baidu.com/s/1jGI8H4U

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏