Search in sources :

Example 1 with SigningSupport

use of com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.SigningSupport in project PdfBox-Android by TomRoush.

the class PDDocument method saveIncrementalForExternalSigning.

/**
 * <p>
 * <b>(This is a new feature for 2.0.3. The API for external signing might change based on feedback after release!)</b>
 * <p>
 * Save PDF incrementally without closing for external signature creation scenario. The general
 * sequence is:
 * <pre>
 *    PDDocument pdDocument = ...;
 *    OutputStream outputStream = ...;
 *    SignatureOptions signatureOptions = ...; // options to specify fine tuned signature options or null for defaults
 *    PDSignature pdSignature = ...;
 *
 *    // add signature parameters to be used when creating signature dictionary
 *    pdDocument.addSignature(pdSignature, signatureOptions);
 *    // prepare PDF for signing and obtain helper class to be used
 *    ExternalSigningSupport externalSigningSupport = pdDocument.saveIncrementalForExternalSigning(outputStream);
 *    // get data to be signed
 *    InputStream dataToBeSigned = externalSigningSupport.getContent();
 *    // invoke signature service
 *    byte[] signature = sign(dataToBeSigned);
 *    // set resulted CMS signature
 *    externalSigningSupport.setSignature(signature);
 *
 *    // last step is to close the document
 *    pdDocument.close();
 * </pre>
 * <p>
 * Note that after calling this method, only {@code close()} method may invoked for
 * {@code PDDocument} instance and only AFTER {@link ExternalSigningSupport} instance is used.
 * </p>
 *
 * @param output stream to write the final PDF. It will be closed when the
 * document is closed. It <i><b>must never</b></i> point to the source file
 * or that one will be harmed!
 * @return instance to be used for external signing and setting CMS signature
 * @throws IOException if the output could not be written
 * @throws IllegalStateException if the document was not loaded from a file or a stream or
 * signature options were not set.
 */
public ExternalSigningSupport saveIncrementalForExternalSigning(OutputStream output) throws IOException {
    if (pdfSource == null) {
        throw new IllegalStateException("document was not loaded from a file or a stream");
    }
    // PDFBOX-3978: getLastSignatureDictionary() not helpful if signing into a template
    // that is not the last signature. So give higher priority to signature with update flag.
    PDSignature foundSignature = null;
    for (PDSignature sig : getSignatureDictionaries()) {
        foundSignature = sig;
        if (sig.getCOSObject().isNeedToBeUpdated()) {
            break;
        }
    }
    int[] byteRange = foundSignature.getByteRange();
    if (!Arrays.equals(byteRange, RESERVE_BYTE_RANGE)) {
        throw new IllegalStateException("signature reserve byte range has been changed " + "after addSignature(), please set the byte range that existed after addSignature()");
    }
    COSWriter writer = new COSWriter(output, pdfSource);
    writer.write(this);
    signingSupport = new SigningSupport(writer);
    return signingSupport;
}
Also used : COSWriter(com.tom_roush.pdfbox.pdfwriter.COSWriter) ExternalSigningSupport(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport) SigningSupport(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.SigningSupport) PDSignature(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)

Aggregations

COSWriter (com.tom_roush.pdfbox.pdfwriter.COSWriter)1 ExternalSigningSupport (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport)1 PDSignature (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature)1 SigningSupport (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.SigningSupport)1