List of usage examples for com.itextpdf.text.pdf PdfSignatureAppearance setLayer2Text
public void setLayer2Text(String text)
From source file:com.isa.firma.FirmaPDFController.java
public ByteArrayOutputStream firmar(PDFFirma infoFirma, InputStream pdfbase64) throws AppletException { try {// w ww . j a v a 2 s. c o m System.out.println("Firma Controller::firmar"); PdfReader reader = new PdfReader(pdfbase64); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); System.out.println("Pre definir apariencia..."); if (infoFirma.isApariencia()) { System.out.println("Insertando apriencia en documento..."); appearance.setSignatureGraphic(Image.getInstance(new URL(infoFirma.getRutaImagen()))); appearance.setRenderingMode(Utiles.getModoApariencia()); int numeroPagFirma = infoFirma.getHoja() == -1 ? reader.getNumberOfPages() : infoFirma.getHoja(); int cantidadFirmaActuales = reader.getAcroFields().getSignatureNames().size(); int[] coords = infoFirma.calcularCorrdenadasFirma(cantidadFirmaActuales, infoFirma.getAncho(), infoFirma.getLargo()); //llx, lly, urx, ury String v = Utiles.encodingString(infoFirma.getFirmante()); System.out.println("Post encoding: " + v); appearance.setLayer2Text(v); //appearance.setLayer2Text( infoFirma.getFirmante() ); appearance.setVisibleSignature(new Rectangle(coords[0], coords[1], coords[2], coords[3]), numeroPagFirma, "Id: " + IdGenerator.generate()); } ExternalSignature es = new PrivateKeySignature(infoFirma.getPk(), "SHA-256", infoFirma.getProvidername()); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, es, infoFirma.getChainCert(), null, null, null, 0, CryptoStandard.CMS); System.out.println("PDF Firmado correctamente."); return os; } catch (IOException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (DocumentException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (KeyStoreException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (UnrecoverableKeyException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (GeneralSecurityException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } }
From source file:com.isa.firma.pades.FirmaPDFController.java
public ByteArrayOutputStream firmar(PDFFirma infoFirma, InputStream pdfbase64) throws AppletException { try {//from w ww. j a v a2 s .com PdfReader reader = new PdfReader(pdfbase64); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true); PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); if (infoFirma.isApariencia()) { System.out.println("Definiendo apariencia..."); appearance.setRenderingMode(Utiles.getModoApariencia()); if (!Utiles.getModoApariencia().equals(PdfSignatureAppearance.RenderingMode.DESCRIPTION)) { appearance.setSignatureGraphic(Image.getInstance(new URL(infoFirma.getRutaImagen()))); } int numeroPagFirma = infoFirma.getHoja() == -1 ? reader.getNumberOfPages() : infoFirma.getHoja(); int cantidadFirmaActuales = reader.getAcroFields().getSignatureNames().size(); int[] coords = infoFirma.calcularCorrdenadasFirma(cantidadFirmaActuales, infoFirma.getAncho(), infoFirma.getLargo()); System.out.println("firmante: " + infoFirma.getFirmante()); System.out.println("serie: " + infoFirma.getNroSerie()); //llx, lly, urx, ury appearance.setLayer2Text(infoFirma.generarTextoEnFirma()); appearance.setVisibleSignature(new Rectangle(coords[0], coords[1], coords[2], coords[3]), numeroPagFirma, "Id: " + IdGenerator.generate()); } ExternalSignature es = new PrivateKeySignature(infoFirma.getPk(), "SHA-256", infoFirma.getProvidername()); ExternalDigest digest = new BouncyCastleDigest(); MakeSignature.signDetached(appearance, digest, es, infoFirma.getChainCert(), null, null, null, 0, CryptoStandard.CMS); System.out.println("PDF Firmado correctamente."); return os; } catch (IOException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (DocumentException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (KeyStoreException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (UnrecoverableKeyException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } catch (GeneralSecurityException ex) { Logger.getLogger(FirmaPDFController.class.getName()).log(Level.SEVERE, null, ex); throw new AppletException(UtilesMsg.ERROR_FIRMANDO_DOCUMENTO, null, ex.getCause()); } }
From source file:de.sign.SignMain.java
License:Open Source License
public void sign() throws DocumentException, IOException, GeneralSecurityException { PdfReader reader = new PdfReader(this.orgFile); OutputStream os = new FileOutputStream(this.orgFile.replace(".pdf", "SIGN.pdf")); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); // Create appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); Rectangle cropBox = reader.getCropBox(1); float width = 50; float height = 50; Rectangle rectangle = new Rectangle(cropBox.getRight(width) - 20, cropBox.getTop(height) - 20, cropBox.getRight() - 20, cropBox.getTop() - 20); appearance.setVisibleSignature(rectangle, 1, "sig"); appearance.setLocation(getHostname()); appearance.setReason("Evidence of document integrity"); appearance.setCertificationLevel(1); // 1 = CERTIFIED_NO_CHANGES_ALLOWED appearance.setAcro6Layers(false);/*from w w w. jav a2 s. c o m*/ appearance.setLayer2Text(""); //Sign Security.addProvider(new BouncyCastleProvider()); TSAClient tsc = new TSAClientBouncyCastle(this.tsa_URL); ExternalDigest digest = new BouncyCastleDigest(); ExternalSignature signature = new PrivateKeySignature(getPrivateKey(), "SHA-1", "BC"); MakeSignature.signDetached(appearance, digest, signature, getCertificateChain(), null, null, tsc, 0, CryptoStandard.CMS); }
From source file:org.opencps.pki.PdfSigner.java
License:Open Source License
/** * Compute digest hash/*from ww w .jav a2 s . c om*/ */ protected byte[] computeDigest(float llx, float lly, float urx, float ury) throws SignatureException { byte digestHash[] = null; int contentEstimated = 8192; try { PdfReader reader = new PdfReader(getOriginFilePath()); FileOutputStream os = new FileOutputStream(getTempFilePath()); PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); signatureFieldName = appearance.getNewSigName(); TSAClient tsaClient = null; appearance.setCertificate(getCertificate()); String tsaUrl = CertificateUtil.getTSAURL(getCertificate()); if (tsaUrl != null) { tsaClient = new TSAClientBouncyCastle(tsaUrl); } if (tsaClient != null) { LtvTimestamp.timestamp(appearance, tsaClient, signatureFieldName); contentEstimated += 4096; } appearance.setSignDate(signDate); CertificateInfo certInfo = new CertificateInfo(getCertificate()); appearance.setLocation(certInfo.getOrganizationUnit()); appearance.setReason("Document is signed by " + certInfo.getCommonName()); appearance.setContact(certInfo.getCommonName()); if (!isVisible) { appearance.setVisibleSignature(new Rectangle(0, 0, 0, 0), 1, signatureFieldName); } else { if (signatureImage != null) { appearance.setSignatureGraphic(signatureImage.getImage()); appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC); } else { appearance.setLayer2Text(certInfo.getCommonName()); } appearance.setVisibleSignature(new Rectangle(llx, lly, urx, ury), 1, signatureFieldName); } ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED); MakeSignature.signExternalContainer(appearance, external, contentEstimated); digestHash = DigestAlgorithms.digest(appearance.getRangeStream(), digest.getMessageDigest(getHashAlgorithm().toString())); reader.close(); os.close(); } catch (Exception e) { throw new SignatureException(e.getMessage(), e); } return digestHash; }