PageOffice在线预览word/excel/ppt/pdf

本文档介绍如何利用PageOffice集成Thymeleaf,在线预览Word、Excel、PPT和PDF文件。涉及步骤包括创建HTML页面、配置Thymeleaf和PageOffice许可证,添加依赖以及设置Controller层。
部署运行你感兴趣的模型镜像

因为PageOffice集成了Thymeleaf,所以需要在resources目录下新建如下页面
在这里插入图片描述
Index.html点击预览操作页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Index</title>
        <script type="text/javascript" src="jquery.min.js"></script>
     	<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>

    </head>
    <body>
        <h1 th:inline="text">文件预览</h1>
        <a href="javascript:POBrowser.openWindowModeless('/word?filepath=/opt/oa/enclosure/notice/1.doc','width=1200px;height=800px;');">打开文件 </a>
    </body>
</html>

Word.html预览页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Hello World!</title>
        <script type="text/javascript">
          	function Save() {
              	document.getElementById("PageOfficeCtrl1").WebSave();
        	}
        </script>
        <script type="text/javascript">
         function AddSeal() {
			try{
        		  document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
			}catch (e){ };
        	}
  		</script>
        
    </head>
    <body>
        <h1 th:inline="text"></h1>
        <div style="width:1000px;height:700px;" th:utext="${pageoffice}"></div>
    </body>
</html>

在application.yml中配置thymleaf和PageOffice的许可证书和印章密码

spring:
  application:
    name: test
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
posyspath: /home/version
popassword: 111111

在pom中添加所需依赖

 <!-- 添加Sqlite依赖(可选:如果不需要使用印章功能的话,不需要添加此依赖 )-->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>
    <!-- 添加PageOffice依赖(必须) -->
    <dependency>
        <groupId>com.zhuozhengsoft</groupId>
        <artifactId>pageoffice</artifactId>
        <version>4.4.0.4</version>
    </dependency>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

Controller层

import com.zhuozhengsoft.pageoffice.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Map;
        
/**
 * PageOffice在线预览
 */
@RestController
public class PageOfficeController {
    private Logger logger = LoggerFactory.getLogger(PageOfficeController.class);

    @Value("${posyspath}")
    private String poSysPath;
    @Value("${popassword}")
    private String poPassWord;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView showIndex() {
        ModelAndView mv = new ModelAndView("Index");
        return mv;
    }

    /**
     * 打开文件
     *
     * @return
     */
    @RequestMapping("/word")
    public ModelAndView word(HttpServletRequest request, Map<String, Object> map, @RequestParam("filepath") String filepath) {
        //word、excel、ppt服务组件
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage("/poserver.zz");//设置服务页面
        poCtrl.setTitlebar(true); // 隐藏标题栏(pageoffice的标题)
        poCtrl.setMenubar(false); // 隐藏菜单栏(文件一个设置)
        poCtrl.setOfficeToolbars(false);// 隐藏Office工具条(word 的编辑按钮)
        poCtrl.setCustomToolbar(false);// 隐藏自定义工具栏(保存 关闭 全屏)
        //poCtrl.addCustomToolButton("保存","Save",1);//添加自定义保存按钮
        //poCtrl.addCustomToolButton("盖章","AddSeal",2);//添加自定义盖章按钮
        //poCtrl.setSaveFilePage("/save");//设置处理文件保存的请求方法

        //pdf服务组件
        PDFCtrl pdfCtrl = new PDFCtrl(request);
        pdfCtrl.setServerPage("/poserver.zz");
        pdfCtrl.setTitlebar(true); // 隐藏标题栏(pageoffice的标题)
        pdfCtrl.setMenubar(false); // 隐藏菜单栏(文件一个设置)
        pdfCtrl.setCustomToolbar(false);// 隐藏自定义工具栏(保存 关闭 全屏)

        //String path = path.replace("/", "\\\\");//windows路径转换 Windows下路径d:\\file\\test.doc Linux下路径"file://" + /home/mobileoa/files2019_05_28/test.doc
        String path = "file://"+filepath;
        logger.info("路径" + path);
        String suffix = path.substring(path.lastIndexOf(".") + 1);
        if ("doc".equals(suffix) || "docx".equals(suffix)||"DOC".equals(suffix) || "DOCX".equals(suffix)) {
            poCtrl.webOpen(path, OpenModeType.docReadOnly, "张三");
            map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        } else if ("xls".equals(suffix) || "xlsx".equals(suffix)||"XLS".equals(suffix) || "XLSX".equals(suffix)) {
            poCtrl.webOpen(path, OpenModeType.xlsReadOnly, "张三");
            map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        } else if ("ppt".equals(suffix) || "pptx".equals(suffix)||"PPT".equals(suffix) || "PPTX".equals(suffix)) {
            poCtrl.webOpen(path, OpenModeType.pptReadOnly, "张三");
            map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        } else if ("pdf".equals(suffix)||"PDF".equals(suffix)) {
            pdfCtrl.webOpen(path);
            map.put("pageoffice", pdfCtrl.getHtmlCode("PageOfficeCtrl1"));
        }

        ModelAndView mv = new ModelAndView("Word");
        return mv;
    }

/**
 * 保存
 *
 * @return
 */
@RequestMapping("/save")
public void saveFile(HttpServletRequest request, HttpServletResponse response) {
    FileSaver fs = new FileSaver(request, response);
    fs.saveToFile("d:\\" + fs.getFileName());
    fs.close();
}


/**
 * 添加PageOffice的服务器端授权程序Servlet(必须)
 *
 * @return
 */
@Bean
public ServletRegistrationBean servletRegistrationBean() {
    com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
    poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录
    ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
    srb.addUrlMappings("/poserver.zz");
    srb.addUrlMappings("/posetup.exe");
    srb.addUrlMappings("/pageoffice.js");
    srb.addUrlMappings("/jquery.min.js");
    srb.addUrlMappings("/pobstyle.css");
    srb.addUrlMappings("/sealsetup.exe");
    return srb;
}

/**
 * 添加印章管理程序Servlet(可选)
 *
 * @return
 */
@Bean
public ServletRegistrationBean servletRegistrationBean2() {
    com.zhuozhengsoft.pageoffice.poserver.AdminSeal adminSeal = new com.zhuozhengsoft.pageoffice.poserver.AdminSeal();
    adminSeal.setAdminPassword(poPassWord);//设置印章管理员admin的登录密码
    adminSeal.setSysPath(poSysPath);//设置印章数据库文件poseal.db存放的目录
    ServletRegistrationBean srb = new ServletRegistrationBean(adminSeal);
    srb.addUrlMappings("/adminseal.zz");
    srb.addUrlMappings("/sealimage.zz");
    srb.addUrlMappings("/loginseal.zz");
    return srb;
}

}

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值