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

发布时间 2023-06-01 13:47:50作者: 爱吃苹果皮

用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();
}

    <span class="hljs-comment">//另存为PDF文件</span>
    <span class="hljs-keyword">function</span> <span class="hljs-title function_">SaveAsPDF</span>(<span class="hljs-params"></span>) {
        <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">"PageOfficeCtrl1"</span>).<span class="hljs-title class_">WebSaveAsPDF</span>();
        <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">"PageOfficeCtrl1"</span>).<span class="hljs-title class_">Alert</span>(<span class="hljs-string">"PDF文件已经保存到 SaveAsPDF/doc目录下。"</span>);
        <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">"div1"</span>).<span class="hljs-property">innerHTML</span> = <span class="hljs-string">"&lt;a href='pdf?fileName=template.pdf'&gt; 查看另存的 pdf 文件&lt;a&gt;&lt;br&gt;&lt;br&gt;"</span>;
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

</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的功能

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