public void saveDocumentIncrementally( PAdESSignatureParameters parameters, File signedFile, FileOutputStream fileOutputStream, PDDocument pdDocument) throws DSSException { FileInputStream signedFileInputStream = null; try { signedFileInputStream = new FileInputStream(signedFile); // the document needs to have an ID, if not a ID based on the current system time is used, and // then the // digest of the signed data is // different if (pdDocument.getDocumentId() == null) { final byte[] documentIdBytes = DSSUtils.digest( DigestAlgorithm.MD5, parameters.bLevel().getSigningDate().toString().getBytes()); pdDocument.setDocumentId(DSSUtils.toLong(documentIdBytes)); pdDocument.setDocumentId(0L); } pdDocument.saveIncremental(signedFileInputStream, fileOutputStream); } catch (IOException e) { throw new DSSException(e); } catch (COSVisitorException e) { throw new DSSException(e); } finally { IOUtils.closeQuietly(signedFileInputStream); } }
@Override public void saveIncremental() throws IOException { try { document.saveIncremental(tempInput, tempOutput); tempOutput.close(); tempInput.close(); tempInput = new FileInputStream(tempDocumentOut); DSSUtils.copy(tempInput, output); tempInput.close(); } catch (COSVisitorException e) { throw new IOException(e); } }
@Override public void addDssDictionary( InputStream inputStream, OutputStream outpuStream, List<DSSDictionaryCallback> callbacks) { File toSignFile = null; File signedFile = null; FileInputStream fis = null; PDDocument pdDocument = null; try { toSignFile = DSSPDFUtils.getFileFromPdfData(inputStream); pdDocument = PDDocument.load(toSignFile); signedFile = File.createTempFile("sd-dss-", "-signed.pdf"); final FileOutputStream fileOutputStream = DSSPDFUtils.getFileOutputStream(toSignFile, signedFile); if (CollectionUtils.isNotEmpty(callbacks)) { final COSDictionary cosDictionary = pdDocument.getDocumentCatalog().getCOSDictionary(); cosDictionary.setItem("DSS", buildDSSDictionary(callbacks)); cosDictionary.setNeedToBeUpdate(true); } if (pdDocument.getDocumentId() == null) { pdDocument.setDocumentId(0L); } pdDocument.saveIncremental(inputStream, fileOutputStream); fis = new FileInputStream(signedFile); IOUtils.copy(fis, outpuStream); } catch (Exception e) { throw new DSSException(e); } finally { IOUtils.closeQuietly(pdDocument); IOUtils.closeQuietly(fis); DSSUtils.delete(toSignFile); DSSUtils.delete(signedFile); } }