引言

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

使用的页面代码:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title></title>
<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">
<!-- 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> </head>
<body>
<div style="position:absolute;left:10px;top:10px;">
<div id="flashContent">
<p>
To view this page ensure that Adobe Flash Player version
10.0.0 or greater is installed.
</p>
<script type="text/javascript">
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>
</body>
</html>

效果图

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

swftool工具

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

 using System.Web;
using System.Text; public static class PSD2swfHelper
{
/// <summary>
/// 转换所有的页,图片质量80%
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
public static bool PDF2SWF(string pdfPath, string swfPath)
{
return PDF2SWF(pdfPath, swfPath, , GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), );
} /// <summary>
/// 转换前N页,图片质量80%
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
/// <param name="page">页数</param>
public static bool PDF2SWF(string pdfPath, string swfPath, int page)
{
return PDF2SWF(pdfPath, swfPath, , page, );
} /// <summary>
/// PDF格式转为SWF
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
/// <param name="swfPath">生成后的SWF文件地址</param>
/// <param name="beginpage">转换开始页</param>
/// <param name="endpage">转换结束页</param>
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 > 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;
} /// <summary>
/// 返回页数
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
private static int GetPageCount(string pdfPath)
{
byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
int length = buffer.Length;
if (buffer == null)
return -;
if (buffer.Length <= )
return -;
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/1hqEpx5a 密码:gupg

swftools-2013-04-09-1007下载:链接:http://pan.baidu.com/s/1c0CvBDA 密码:v38r

FlexPaper+SWFTool+操作类=在线预览PDF的更多相关文章

  1. FlexPaper+SWFTool+操作类=在线预览PDF(转)

    引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...

  2. 在线预览PDF

    FlexPaper+SWFTool+操作类=在线预览PDF   引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf ...

  3. .net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

    FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等. 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览. pdf很好实现. 首先下载相关的 ...

  4. 网页中动态嵌入PDF文件/在线预览PDF内容https://www.cnblogs.com/xgyy/p/6119459.html

    #网页中动态嵌入PDF文件/在线预览PDF内容# 摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如 ...

  5. WEB在线预览PDF

    这是我在博客园发表的第一篇文章.以后会陆续把在线预览其他格式文档的解决方案发表出来. 解决思路:把pdf转换成html显示. 在线预览pdf我暂时了解3种解决方案,欢迎大家补充. 方案一: 利用pdf ...

  6. 用pdf.js实现在移动端在线预览pdf文件

    用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js    官网地址:https://mozilla.github.io/pdf.js/ 2.配置    下载下来的文件包,就是一个demo ...

  7. #网页中动态嵌入PDF文件/在线预览PDF内容#

    摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如下: 代码片段1: 1 <object ty ...

  8. 前端实现在线预览pdf、docx、xls、ppt等文件

    思路:前台将各种格式的附件上传到服务器----后台通过方法将这些格式的文件转化成图片,前台通过放映ppt的方式将其展示在页面上. 关键点:reveal.js 参考文章:https://www.awes ...

  9. 网页嵌入pdf、在线预览pdf工具及插件(转)

    摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如下: 代码片段1: 1 <object ty ...

随机推荐

  1. COGS743. [网络流24题] 最长k可重区间集

    743. [网络流24题] 最长k可重区间集 ★★★   输入文件:interv.in   输出文件:interv.out   简单对比时间限制:1 s   内存限制:128 MB «问题描述: «编 ...

  2. 第5章 Java数组

    1.什么是数组 数组可以想象成一个巨大的盒子,这个盒子里面存放的是同一个数据类型的数据 例如:int[] scores = {78,68,94,93}; 2.如何使用Java中的数组 2.1申明数组 ...

  3. IdentityHashMap类:增加相同Key(键)内容的结果

    正常Map操作中,key是不能重复的. 先看使用HashMap()操作,增加相同Key内容的结果: 先看代码: package 类集; import java.util.HashMap; import ...

  4. Java多线程系列--“JUC线程池”06之 Callable和Future

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  5. docker创建镜像

    手动创建: docker run -d -p mynginx:v2 nginx rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest- ...

  6. css3伪类温故知新

    今天遇到一个问题.要求::after 和 ::before的 content:"content" 能够动态的进行变换,能被JavaScript操作. 百度了下,自己做了实验,发现有 ...

  7. Python-07-面向对象(进阶篇)

    1.面向对象高级语法部分 1.1 静态方法 通过 @staticmethod 装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...

  8. 微信小程序之登录态维护(十一)

    [未经作者本人同意,请勿以任何形式转载] >什么是登录态? 所谓登录态,就是程序在运行时,能够识别当前用户,能够证明自己的唯一性且合法. 我们知道,WEB服务器通过浏览器携带的cookie获取s ...

  9. Jumony Core 3,真正的HTML引擎,正式版发布

    Jumony是一个开源项目,已经有三年的历史了,在这三年中,秉承提供给.NET程序员完整的HTML掌控能力,Jumony历经无数次的改进,终于进入了一个新的阶段.Jumony Core 3是一个真正意 ...

  10. 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法

    现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...