不能单独运行,仅限小记。
package com.huajin.usercenterclient.vo;
import java.io.Serializable;
public class SealRequest implements Serializable {
private static final long serialVersionUID = 462692688751689745L;
private String pdfPath;
private String targetPath;
private int sealInfoId;
private String userName;
private String sealLocation;
private String sealReason;
private String keyword;
private int offsetX;
private int offsetY;
private int page;
private int lx;
private int ly;
private int fromPage = 1;
/**
* get和set方法忽略
*/
}
public void sealPdfByCoordinate(SealRequest request) {
sealService.sealPdfByCoordinate(request.getPdfPath(), request.getTargetPath(), request.getSealInfoId(),
request.getUserName(), request.getSealLocation(), request.getSealReason(), request.getPage(),
request.getLx(), request.getLy());
}
public void sealPdfByCoordinate(String pdfPath, String targetPath, int sealInfoId, String userName, String sealLocation, String sealReason, int page, int lx, int ly) {
try {
chkPdfFileAvailable(pdfPath);
File targetFile = new File(FilenameUtils.concat(targetPath, FilenameUtils.getName(pdfPath)));
targetFile.getParentFile().mkdirs();
chkTargetPathAvailable(targetPath);
//调用签章方法
byte[] pdfByCoordinate = this.sealPdfByCoordinate(pdfPath, sealInfoId, userName, sealLocation, sealReason, page, lx, ly);
//参数为:保存的路径,调用签章方法返回的字节数据;wirteDataToFile是cfca.seal.util 包中提供的方法,用于保存该字节数据到指定路径下。
FileUtil.wirteDataToFile(FilenameUtils.concat(targetPath, FilenameUtils.getName(pdfPath)), pdfByCoordinate);
} catch (Exception ex) {
Xlogger.error(XMsgError.buildSimple(getClass().getName(), "sealPdfByCoordinate", ex));
throw new BayMaxBaseException(ResponseVo.FAIL, ex);
}
}
/**
* 根据坐标给PDF 文件签章
*
* @param pdfFile 签章文件
* @param sealInfoId 签章ID
* @param userName 盖章人
* @param sealLocation 盖章地点
* @param sealReason 盖章原因
* @param page 第几页
* @param x 盖章X坐标
* @param y 盖章Y坐标
* @return 已签章的文件
* @throws Exception
*/
public byte[] sealPdfByCoordinate(String pdfFile, int sealInfoId, String userName, String sealLocation, String sealReason, int page, int x, int y) {
try {
byte[] pdfFileByte = FileUtil.getBytesFromFile(new File(pdfFile));
if(page<=1) {
page = 1;
} else {
int pdfPageCnt = this.getPdfPageCount(pdfFileByte);
if (page > pdfPageCnt) {
page = pdfPageCnt;
}
}
SealInfoPo sealInfoPo = sealInfoDao.getById(sealInfoId);
SealUserInfo sealUserInfo = new SealUserInfo(userName, sealLocation, sealReason);
SignLocation signLocation = new SignLocation(page, x, y);
//签章的调用
byte[] sealedPdf = sealPdf(pdfFileByte, sealInfoPo.getSealCode(), sealInfoPo.getSealPassword(), sealUserInfo, signLocation);
if (StringUtil.checkResultDataValid(sealedPdf)) {
return sealedPdf;
} else {
throw new BayMaxBaseException(ResponseVo.FAIL, null, StringUtil.getNodeText(new String(sealedPdf), "ErrorMessage"));
}
} catch (Exception ex) {
Xlogger.error(XMsgError.buildSimple(getClass().getName(), "sealPdfByCoordinate", ex));
throw new BayMaxBaseException(ResponseVo.FAIL, ex, "对PDF文件签章时发生异常 " + ex.getMessage());
}
}
/**
* 对PDF文件签章
*
* @param pdf 文件字节流
* @param sealCode 签章编码
* @param sealPassword 签章密码
* @param sealUserInfo 签章用户信息
* @param signLocation 签章位置信息
* @return 已签章的文件字节流
* @throws Exception
*/
private byte[] sealPdf(byte[] pdf, String sealCode, String sealPassword, SealUserInfo sealUserInfo, SignLocation signLocation) throws Exception {
String sealStrategyXML = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, "", "", sealUserInfo, signLocation);
return this.sealClient.sealAutoPdf(pdf, sealStrategyXML);
}