Example usage for org.apache.pdfbox.pdmodel PDDocument setDocumentId

List of usage examples for org.apache.pdfbox.pdmodel PDDocument setDocumentId

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument setDocumentId.

Prototype

public void setDocumentId(Long docId) 

Source Link

Document

Sets the document ID to the given value.

Usage

From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxSignatureService.java

License:Open Source License

public static void saveDocumentIncrementally(SignatureParameters parameters, File signedFile,
        FileOutputStream fileOutputStream, PDDocument pdDocument) throws DSSException {

    FileInputStream signedFileInputStream = null;
    try {//from  ww  w .jav  a  2 s.  c  o  m

        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 {
        DSSUtils.closeQuietly(signedFileInputStream);
    }
}

From source file:eu.europa.esig.dss.pdf.pdfbox.PdfBoxSignatureService.java

License:Open Source License

public void saveDocumentIncrementally(PAdESSignatureParameters parameters, File signedFile,
        FileOutputStream fileOutputStream, PDDocument pdDocument) throws DSSException {

    FileInputStream signedFileInputStream = null;
    try {//w w w.j a v  a  2 s  .co  m

        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);
    }
}

From source file:eu.europa.esig.dss.pdf.pdfbox.PdfBoxSignatureService.java

License:Open Source License

@Override
public void addDssDictionary(InputStream inputStream, OutputStream outpuStream,
        List<DSSDictionaryCallback> callbacks) {
    File toSignFile = null;/*w w  w  .  j  a va2 s.  c o m*/
    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);
    }
}