1:描述
在线打开word文件,有时候需要一定的安全浏览的需求:比如,只读,禁止编辑;禁止拷贝,禁止另存,禁止打印等
2:方案
用 pageoffice 提供安全浏览的在线只读打开文档,再打开文档的事件中禁止另存,打印
或者把 word 文件转换成 pdf,然后在打开pdf。 pdf 的性质 和图片一样,是没办法修改编辑的,只能查看。
3:核心代码
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(4, false); //禁止另存
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(5, false); //禁止打印
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(6, false); //禁止页面设置
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(8, false); //禁止打印预览
poCtrl1.setAllowCopy(false);//禁止拷贝
4:具体实现过程
(1)在线打开文档
<%@ page language="java"
import="java.util.*,com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"
pageEncoding="utf-8"%>
<%
//******************************卓正PageOffice组件的使用*******************************
//设置PageOffice服务器组件
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
//添加自定义按钮
poCtrl1.addCustomToolButton("轉pdf ", "Save", 1);
//设置保存页面
poCtrl1.setSaveFilePage("SaveFile.jsp");
poCtrl1.setAllowCopy(false);//禁止拷贝
poCtrl1.setMenubar(false);//隐藏菜单栏
poCtrl1.setOfficeToolbars(false);//隐藏Office工具条
poCtrl1.setCustomToolbar(false);//隐藏自定义工具栏
poCtrl1.setJsFunction_AfterDocumentOpened("AfterDocumentOpened");
//设置页面的显示标题
poCtrl1.setCaption("演示:文件在线安全浏览");
//打开文件
poCtrl1.webOpen("d:\\aa.doc", OpenModeType.docReadOnly, "张三");//docReadOnly 只读打开文档
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>演示:文件在线安全浏览</title>
</head>
<body>
<script type="text/javascript">
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
function AfterDocumentOpened() {
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(4, false); //禁止另存
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(5, false); //禁止打印
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(6, false); //禁止页面设置
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(8, false); //禁止打印预览
}
</script>
<div style=" width:900px; height:700px;">
<%=poCtrl1.getHtmlCode("PageOfficeCtrl1")%>
</div>
</body>
</html>
(2)保存 pdf 文件
FileSaver fs=new FileSaver(request,response);
if(".pdf".equals(fs.getFileExtName())){
fs.saveToFile("D:\\aa.pdf");
}
if(".doc".equals(fs.getFileExtName())){
fs.saveToFile("D:\\aa.doc");
}
fs.close();
(3)打开pdf
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.zhuozhengsoft.pageoffice.PDFCtrl"%>
<%@page import="com.zhuozhengsoft.pageoffice.ThemeType"%>
<%
// 按键说明:光标键、Home、End、PageUp、PageDown可用来移动或翻页;数字键盘+、-用来放大缩小;数字键盘/、*用来旋转页面。
PDFCtrl pdfCtrl = new PDFCtrl(request);//定义PDFCtrl控件对象
pdfCtrl.setServerPage(request.getContextPath()+"/poserver.zz"); //设置服务器页面
//添加自定义按钮
pdfCtrl.addCustomToolButton("实际大小", "SetPageReal()", 16);
pdfCtrl.addCustomToolButton("适合页面", "SetPageFit()", 17);
pdfCtrl.addCustomToolButton("适合宽度", "SetPageWidth()", 18);
pdfCtrl.addCustomToolButton("首页", "FirstPage()", 8);
pdfCtrl.addCustomToolButton("上一页", "PreviousPage()", 9);
pdfCtrl.addCustomToolButton("下一页", "NextPage()", 10);
pdfCtrl.addCustomToolButton("尾页", "LastPage()", 11);
pdfCtrl.addCustomToolButton("左转", "RotateLeft()", 12);
pdfCtrl.addCustomToolButton("右转", "RotateRight()", 13);
pdfCtrl.addCustomToolButton("放大", "ZoomIn()", 14);
pdfCtrl.addCustomToolButton("缩小", "ZoomOut()", 15);
pdfCtrl.addCustomToolButton("全屏", "SwitchFullScreen()", 4);
//设置禁止拷贝
pdfCtrl.setAllowCopy(false);
pdfCtrl.webOpen("D:\\aa.pdf");//打开文件
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>打开PDF文件</title>
<!--************** 卓正 PageOffice 客户端代码开始 ************************-->
<script language="javascript" type="text/javascript">
function SwitchFullScreen() {
document.getElementById("PDFCtrl1").FullScreen = !document
.getElementById("PDFCtrl1").FullScreen;
}
function SetPageReal() {
document.getElementById("PDFCtrl1").SetPageFit(1);
}
function SetPageFit() {
document.getElementById("PDFCtrl1").SetPageFit(2);
}
function SetPageWidth() {
document.getElementById("PDFCtrl1").SetPageFit(3);
}
function ZoomIn() {
document.getElementById("PDFCtrl1").ZoomIn();
}
function ZoomOut() {
document.getElementById("PDFCtrl1").ZoomOut();
}
function FirstPage() {
document.getElementById("PDFCtrl1").GoToFirstPage();
}
function PreviousPage() {
document.getElementById("PDFCtrl1").GoToPreviousPage();
}
function NextPage() {
document.getElementById("PDFCtrl1").GoToNextPage();
}
function LastPage() {
document.getElementById("PDFCtrl1").GoToLastPage();
}
function RotateRight() {
document.getElementById("PDFCtrl1").RotateRight();
}
function RotateLeft() {
document.getElementById("PDFCtrl1").RotateLeft();
}
</script>
</head>
<body>
<form id="form1">
<div style="width: 900px; height: 700px;">
<%=pdfCtrl.getHtmlCode("PDFCtrl1")%>
</div>
</form>
</body>
</html>
(5)具体的效果可以看看卓正官网看看下面的两个的示例
http://www.pageoffice.cn:8080/Samples4/
注意:
如果需要更丰富的功能,大家可以去pageoffice官网下载示例代码直接将samples4文件夹扔到Tomcat的webapps下,启动Tomcat,浏览器访问http://localhost:8080/Samples4/index.html,看看具体的示例效果以及详细的代码。
也可以看看视频快速了解 pageoffice。