List of usage examples for org.apache.pdfbox.pdmodel PDDocument addSignature
public void addSignature(PDSignature sigObject, SignatureInterface signatureInterface) throws IOException
From source file:CreateSignature.java
License:Apache License
public void signDetached(PDDocument document, OutputStream output, TSAClient tsaClient) throws IOException { setTsaClient(tsaClient);/*from www . ja va 2 s . c o m*/ // create signature dictionary PDSignature signature = new PDSignature(); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED); signature.setName("Example User"); signature.setLocation("Los Angeles, CA"); signature.setReason("Testing"); // TODO extract the above details from the signing certificate? Reason as a parameter? // the signing date, needed for valid signature signature.setSignDate(Calendar.getInstance()); // register signature dictionary and sign interface document.addSignature(signature, this); // write incremental (only for signing purpose) document.saveIncremental(output); }
From source file:com.aaasec.sigserv.csspsupport.pdfbox.CreateSignature.java
License:EUPL
/** * Signs the given pdf file./*from w ww. ja v a 2s . c o m*/ */ public File signPDF() throws IOException, COSVisitorException, SignatureException { byte[] buffer = new byte[8 * 1024]; File document = new File(model.getOriginalLoc()); if (document == null || !document.exists()) { throw new RuntimeException("Document for signing does not exist"); } File outputDocument = new File(model.getSignedLoc()); FileInputStream fis = new FileInputStream(document); FileOutputStream fos = new FileOutputStream(outputDocument); int c; while ((c = fis.read(buffer)) != -1) { fos.write(buffer, 0, c); } fis.close(); fis = new FileInputStream(outputDocument); // load document PDDocument doc = PDDocument.load(document); // create signature dictionary PDSignature signature = new PDSignature(); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter // subfilter for basic and PAdES Part 2 signatures signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED); if (model.getSignerName() != null) { signature.setName(model.getSignerName()); } if (model.getSignerLocation() != null) { signature.setLocation(model.getSignerLocation()); } if (model.getReasonForSigning() != null) { signature.setReason(model.getReasonForSigning()); } // the signing date, needed for valid signature Calendar sigDate = Calendar.getInstance(); sigDate.setTime(new Date(model.getSigningAndIdTime())); signature.setSignDate(sigDate); // register signature dictionary and sign interface if (model.getOptions() == null) { doc.addSignature(signature, this); } else { doc.addSignature(signature, this, model.getOptions()); } // write incremental (only for signing purpose) PdfBoxSigUtil.saveIncremental(doc, fis, fos, model.getSigningAndIdTime()); return outputDocument; }
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxSignatureService.java
License:Open Source License
private byte[] signDocumentAndReturnDigest(final SignatureParameters parameters, final byte[] signatureBytes, final File signedFile, final FileOutputStream fileOutputStream, final PDDocument pdDocument, final PDSignature pdSignature, final DigestAlgorithm digestAlgorithm) throws DSSException { try {/*from w w w.ja v a 2s . c om*/ final MessageDigest digest = DSSUtils.getMessageDigest(digestAlgorithm); // register signature dictionary and sign interface SignatureInterface signatureInterface = new SignatureInterface() { @Override public byte[] sign(InputStream content) throws SignatureException, IOException { byte[] b = new byte[4096]; int count; while ((count = content.read(b)) > 0) { digest.update(b, 0, count); } return signatureBytes; } }; pdDocument.addSignature(pdSignature, signatureInterface); saveDocumentIncrementally(parameters, signedFile, fileOutputStream, pdDocument); final byte[] digestValue = digest.digest(); if (LOG.isDebugEnabled()) { LOG.debug("Digest to be signed: " + DSSUtils.encodeHexString(digestValue)); } fileOutputStream.close(); return digestValue; } catch (NoSuchAlgorithmException e) { throw new DSSException(e); } catch (IOException e) { throw new DSSException(e); } catch (SignatureException e) { throw new DSSException(e); } }