用pageOffice控件实现 office word文档在线编辑另存为pdf的功能

用pageOffice控件实现 office word文档在线编辑另存为pdf的功能

1 应用场景

OA办公中,经常要将word文档转存为pdf方法,方式文档的查看。

怎么实现word文档的转存为pdf呢?

2 实现方法

通过pageOffice实现简单的在线打开编辑word后,只要增加一行
document.getElementById("PageOfficeCtrl1").WebSaveAsPDF();
就可以实现另存为pdf的功能

3 实现过程

以java的springboot框架为例

1 集成pageOffice

https://www.zhuozhengsoft.com/dowm/

image
从pageOffice官网
下载页面,找到springboot的集成示例,按照里面的集成明说,可以集成到自己的springboot项目中。

2 在线打开编辑word

image

可以按照这个示例首先实现最基本的打开word的方法。

3 通过代码实现word转pdf

代码参考以下功能示例代码
image

control代码

点击查看代码
 @RequestMapping(value = "Word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置服务页面
        //添加自定义按钮
        poCtrl.addCustomToolButton("保存", "Save()", 1);
        poCtrl.addCustomToolButton("另存为PDF文件", "SaveAsPDF()", 1);
        //设置保存页面
        poCtrl.setSaveFilePage("save");//设置处理文件保存的请求方法
        //打开Word文档
        poCtrl.webOpen("/doc/SaveAsPDF/template.doc", OpenModeType.docNormalEdit, "张三");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        ModelAndView mv = new ModelAndView("SaveAsPDF/Word");
        return mv;
		}

@RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "SaveAsPDF/" + fs.getFileName());
        fs.close();
    }

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>Word文件转换成PDF格式</title>
    <script type="text/javascript">
        //保存
        function Save() {
            document.getElementById("PageOfficeCtrl1").WebSave();
        }

        //另存为PDF文件
        function SaveAsPDF() {
            document.getElementById("PageOfficeCtrl1").WebSaveAsPDF();
            document.getElementById("PageOfficeCtrl1").Alert("PDF文件已经保存到 SaveAsPDF/doc目录下。");
            document.getElementById("div1").innerHTML = "<a href='pdf?fileName=template.pdf'> 查看另存的 pdf 文件<a><br><br>";
        }
    </script>

</head>
<body>
<form id="form1">
    <div id="div1"></div>
    <div style="width: auto; height: 700px;" th:utext="${pageoffice}">
    </div>
</form>
</body>

</html>

通过以上代码,可以实现word另存为pdf的功能。
生成的pdf可以在后端查看到。

4总结

用pageOffice控件实现 office word文档在线编辑另存为pdf的功能

posted on 2022-12-08 14:57  Tony636  阅读(669)  评论(0编辑  收藏  举报