java integrated pageoffice to open word file online, and set bookmark text color and background color with js

Posted by renj0806 on Wed, 08 Jan 2020 05:49:50 +0100

1, Core code

1: First you need to navigate to the location of the bookmark

2: To set the text color and background color of bookmark, vba macro is needed.

2, Specific implementation process

1. Download the integration file, introduce the jar package and configure the web.xml on the official website http://www.zhuzhengsoft.com/dowm/

Go to the integration file you just downloaded and find lib. Put the contents in the Lib of Web inf project and introduce the jar package. Then, introduce the pageoffice configuration of web.xml into the wb.xml of your project

2. Place an a tag or button on the parent page index.jsp (the page that needs to open the document)

Import the js file required by pageoffice before writing a tag

<script type="text/javascript" src="/pageoffice.js"></script>

Then add a tag

<a href="javascript:POBrowser.openWindowModeless('Word.jsp' , 'width=1200px;height=800px;');">Open file</a>

3. Create a file named Word.jsp under the same level directory of the parent page

<%@ page language="java"
         import="java.util.*,com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"
         pageEncoding="UTF-8"%>
<%

    PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
    //Set up server page
    poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
    //Add custom button
    poCtrl.addCustomToolButton("Set bookmark color", "setColor()", 1);
    poCtrl.addCustomToolButton("Set bookmark background color", "setBackground()", 1);
    //Open Word document
    poCtrl.webOpen("test.doc", OpenModeType.docNormalEdit, "Zhang Yi Ming");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<script type="text/javascript">
    function setColor() {
        //Locate the selected bookmark content
        document.getElementById("PageOfficeCtrl1").DataRegionList.GetDataRegionByName("PO_userName").Locate();
        //Set font color for text in bookmarks
        document.getElementById("PageOfficeCtrl1").RunMacro("myFunc2", 'Function myFunc2() \r\n  Selection.Font.Color = -654245889 \r\n End Function');
    }
    function setBackground() {
        //Locate the selected bookmark content
        document.getElementById("PageOfficeCtrl1").DataRegionList.GetDataRegionByName("PO_deptName").Locate();
        //Set bookmark background color
        document.getElementById("PageOfficeCtrl1").RunMacro("myFunc2", 'Function myFunc2() \r\n Selection.Range.HighlightColorIndex = wdYellow \r\n End Function');
    }

</script>
<div style="height: 850px; width: 1036px;">
    <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
</div>
</body>
</html>

4. Under the same level directory of Word.jsp file, create a new word file named test.doc (if the new one is a docx file, the third step will be

poCtrl1.webOpen("test.doc", OpenModeType.docReadOnly, "Zhang San");

Change the code to poCtrl1.webOpen("test.docx", OpenModeType.docReadOnly, "Zhang San");

And set the corresponding bookmarks Po? Deptname and Po? Username

Start the project and directly visit aaa.jsp and click the link. At this time, you will be prompted to install the plug-in. After the installation is successful, you will be prompted to register, fill in the relevant information, and fill in the registration code CA1XB-MF7Y-12ST-PSBP2 to open the document

click

3, Final effect

 

Note: if you need more functions, you can go to the pageoffice official website to download the sample code and directly throw the samples4 folder into Tomcat's webapps, start tomcat, and visit http://localhost:8080/Samples4/index.html to see the specific effect and detailed code.

At the beginning of the contact with pageoffice, you can also watch the video quickly http://www.zhuozhengsoft.com/Technical/

Topics: Programming JSP xml Javascript Java