之前在写系统的时候有个功能要求直接在页面上显示PDF文件,而且根据用户权限决定是否可打印这一PDF文档。
实现代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show Document</title>
<script type="text/javascript" src="js/jquery-1.4.4.min.js" ></script>
<script type="text/javascript">
<%
boolean canPrint = request.getParameter("canPrint") == null ? false : (boolean)request.getParameter("canPrint");
String docPath = request.getParameter("docPath ") == null ? "": request.getParameter("docPath ");
%>
var canPrint = <%=canPrint%>;
$(function(){
if(!canPrint)
$('#pHead').attr('style','margin-top:-45px;margin-bottom:0px');
else
$('#pHead').attr('style','margin-top:0px;margin-bottom:0px');
});
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
</script>
</head>
<body>
<p id="pHead" style="margin-top:-45px;margin-bottom:0px"></p>
<object id="obj" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="100%" height="800px" border="0" name="pdf">
<param name="toolbar" value="false">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param id="pURL" name="SRC" value="<%=docPath%>">
</object>
</body>
</html>
请求此页面时地址须带canPrint和docPath两个参数,比如:http://lsls22/thisPage.jsp?canPrint=false&docPath=C:\test.pdf 。
根据用户的打印权限,决定是否显示工具列。若无权限,则通过设置margin-top属性的值把pdf的工具列隐藏掉。
此功能代码最重要的是<object>标签(w3cshool地址:http://www.w3school.com.cn/tags/tag_object.asp)。
关于<object>,网上搜了搜,Mark如下:
1.对<objec>t的理解:
网页中的object是一个COM组件,classid是这一COM组件在系统中注册的一个ID值,有了这个ID值系统就能找到这个COM组件对应的DLL文件。
2.<object>的classid属性
这一属性用于设置文件格式。有个快捷实用的方法,在dreamweaver等类似软件中的 “插入对象”,则该类工具会自动生成classid,上面代码中classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" 表示要显示的文档是pdf格式
3.<object>的codebase属性
codebase一般是用来下载和更新组件,比如浏览某个网页时,发现您的机器上没有安装这个组件,就会去codebase的地方下载组件,有了新的版本也会提示您安装新版 本。