java springboot实现pdf在线加盖电子印章和签字的功能

原文地址:https://blog.csdn.net/weixin_38757817/article/details/109092774

为什么要使用pdf签章

pdf在办公过程中,做矢量文档工具,使用比较广范。
在文档审批和发布过程中,也要求最后要pdf有效的签名或者印章,以示文档的重要性。

什么插件能实现pdf签章功能

卓正软件公司的pageOffice,专门实现办公文档的在线编辑,在办公文档编辑领域,提供的插件比较丰富。为做办公系统的程序员节约了大量时间。最新的5.0版本,推出了pdf的在线签章功能。
实现了pdf签发的全流程处理。

spirngboot如果集成pageOffice并实现pdf签章呢

本程序员实现了一个最简单的pdf签章的功能。

项目的结构如下图:

1. pom文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PDFseal</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- 添加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>5.1.0.2</version>

        </dependency>
    </dependencies>


</project>

2. appplication.properties 文档的设置。

########################################################
###PageOffice
########################################################
posyspath=d:/lic/
popassword=111111
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh

spring.thymeleaf.cache=false

3. 写入pageOffice定义的bean
DemoController.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;

/**
 * @author Administrator
 */
@RestController
public class DemoController {
    @Value("${posyspath}")
    private String poSysPath;

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

    /**
     * 添加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;// 
    }
}

4开始写后端业务代码。
PDFInsertSealController.java

package com.example.demo;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.PDFCtrl;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
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.FileNotFoundException;
import java.util.Map;

@RestController
public class PDFInsertSealController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView showIndex() {
        ModelAndView mv = new ModelAndView("Index");
        return mv;
    }
//盖章功能
    @RequestMapping(value = "/AddSeal", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map) {
        PDFCtrl pdfCtrl1 = new PDFCtrl(request);
        pdfCtrl1.setServerPage(request.getContextPath() + "/poserver.zz"); //此行必须
        //设置保存页面
        pdfCtrl1.setSaveFilePage("/AddSeal/save");
        // Create custom toolbar
        pdfCtrl1.addCustomToolButton("保存", "Save()", 1);
        pdfCtrl1.addCustomToolButton("加盖印章", "InsertSeal()", 2);
        //此处路径为服务器端文档路径
        pdfCtrl1.webOpen("D:\\doc\\AddSeal\\test1.pdf");
        map.put("pageoffice", pdfCtrl1.getHtmlCode("PDFCtrl1"));
        ModelAndView mv = new ModelAndView("/AddSeal/PDF1");
        return mv;
    }

//签字功能
    @RequestMapping(value = "AddSign", method = RequestMethod.GET)
    public ModelAndView showWord11(HttpServletRequest request, Map<String, Object> map) {
        PDFCtrl pdfCtrl1 = new PDFCtrl(request);
        pdfCtrl1.setServerPage(request.getContextPath() + "/poserver.zz"); //此行必须
        //设置保存页面
        pdfCtrl1.setSaveFilePage("/AddSign/save");
        // Create custom toolbar
        pdfCtrl1.addCustomToolButton("保存", "Save()", 1);
        pdfCtrl1.addCustomToolButton("签字", "AddHandSign()", 3);
        //此处路径为服务器端文档路径
        pdfCtrl1.webOpen("D:\\doc\\AddSign\\test1.pdf");
        map.put("pageoffice", pdfCtrl1.getHtmlCode("PDFCtrl1"));
        ModelAndView mv = new ModelAndView("/AddSign/PDF1");
        return mv;
    }


    @RequestMapping("/AddSeal/save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        //此处路径为服务器端文档路径
        fs.saveToFile(  "D:/doc/AddSeal/" + fs.getFileName());
        fs.close();
    }

    @RequestMapping("/AddSign/save")
    public void save2(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        //此处路径为服务器端文档路径
        fs.saveToFile(  "D:/doc/AddSign/" + fs.getFileName());
        fs.close();
    }

}

最后
写上三个对应的thymleleaf的模板

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="pageoffice.js" ></script>

</head>

<body>
<h1 th:inline="text">PageOffice 集成效果演示</h1>

<div style="width: 49%;float: left ">


    <a href="javascript:POBrowser.openWindowModeless('AddSeal','fullscreen=yes;');">打开pdf盖章 </a><br>

    <a href="javascript:POBrowser.openWindowModeless('AddSign','fullscreen=yes;');">打开pdf签字</a><br>


</div>



</body>
</html>

templates\AddSeal\PDF1.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>
</head>
<body style="overflow:hidden">
<!--**************   卓正 PageOffice 客户端代码开始    ************************-->
<script language="javascript" type="text/javascript">
    function Save() {
        document.getElementById("PDFCtrl1").WebSave();
    }

    function InsertSeal() {
        try {
            document.getElementById("PDFCtrl1").ZoomSeal.AddSeal();//如果使用ZoomSeal中的USBKEY方式盖章,第一个参数不能为盖章用户登录名,只能为null或者空字符串
        } catch(e) {}
    }


</script>
<div style="height:850px;width:auto;" th:utext="${pageoffice}">

</div>
</body>

</html>

templates\AddSign\PDF1.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>
</head>
<body style="overflow:hidden">
<!--**************   卓正 PageOffice 客户端代码开始    ************************-->
<script language="javascript" type="text/javascript">
    function Save() {
        document.getElementById("PDFCtrl1").WebSave();
    }

    function AddHandSign() {
        try {
            document.getElementById("PDFCtrl1").ZoomSeal.AddHandSign();
        } catch(e) {}
    }

</script>
<div style="height:850px;width:auto;" th:utext="${pageoffice}">

</div>
</body>
</html>

代码写完后,配置环境

1电脑创建d:/lic/ 这个目录,里面添加pageOffice自带的数据库。
2创建

                   

目录结构,每个目录放入一个test1.pdf的pdf文档。
3启动服务

最终效果

index页面打开效果

点 打开pdf盖章

盖章后的效果

签字后的效果
git项目地址为

https://github.com/qingxue0606/PDFseal

需要的,数据库文档,和pdf文档都在data目录中。

有问题可访问 http://www.zhuozhengsoft.com/

 

posted @ 2020-10-19 15:09  ziwuxian  阅读(7055)  评论(1编辑  收藏  举报