1. 程式人生 > >java 實現 word 文件的線上編輯以及流轉

java 實現 word 文件的線上編輯以及流轉

用 pageoffice 實現文件的線上編輯以及文件流轉

1: 流程

用修改無痕跡模式起草檔案,各個領導批註自己意見的時候使用強制留痕模式開啟,文員清稿的時候用核稿模式開啟,還有最後只讀模式打開發布的正式檔案。其中在領導批註環節也演示了PageOffice提供的手寫功能,在檔案核稿之後可以加蓋印章。

2:詳細程式碼

<!-- PageOffice Begin -->
	<servlet>
		<servlet-name>poserver</servlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class>
	</servlet>

	
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/poserver.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/sealsetup.exe</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/posetup.exe</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/pageoffice.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/jquery.min.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/pobstyle.css</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>adminseal</servlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/adminseal.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/loginseal.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/sealimage.zz</url-pattern>
	</servlet-mapping>
	<mime-mapping>
		<extension>mht</extension>
		<mime-type>message/rfc822</mime-type>
	</mime-mapping>
	<context-param>
		<param-name>adminseal-password</param-name>
		<param-value>111111</param-value>
	</context-param>
	<!-- PageOffice End -->
<a href="word.jsp?type=creat">線上編輯</a> →
<a href="word.jsp?type=check&user=張三">張三批閱 </a> →
<a href="word.jsp?type=check&user=LiSi">李四批閱</a> →
<a href="word.jsp?type=accept">文員清稿</a> →
<a href="word.jsp?type=publish">正式發文</a>
<%@ page language="java"
	import="java.util.*,com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"
	pageEncoding="UTF-8"%>
<%@page import="java.awt.Color"%>
<%
	String type = (String) request.getParameter("type");
	String user = (String) request.getParameter("user");

	PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
	//設定伺服器頁面
	poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
	//新增自定義按鈕
	poCtrl.addCustomToolButton("儲存", "Save", 1);
	poCtrl.addCustomToolButton("全屏/還原", "IsFullScreen", 4);
	//設定儲存頁面
	poCtrl.setSaveFilePage("SaveFile.jsp");

	if ("creat".equals(type)) {
		//普通編輯模式開啟文件 開啟Word文件
		poCtrl.webOpen("doc/test.doc", OpenModeType.docNormalEdit,
				"張佚名");
	}
	if ("check".equals(type)) {
		poCtrl.addCustomToolButton("顯示/隱藏痕跡", "Show_HidRevisions", 5);
		poCtrl.addCustomToolButton("領導圈閱", "StartHandDraw", 3);
		poCtrl.addCustomToolButton("插入鍵盤批註", "StartRemark", 3);
		poCtrl.addCustomToolButton("分層顯示手寫批註", "ShowHandDrawDispBar",7);

		//強制留痕模式開啟文件 開啟Word文件
		poCtrl.webOpen("doc/test.doc", OpenModeType.docRevisionOnly,user);
	}
	if ("accept".equals(type)) {
		poCtrl.addCustomToolButton("顯示/隱藏痕跡", "Show_HidRevisions", 5);
		poCtrl.addCustomToolButton("插入印章/簽名", "InsertSeal", 2);
		poCtrl.addCustomToolButton("接受所有修訂", "AcceptAllRevisions", 5);
		poCtrl .addCustomToolButton("分層顯示手寫批註", "ShowHandDrawDispBar",7);
		//核稿模式開啟文件 開啟Word文件
		poCtrl.webOpen("doc/test.doc", OpenModeType.docAdmin, "張佚名");
	}
	if ("publish".equals(type)) {
		poCtrl.addCustomToolButton("另存到本地", "ShowDialog(0)", 5);
		poCtrl.addCustomToolButton("頁面設定", "ShowDialog(1)", 0);
		poCtrl.addCustomToolButton("列印", "ShowDialog(2)", 6);

		poCtrl.setMenubar(false);
		poCtrl.setOfficeToolbars(false);
		//只讀模式開啟文件 開啟Word文件
		poCtrl.webOpen("doc/test.doc", OpenModeType.docReadOnly, "張佚名");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
	<script type="text/javascript">
		//儲存
        function Save() {
            document.getElementById("PageOfficeCtrl1").WebSave();
        }
        
        //全屏/還原
        function IsFullScreen() {
            document.getElementById("PageOfficeCtrl1").FullScreen = !document.getElementById("PageOfficeCtrl1").FullScreen;
        }
		//顯示/隱藏痕跡
        function Show_HidRevisions() {
            document.getElementById("PageOfficeCtrl1").ShowRevisions = !document.getElementById("PageOfficeCtrl1").ShowRevisions;
        }
        
        //領導圈閱簽字
        function StartHandDraw() {
            document.getElementById("PageOfficeCtrl1").HandDraw.SetPenWidth(5);
            document.getElementById("PageOfficeCtrl1").HandDraw.Start();
        }
		// 插入鍵盤批註
        function StartRemark() {
            var appObj = document.getElementById("PageOfficeCtrl1").WordInsertComment();

        }	
        //分層顯示手寫批註
        function ShowHandDrawDispBar() {
            document.getElementById("PageOfficeCtrl1").HandDraw.ShowLayerBar(); ;
        }


        //接受所有修訂
        function AcceptAllRevisions() {
            document.getElementById("PageOfficeCtrl1").AcceptAllRevisions(); 
        }
        
        function ShowDialog(index) {
            if (index == 0) document.getElementById("PageOfficeCtrl1").ShowDialog(2);
            if (index == 1) document.getElementById("PageOfficeCtrl1").ShowDialog(5);
            if (index == 2) document.getElementById("PageOfficeCtrl1").ShowDialog(4);
        }
        
        //插入印章
        function InsertSeal(){
        	try {
				document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
			} catch(e) {}
        }
  
    </script>
	</head>
	<body>
		<div style="height: 850px; width: 1036px; overflow: hidden;">
			<%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
		</div>
	</body>
</html>

儲存程式碼

FileSaver fs=new FileSaver(request,response);
fs.saveToFile(request.getSession().getServletContext().getRealPath("doc/")+"/"+fs.getFileName());
fs.close();

3:注意事項

(1)此示例演示了簡單的檔案流轉,沒有返回. 如需返回重新編輯審閱文件,可以寫自己的邏輯

(2)本示例 用到的印章是pgaeoffice 自帶的簡易印章. 用的是 sqlite 資料庫. 必須複製sqlite 的jar 以及 poseal.db檔案. 可以到

http://www.zhuozhengsoft.com/dowm/  下載試用程式.裡面有對應的檔案