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

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

Introduction

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

Prototype

public Long getDocumentId() 

Source Link

Document

Provides the document ID.

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. j  av  a  2s. c  om*/

        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  .  ja v  a2  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 {
        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;//from  ww w .  j a va  2  s  .co  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);
    }
}