Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package th.in.oneauthen.signing;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.pdf.PdfReader;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.util.Arrays;
- import java.util.Date;
- import th.in.oneauthen.object.LogReportDB;
- import th.in.oneauthen.object.UserUidDB;
- import th.in.oneauthen.object.DAO.LogReportDAO;
- import th.teda.ETSITS102778.PAdES;
- import th.teda.ETSITS102778.PAdESSignatureAppearance;
- /**
- *
- * @author paradorn
- */
- public class SignatureUtil {
- private final SignatureProfile profile;
- private String timestampURL = "";
- public static final String ALGORITHM = "sha256withrsa";
- public SignatureUtil(SignatureProfile profile, String timestampURL){
- this.profile = profile;
- this.timestampURL = timestampURL;
- }
- public byte[] SignTheDocument(byte[] pdfData){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
- PAdESSignatureAppearance sap=null;
- if (profile.isIsVisible()){
- PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
- sap = new PAdESSignatureAppearance(reader);
- sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
- float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
- // System.out.println(Arrays.toString(sigCoordinate));
- if (profile.getSigImg()!=null) {
- sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
- } else {
- sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
- }
- }
- PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
- return baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public byte[] SignTheDocumentAPI(byte[] pdfData, UserUidDB user, int sigProfile_id){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
- PAdESSignatureAppearance sap=null;
- if (profile.isIsVisible()){
- PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
- sap = new PAdESSignatureAppearance(reader);
- sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
- float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
- // System.out.println(Arrays.toString(sigCoordinate));
- if (profile.getSigImg()!=null) {
- sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
- } else {
- sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
- }
- }
- PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
- return baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- String issue = e.toString();
- LogReportDAO saveLogDao = new LogReportDAO();
- LogReportDB saveLog = new LogReportDB();
- saveLog.setCreator(user);
- saveLog.setTimeSign(new Date());
- saveLog.setStatus("fail");
- saveLog.setTotal(1);
- saveLog.setType("ApiResful");
- saveLog.setIssue(issue);
- saveLog.setSignatureId(sigProfile_id);
- try {
- saveLogDao.save(saveLog);
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- return null;
- }
- public byte[] SignTheDocumentAPIforOne(byte[] pdfData, UserUidDB user, int sigProfile_id,String service_name){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try {
- PAdESSignatureAppearance sap=null;
- if (profile.isIsVisible()){
- PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
- sap = new PAdESSignatureAppearance(reader);
- sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
- float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
- // System.out.println(Arrays.toString(sigCoordinate));
- if (profile.getSigImg()!=null) {
- sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
- } else {
- sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
- }
- }
- PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
- return baos.toByteArray();
- } catch (Exception e) {
- e.printStackTrace();
- String issue = e.toString();
- LogReportDAO saveLogDao = new LogReportDAO();
- LogReportDB saveLog = new LogReportDB();
- saveLog.setCreator(user);
- saveLog.setTimeSign(new Date());
- saveLog.setStatus("fail");
- saveLog.setTotal(1);
- saveLog.setType("ApiResful");
- saveLog.setIssue(issue);
- saveLog.setSignatureId(sigProfile_id);
- saveLog.setTypeService(service_name);
- try {
- saveLogDao.save(saveLog);
- } catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- return null;
- }
- public float[] tranformSignatureCoordinate (Rectangle page, float[] percentageCoordinate) {
- float[] signatureCoordinate = new float[4];
- float pageH = (float) page.getHeight();
- float pageW = (float) page.getWidth();
- //llx
- signatureCoordinate[0] = (float) (pageW * percentageCoordinate[0] ); // page sig x0 value
- //urx
- signatureCoordinate[1] = (float) (pageW * (percentageCoordinate[0] + percentageCoordinate[1]) ); // page sig x1 value
- //lly
- signatureCoordinate[2] = (float) (pageH * (1-(percentageCoordinate[2]+percentageCoordinate[3]))); // page sig y0 value
- //ury
- signatureCoordinate[3] = (float) (pageH * (1-percentageCoordinate[2])); // page sig y1 value
- return signatureCoordinate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement