pdf转Image 使用itextpdf、pdfbox

maven依赖:

package com.testdemo.util;import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;/**** PDF转图片**/
public class PdfToImage {/**** pdf转图片并返回图片路径* @param filePath pdf路径* @param page 转前几页的图片* @return List*/public static List pdfToImagePath(String filePath, int page) {List list = new ArrayList<>();String fileDirectory = filePath.substring(0, filePath.lastIndexOf("."));//获取去除后缀的文件路径String imagePath = null;PDDocument doc = null;clearPasswordUsed(filePath, "/home/cpm/"+filePath);try {File f = new File(fileDirectory);if (!f.exists()) {f.mkdir();}doc = PDDocument.load(new File(filePath));PDFRenderer renderer = new PDFRenderer(doc);int pageCount = doc.getNumberOfPages();if (pageCount > page) {pageCount = page;}for (int i = 0; i < pageCount; i++) {BufferedImage image = renderer.renderImageWithDPI(i, 100); //第二个参数越大生成图片分辨率越高,转换时间也就越长imagePath = fileDirectory + "/" + i + ".jpg";ImageIO.write(image, "PNG", new File(imagePath));list.add(imagePath);}} catch (Exception e) {e.printStackTrace();} finally {if (doc != null) {try {doc.close();} catch (IOException e) {e.printStackTrace();}}}return list;}/**** pdf文件免密* @param pdfPath 原文件路径* @param pdfPath2 新文件路径(不能覆盖原文件路径,否则文件内容会有错误)*/public static void clearPasswordUsed(String pdfPath,String pdfPath2) {PdfReader pdfReader = null;PdfStamper stamper = null;try {pdfReader = new PdfReader(pdfPath);//得到写的权限Field field = pdfReader.getClass().getDeclaredField("ownerPasswordUsed");field.setAccessible(true);//修改私有或者受保护的成员变量field.setBoolean(pdfReader, true);//免密stamper = new PdfStamper(pdfReader, new FileOutputStream(pdfPath2));} catch (Exception e) {e.printStackTrace();} finally {if (stamper != null) {try {stamper.close();} catch (Exception e) {e.printStackTrace();}}if (pdfReader != null) {pdfReader.close();}}}public static Rectangle getPDfProperty(String pdfFilePath) {PdfReader reader = null;Rectangle rectangle = null;try {reader = new PdfReader(pdfFilePath);rectangle = reader.getPageSize(1);} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {reader.close();}}return rectangle;}
}

 

package com.testdemo.biz;import com.hikvision.common.enums.BooleanEnum;
import com.hikvision.common.enums.SignatureScopeEnum;
import com.hikvision.common.enums.SignatureTypeEnum;
import com.hikvision.model.vo.SignatureVo;
import com.hikvision.util.FileSuffixUtil;
import com.hikvision.util.PdfToImage;
import com.itextpdf.text.Rectangle;
import com.timevale.esign.sdk.tech.bean.PosBean;
import com.timevale.esign.sdk.tech.bean.SignPDFFileBean;
import com.timevale.esign.sdk.tech.bean.result.FileDigestSignResult;
import com.timevale.esign.sdk.tech.impl.constants.SignType;
import com.timevale.esign.sdk.tech.service.SelfSignService;
import com.timevale.esign.sdk.tech.service.UserSignService;
import com.timevale.esign.sdk.tech.v3.client.ServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;/*** 电子签章Service* @Author:billjc24.hik* @Date:2018年8月7日*/
@Slf4j
@Service
public class SignatureBiz {/*** 账户*/@Value("${signature.accountId}")private String accountId;/*** 印章Id*/@Value("${signature.sealId}")private String sealId;/*** 印章base64数据*/@Value("${signature.sealData}")private String sealData;/*** 关键字*/@Value("${signature.keyWord}")private String keyWord;@Autowiredprivate ServiceClient serviceClient;public FileDigestSignResult doSignature(SignatureVo signatureVo) {//签章文件BeanSignPDFFileBean signPDFFileBean;//签章参数BeanPosBean posBean;//第二次签章参数BeanPosBean posBean2;//签章类型SignType signType = null;//签章结果FileDigestSignResult signResult;//pdf文件长宽Rectangle rectangle = PdfToImage.getPDfProperty(signatureVo.getFilePath());signPDFFileBean = new SignPDFFileBean();posBean = new PosBean();posBean2 = new PosBean();//原文件路径signPDFFileBean.setSrcPdfFile(signatureVo.getFilePath());//新文件路径signPDFFileBean.setDstPdfFile(signatureVo.getFilePath());//pdf长度低于1000,印章大小设置为159pxif(rectangle.getWidth()<=1000){posBean.setWidth(159F);}else{posBean.setWidth(rectangle.getWidth()*0.18F);}posBean2.setWidth(posBean.getWidth());//定位类型(关键字签章时为1)posBean.setPosType(0);posBean2.setPosType(0);if (SignatureTypeEnum.KEYWORK.getCode().equals(signatureVo.getSignatureType())) {signType = SignType.Key;posBean.setPosType(1);posBean.setKey(keyWord);return localSignPDF(signPDFFileBean, posBean, signType, accountId, sealId, sealData);} else if (SignatureTypeEnum.PAGE.getCode().equals(signatureVo.getSignatureType())) {signType = SignType.Multi;posBean.setPosPage("0");posBean.setPosX(rectangle.getWidth()/2);posBean.setPosY(rectangle.getHeight()/2);return localSignPDF(signPDFFileBean, posBean, signType, accountId, sealId, sealData);} else if (SignatureTypeEnum.SEAM.getCode().equals(signatureVo.getSignatureType())) {//尾页posBean.setPosType(0);posBean.setPosPage("-1");posBean.setPosX(rectangle.getWidth()/2);posBean.setPosY(rectangle.getHeight()/2);signResult = localSignPDF(signPDFFileBean, posBean, SignType.Single, accountId, sealId,sealData);if (signResult.getErrCode() != 0) {signatureVo.setSignResult(signResult);return signResult;}//骑缝posBean2.setPosPage("0");posBean2.setPosY(100);posBean2.setPosY(PdfToImage.getPDfProperty(signPDFFileBean.getSrcPdfFile()).getHeight() / 2);//针对需要连续多次签章的,SignPDFFileBean需要重新创建一个,否则第二次签章后的文件没有第一次签章的数据SignPDFFileBean signPDFFileBean2 = new SignPDFFileBean();signPDFFileBean2.setSrcPdfFile(signPDFFileBean.getSrcPdfFile());signPDFFileBean2.setDstPdfFile(signPDFFileBean.getDstPdfFile());return localSignPDF(signPDFFileBean2, posBean2, SignType.Edges, accountId, sealId,sealData);} else if (SignatureTypeEnum.MANUAL.getCode().equals(signatureVo.getSignatureType())) {//指定位置if (StringUtils.isNotEmpty(signatureVo.getPosition())) {String[] pos = StringUtils.split(signatureVo.getPosition(), ",");//获取pdf长宽float width = rectangle.getWidth();float height = rectangle.getHeight();//按百分比加上印章长度posBean.setPosX(width / 100 * Integer.parseInt(pos[0]) + posBean.getWidth() / 2 - 15);posBean.setPosY(height / 100 * Integer.parseInt(pos[1]) + posBean.getWidth() / 2 - 15);}//签章页面范围if (SignatureScopeEnum.ALL.getCode().equals(signatureVo.getScope())) {signType = SignType.Multi;posBean.setPosPage("0");} else if (SignatureScopeEnum.LAST.getCode().equals(signatureVo.getScope())) {signType = SignType.Single;posBean.setPosPage("-1");} else if (SignatureScopeEnum.PAGE.getCode().equals(signatureVo.getScope())) {signType = SignType.Multi;posBean.setPosPage(signatureVo.getPageStart() + "-" + signatureVo.getPageEnd());}signResult = localSignPDF(signPDFFileBean, posBean, signType, accountId, sealId,sealData);if (signResult.getErrCode() != 0) {return signResult;}//骑缝if (BooleanEnum.TRUE.getCode().equals(signatureVo.getPagingSeal())) {posBean2.setPosPage("0");posBean2.setPosY(PdfToImage.getPDfProperty(signPDFFileBean.getSrcPdfFile()).getHeight() / 2);SignPDFFileBean signPDFFileBean2 = new SignPDFFileBean();signPDFFileBean2.setSrcPdfFile(signPDFFileBean.getSrcPdfFile());signPDFFileBean2.setDstPdfFile(signPDFFileBean.getDstPdfFile());return localSignPDF(signPDFFileBean2, posBean2, SignType.Edges, accountId,sealId, sealData);}}return null;}/**** 签章* @param signPDFFileBean pdf文件Bean* @param posBean 印章位置Bean* @param signType 签章类型* @param accountId 客户Id* @param sealId 印章Id* @param sealData 印章base64数据* @return FileDigestSignResult*/private FileDigestSignResult localSignPDF(SignPDFFileBean signPDFFileBean,PosBean posBean, SignType signType, String accountId, String sealId, String sealData) {if (StringUtils.isNotEmpty(sealId)) {SelfSignService selfSignService = serviceClient.selfSignService();return selfSignService.localSignPdf(signPDFFileBean, posBean, Integer.parseInt(sealId), signType);} else {UserSignService userSignService = serviceClient.userSignService();return userSignService.localSignPDF(accountId, sealData, signPDFFileBean, posBean, signType);}}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部