本人既蠢又懒纯菜鸟一枚,出道近三载却发现自己好生颓废,一切都好似原地踏步!决定痛定思痛痛改前非,不能再这样虚度光阴!
首先有了整理之前积累经验的想法,思来想去却又实在没有能够拿出手的东西,只有把在前东家做的一个仿百度文库的小功能献上,但愿能为有需要者提供些许参考……
功能需求为pdf文档只供在线查看不能下载或打印,所尝试方法均参考网络资料,归为原创确实心虚,下面开始介绍:
方法一:pdfobject.js加载pdf文件 + 隐藏Adobe工具栏:该方法严格来说没有实现要求,因为pdf文档在缓存中可以找到,如果要求不高可以使用本方法
1.引入pdfobject.js
2.margin-top设置为-57px隐藏Adobe工具栏
<head runat="server">
<script type="text/javascript" src="scripts/pdfobject.js"></script>
<script type="text/javascript">
window.onload = function() {
var success = new PDFObject({ url: 'test.pdf', pdfOpenParams: { scrollbars: '0', toolbar: '0', statusbar: '0'} }).embed("pdfLoad");
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:700px; height:600px;">
<p id="pdf" style="margin-top: -57px; margin-bottom: 0" mce_style="margin-top: -57px; margin-bottom: 0">
</p>
</div>
</form>
</body>
方法二:SWFTools + FlexPaper:该方法通过SWFTools将pdf文件转换为swf格式通过FlexPaper展示,但是有些pdf文件通过转换工具无法转换或转换后无法显示,高版本FlexPaper未找到去打印功能版
1.安装SWFTools 0.9.0
3.将FlexPaper 2.1.0中的css和js文件夹及FlexPaperViewer.swf文件拷贝到项目中,创建浏览页面(浏览页面与FlexPaperViewer.swf要位于同一目录)
<head runat="server">
<link rel="stylesheet" type="text/css" href="FlexPaper2.1.0/css/flexpaper.css" />
<script type="text/javascript" src="FlexPaper2.1.0/js/jquery.min.js"></script>
<script type="text/javascript" src="FlexPaper2.1.0/js/flexpaper.js"></script>
<script type="text/javascript" src="FlexPaper2.1.0/js/flexpaper_handlers.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="documentViewer" class="flexpaper_viewer" style="width:80%;height:80%" align="center">
<script type="text/javascript">
$('#documentViewer').FlexPaperViewer(
{ config: {
SWFFile: 'test.swf',
Scale: 0.6,
ZoomTransition: 'easeOut',
ZoomTime: 0.5,
ZoomInterval: 0.2,
FitPageOnLoad: true,
FitWidthOnLoad: true,
FullScreenAsMaxWindow: false,
ProgressiveLoading: false,
MinZoomSize: 0.2,
MaxZoomSize: 5,
SearchMatchAll: false,
InitViewMode: 'Portrait',
RenderingOrder: 'flash',
StartAtPage: '',
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
WMode: 'window',
localeChain: 'en_US'
}
}
);
</script>
</div>
</form>
</body>
4.转换代码:
public bool Pdf2Swf(string fileUrl, string savePath)
{
try
{
using (Process p = new Process())
{
string cmdStr = @"C:\Program Files\SWFTools\pdf2swf.exe";
string argsStr = " -t " + fileUrl + " -s flashversion=9 -o " + savePath;
//调用新进程 进行转换
ProcessStartInfo psi = new ProcessStartInfo(cmdStr, argsStr);
p.StartInfo = psi;
p.Start();
p.WaitForExit();
}
}
catch
{
return false;
}
return true;
}
方法三:Print2flash + Print2flash自带展示代码:所有能够打印预览的文件均可以通过Print2flash转换为swf,而且可以设置转换参数(如是否允许打印等),但是其转换的swf文件无法在FlexPaper 2.1.0中显示,不知道是不是因为FlexPaper版本太低的问题;采用Print2flash将文件转换成html文件自动产生的代码展示转换后的文件,存在问题为代码调用Print2flash没有找到参数设置去除打印功能
1.安装Print2flash
2.将文件通过Print2flash转换为html文件自动生成的前台展示代码:
<body>
<!-- Start of document code -->
<script type="text/javascript" language="JavaScript1.1">
<!--
var requiredMajorVersion = 9;
var requiredMinorVersion = 0;
var requiredRevision = 28;
var appVersion = navigator.appVersion.toLowerCase()
var isIE = (appVersion.indexOf("msie") != -1) ? true : false;
var isWin = (appVersion.indexOf("win") != -1) ? true : false;
var isMac = /mac/.test(appVersion);
var isSafari = /webkit/.test(appVersion);
var isOpera = /opera/.test(appVersion);
var safariVersion = (appVersion.match(/version\/((?:\d|\.)+)/) || [])[1]
function JSGetSwfVer(i) {
if (navigator.plugins != null && navigator.plugins.length > 0) {
if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
descArray = flashDescription.split(" ");
tempArrayMajor = descArray[2].split(".");
versionMajor = tempArrayMajor[0];
versionMinor = tempArrayMajor[1];
if (descArray[3] != "") tempArrayMinor = descArray[3].split("r"); else tempArrayMinor = descArray[4].split("r");
versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
} else flashVer = -1;
}
else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
else flashVer = -1;
return flashVer;
}
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
reqVer = parseFloat(reqMajorVer + "." + reqRevision);
for (i = 25; i > 0; i--) {
versionStr = JSGetSwfVer(i);
if (versionStr == -1) return false;
else if (versionStr != 0) {
versionArray = versionStr.split(".");
versionMajor = versionArray[0];
versionMinor = versionArray[1];
versionRevision = versionArray[2];
versionString = versionMajor + "." + versionRevision;
versionNum = parseFloat(versionString);
if (versionMajor > reqMajorVer && versionNum >= reqVer) return true; else return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false);
}
}
return (reqVer ? false : 0.0);
}
function GetDoc(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
var P2FDocs = new Array()
function AddP2FDoc(P2FDoc) {
P2FDocs.push(P2FDoc);
}
var oldonmousewheel = document.onmousewheel
function mousewheel(event) {
for (var i = 0; i < P2FDocs.length; i++) {
if (event.target == P2FDocs[i]) {
var delta = 0;
if (event.wheelDelta) delta = event.wheelDelta / (isOpera ? 12 : 120);
else if (event.detail) delta = -event.detail;
if (event.preventDefault) event.preventDefault();
try {
P2FDocs[i].scrollLine(delta);
}
catch (e) {
}
return true;
}
}
return oldonmousewheel(event)
}
if (isMac || isWin && isSafari && safariVersion < "4.0") {
if (typeof window.addEventListener != "undefined") window.addEventListener("DOMMouseScroll", mousewheel, false);
window.onmousewheel = document.onmousewheel = mousewheel;
}
// -->
</script>
<!-- Start of document placement code -->
<script language="JavaScript" type="text/javascript">
<!--
var width = "100%"
var height = "100%"
var align = "Middle"
var name = "Print2FlashDoc"
var url = 'test.swf' <%--文件路径--%>
var flashvars = ""
var alternateContent = 'This content requires the Adobe Flash Player. It either has not been installed yet or is prohibited by your browser security settings. Either'
+ ' <a href="http://www.macromedia.com/go/getflash/">click here to get Flash</a> or loosen your browser security restrictions';
if (isIE && isWin) alternateContent += ' and then <a href="javascript:document.location.reload()">Refresh</a> this page'
alternateContent += '.'
if (isIE && isWin) {
var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
+ 'width="' + width + '" height="' + height + '" align="' + align + '" id="' + name + '"'
+ 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + requiredMajorVersion + ',' + requiredMinorVersion + ',' + requiredRevision + ',0">'
+ '<param name="movie" value="' + url + '" /><param name="quality" value="best" />'
+ '<param name="allowFullScreen" value="true" />'
+ '<param name="allowFullScreenInteractive" value="true" />'
+ '<param name="allowScriptAccess" value="sameDomain" />'
+ '<param name="FlashVars" value="extName=' + name + flashvars + '" />'
+ '<param name="menu" value="false" />'
+ alternateContent
+ '<\/object>';
document.write(oeTags);
AddP2FDoc(GetDoc(name));
} else if (DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)) {
var oeTags = '<embed src="' + url + '" quality="best" '
+ 'width="' + width + '" height="' + height + '" align="' + align + '" name="' + name + '" '
+ 'play="true" '
+ 'loop="false" '
+ 'quality="best" '
+ 'allowScriptAccess="sameDomain" allowFullScreen="true" allowFullScreenInteractive="true"'
+ 'type="application/x-shockwave-flash" '
+ 'pluginspage="http://www.macromedia.com/go/getflashplayer" '
+ 'FlashVars="extName=' + name + flashvars + '"> '
+ '<\/embed>'
document.write(oeTags);
AddP2FDoc(GetDoc(name));
} else document.write(alternateContent);
-->
</script>
<%--</p>--%>
<noscript>
This content requires scripts to be enabled in your browser. Please enable scripts in your browser settings.
</noscript>
<!-- End of document placement code -->
<!-- End of document code -->
</body>
3.后台转化代码:
public void Pdf2Swf(string src,string dest)
{
Print2Flash3.Server2 server = new Print2Flash3.Server2();
server.ConvertFile(src, dest, null, null, null);
}
以上三种方法均有不足之处,还待大神指导完善;因本人坐井太久,见识极为有限,亦期待大神指点一二……