use of com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature 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;
}
use of com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature in project PdfBox-Android by TomRoush.
the class FDFCatalog method getSignature.
/**
* This will get the signature or null if there is none.
*
* @return The signature.
*/
public PDSignature getSignature() {
PDSignature signature = null;
COSDictionary sig = (COSDictionary) catalog.getDictionaryObject(COSName.SIG);
if (sig != null) {
signature = new PDSignature(sig);
}
return signature;
}
use of com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.PDSignature in project PdfBox-Android by TomRoush.
the class PDVisibleSigBuilder method createSignature.
@Override
public void createSignature(PDSignatureField pdSignatureField, PDPage page, String signerName) throws IOException {
PDSignature pdSignature = new PDSignature();
PDAnnotationWidget widget = pdSignatureField.getWidgets().get(0);
pdSignatureField.setValue(pdSignature);
widget.setPage(page);
page.getAnnotations().add(widget);
if (!signerName.isEmpty()) {
pdSignature.setName(signerName);
}
pdfStructure.setPdSignature(pdSignature);
Log.i("PdfBox-Android", "PDSignature has been created");
}
Aggregations