pdf在办公过程中,做矢量文档工具,使用比较广范。
在文档审批和发布过程中,也要求最后要pdf有效的签名或者印章,以示文档的重要性。
卓正软件公司的pageOffice,专门实现办公文档的在线编辑,在办公文档编辑领域,提供的插件比较丰富。为做办公系统的程序员节约了大量时间。最新的5.0版本,推出了pdf的在线签章功能。
实现了pdf签发的全流程处理。
本程序员实现了一个最简单的pdf签章的功能。
项目的结构如下图
第一步
pom.xml
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>PDFsealname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.xerialgroupId>
<artifactId>sqlite-jdbcartifactId>
<version>3.7.2version>
dependency>
<dependency>
<groupId>com.zhuozhengsoftgroupId>
<artifactId>pageofficeartifactId>
<version>5.1.0.2version>
dependency>
dependencies>
project>
2appplication.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= 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
<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>Indextitle>
<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
<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">
<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
<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">
<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启动服务
点 打开pdf盖章
https://github.com/qingxue0606/PDFseal
有问题可访问 http://www.zhuozhengsoft.com/