public class PdfSigner extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
PdfSigner.CryptoStandard
Enum containing the Cryptographic Standards.
|
static interface |
PdfSigner.ISignatureEvent
An interface to retrieve the signature dictionary for modification.
|
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
bout
The bytes of the file right before the signature is added (if raf is null).
|
protected boolean |
closed
Boolean to check if this PdfSigner instance has been closed already or not.
|
protected PdfSignature |
cryptoDictionary
The crypto dictionary.
|
protected PdfDocument |
document
The PdfDocument.
|
protected Map<PdfName,PdfLiteral> |
exclusionLocations
Name and content of keys that can only be added in the close() method.
|
protected OutputStream |
originalOS
OutputStream for the bytes of the document.
|
protected boolean |
preClosed
Indicates if the pdf document has already been pre-closed.
|
protected RandomAccessFile |
raf
The file right before the signature is added (can be null).
|
protected long[] |
range
Array containing the byte positions of the bytes that need to be hashed.
|
protected PdfSigner.ISignatureEvent |
signatureEvent
Holds value of property signatureEvent.
|
protected File |
tempFile
Tempfile to hold the output temporarily.
|
protected ByteArrayOutputStream |
temporaryOS
Outputstream that temporarily holds the output in memory.
|
| Constructor and Description |
|---|
PdfSigner(PdfReader reader,
OutputStream outputStream,
StampingProperties properties)
Creates a PdfSigner instance.
|
PdfSigner(PdfReader reader,
OutputStream outputStream,
String path,
StampingProperties properties)
Creates a PdfSigner instance.
|
PdfSigner(PdfReader reader,
OutputStream outputStream,
String path,
StampingProperties stampingProperties,
SignerProperties signerProperties)
Creates a PdfSigner instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addDeveloperExtension(PdfDeveloperExtension extension)
Add developer extension to the current
PdfDocument. |
protected void |
addDocMDP(PdfSignature crypto)
Adds keys to the signature dictionary that define the certification level and the permissions.
|
protected void |
addFieldMDP(PdfSignature crypto,
PdfSigFieldLock fieldLock)
Adds keys to the signature dictionary that define the field permissions.
|
protected void |
applyAccessibilityProperties(PdfFormField formField,
IAccessibleElement modelElement,
PdfDocument pdfDocument)
Applies
AccessibilityProperties for provided signature field. |
protected void |
close(PdfDictionary update)
This is the last method to be called when using external signatures.
|
protected PdfSigFieldLock |
createNewSignatureFormField(PdfAcroForm acroForm,
String name)
Creates new signature form field and adds it to the acroForm object.
|
protected boolean |
documentContainsCertificationOrApprovalSignatures()
Check if current document instance already contains certification or approval signatures.
|
PdfDocument |
getDocument()
Gets the PdfDocument associated with this instance.
|
String |
getNewSigFieldName()
Gets a new signature field name that doesn't clash with any existing name.
|
protected InputStream |
getRangeStream()
Gets the document bytes that are hashable when using external signatures.
|
protected SignatureFieldAppearance |
getSignatureAppearance()
Returns final signature appearance object set by
SignerProperties.setSignatureAppearance(SignatureFieldAppearance) and
customized using PdfSigner properties such as signing date, reason, location and signer name
in case they weren't specified by the user, or, if none was set, returns a new one with default appearance. |
PdfSignature |
getSignatureDictionary()
Returns the user made signature dictionary.
|
PdfSigner.ISignatureEvent |
getSignatureEvent()
Getter for property signatureEvent.
|
PdfSignatureFormField |
getSignatureField()
Gets the signature field to be signed.
|
SignerProperties |
getSignerProperties()
Gets the properties to be used in signing operations.
|
protected com.itextpdf.io.source.IRandomAccessSource |
getUnderlyingSource()
Returns the underlying source.
|
protected int |
getWidgetPageNumber(PdfWidgetAnnotation widget)
Get the page number associated to the provided widget.
|
protected Rectangle |
getWidgetRectangle(PdfWidgetAnnotation widget)
Get the rectangle associated to the provided widget.
|
protected PdfDocument |
initDocument(PdfReader reader,
PdfWriter writer,
StampingProperties properties)
Initialize new
PdfDocument instance by using provided parameters. |
protected boolean |
isPreClosed()
Checks if the document is in the process of closing.
|
protected PdfSigFieldLock |
populateExistingSignatureFormField(PdfAcroForm acroForm)
Populates already existing signature form field in the acroForm object.
|
protected void |
preClose(Map<PdfName,Integer> exclusionSizes)
This is the first method to be called when using external signatures.
|
protected Collection<byte[]> |
processCrl(Certificate cert,
Collection<ICrlClient> crlList)
Processes a CRL list.
|
protected void |
setDocument(PdfDocument document)
Sets the PdfDocument.
|
void |
setOriginalOutputStream(OutputStream originalOS)
Setter for the OutputStream.
|
void |
setSignatureEvent(PdfSigner.ISignatureEvent signatureEvent)
Sets the signature event to allow modification of the signature dictionary.
|
PdfSigner |
setSignerProperties(SignerProperties properties)
Sets the properties to be used in signing operations.
|
static void |
signDeferred(PdfDocument document,
String fieldName,
OutputStream outs,
IExternalSignatureContainer externalSignatureContainer)
Deprecated.
signDeferred(PdfReader, String, OutputStream, IExternalSignatureContainer)
should be used instead. |
static void |
signDeferred(PdfReader reader,
String fieldName,
OutputStream outs,
IExternalSignatureContainer externalSignatureContainer)
Signs a PDF where space was already reserved.
|
void |
signDetached(IExternalDigest externalDigest,
IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signDetached(IExternalDigest externalDigest,
IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype,
com.itextpdf.commons.bouncycastle.asn1.esf.ISignaturePolicyIdentifier signaturePolicy)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signDetached(IExternalDigest externalDigest,
IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype,
SignaturePolicyInfo signaturePolicy)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signDetached(IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signDetached(IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype,
com.itextpdf.commons.bouncycastle.asn1.esf.ISignaturePolicyIdentifier signaturePolicy)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signDetached(IExternalSignature externalSignature,
Certificate[] chain,
Collection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize,
PdfSigner.CryptoStandard sigtype,
SignaturePolicyInfo signaturePolicy)
Signs the document using the detached mode, CMS or CAdES equivalent.
|
void |
signExternalContainer(IExternalSignatureContainer externalSignatureContainer,
int estimatedSize)
Sign the document using an external container, usually a PKCS7.
|
void |
timestamp(ITSAClient tsa,
String signatureName)
Signs a document with a PAdES-LTV Timestamp.
|
protected RandomAccessFile raf
protected byte[] bout
protected long[] range
protected PdfDocument document
protected PdfSignature cryptoDictionary
protected PdfSigner.ISignatureEvent signatureEvent
protected OutputStream originalOS
protected ByteArrayOutputStream temporaryOS
protected File tempFile
protected Map<PdfName,PdfLiteral> exclusionLocations
protected boolean preClosed
protected boolean closed
public PdfSigner(PdfReader reader, OutputStream outputStream, StampingProperties properties) throws IOException
ByteArrayOutputStream instead of a temporary file.reader - PdfReader that reads the PDF fileoutputStream - OutputStream to write the signed PDF fileproperties - StampingProperties for the signing document. Note that encryption will be
preserved regardless of what is set in properties.IOException - if some I/O problem occurspublic PdfSigner(PdfReader reader, OutputStream outputStream, String path, StampingProperties stampingProperties, SignerProperties signerProperties) throws IOException
ByteArrayOutputStream instead of a temporary file.reader - PdfReader that reads the PDF fileoutputStream - OutputStream to write the signed PDF filepath - File to which the output is temporarily writtenstampingProperties - StampingProperties for the signing document. Note that encryption will be
preserved regardless of what is set in properties.signerProperties - SignerProperties bundled properties to be used in signing operations.IOException - if some I/O problem occurspublic PdfSigner(PdfReader reader, OutputStream outputStream, String path, StampingProperties properties) throws IOException
ByteArrayOutputStream instead of a temporary file.reader - PdfReader that reads the PDF fileoutputStream - OutputStream to write the signed PDF filepath - File to which the output is temporarily writtenproperties - StampingProperties for the signing document. Note that encryption will be
preserved regardless of what is set in properties.IOException - if some I/O problem occursprotected PdfDocument initDocument(PdfReader reader, PdfWriter writer, StampingProperties properties)
PdfDocument instance by using provided parameters.reader - PdfReader to be used as a reader in the new documentwriter - PdfWriter to be used as a writer in the new documentproperties - StampingProperties to be provided in the new documentPdfDocument instancepublic PdfSigner setSignerProperties(SignerProperties properties)
properties - the signer propertiespublic SignerProperties getSignerProperties()
public PdfSignature getSignatureDictionary()
public PdfSigner.ISignatureEvent getSignatureEvent()
public void setSignatureEvent(PdfSigner.ISignatureEvent signatureEvent)
signatureEvent - the signature eventpublic String getNewSigFieldName()
public PdfDocument getDocument()
protected void setDocument(PdfDocument document)
document - The PdfDocumentpublic void setOriginalOutputStream(OutputStream originalOS)
originalOS - OutputStream for the bytes of the documentpublic PdfSignatureFormField getSignatureField()
This field instance is expected to be used for setting appearance related properties such as
PdfSignatureFormField.setReuseAppearance(boolean), PdfSignatureFormField.setBackgroundLayer(com.itextpdf.kernel.pdf.xobject.PdfFormXObject) and
PdfSignatureFormField.setSignatureAppearanceLayer(com.itextpdf.kernel.pdf.xobject.PdfFormXObject).
Note that for the new signature field SignerProperties.setPageRect(Rectangle) and
SignerProperties.setPageNumber(int) should be called before this method.
PdfSignatureFormField instancepublic void signDetached(IExternalDigest externalDigest, IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientexternalDigest - an implementation that provides the digestestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESIOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signDetached(IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESIOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signDetached(IExternalDigest externalDigest, IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype, SignaturePolicyInfo signaturePolicy) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientexternalDigest - an implementation that provides the digestestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESsignaturePolicy - the signature policy (for EPES signatures)IOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signDetached(IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype, SignaturePolicyInfo signaturePolicy) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESsignaturePolicy - the signature policy (for EPES signatures)IOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signDetached(IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype, com.itextpdf.commons.bouncycastle.asn1.esf.ISignaturePolicyIdentifier signaturePolicy) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESsignaturePolicy - the signature policy (for EPES signatures)IOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signDetached(IExternalDigest externalDigest, IExternalSignature externalSignature, Certificate[] chain, Collection<ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, PdfSigner.CryptoStandard sigtype, com.itextpdf.commons.bouncycastle.asn1.esf.ISignaturePolicyIdentifier signaturePolicy) throws IOException, GeneralSecurityException
externalSignature - the interface providing the actual signingchain - the certificate chaincrlList - the CRL listocspClient - the OCSP clienttsaClient - the Timestamp clientexternalDigest - an implementation that provides the digestestimatedSize - the reserved size for the signature. It will be estimated if 0sigtype - Either Signature.CMS or Signature.CADESsignaturePolicy - the signature policy (for EPES signatures)IOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic void signExternalContainer(IExternalSignatureContainer externalSignatureContainer, int estimatedSize) throws GeneralSecurityException, IOException
externalSignatureContainer - the interface providing the actual signingestimatedSize - the reserved size for the signatureGeneralSecurityException - if some problem during apply security algorithms occursIOException - if some I/O problem occurspublic void timestamp(ITSAClient tsa, String signatureName) throws IOException, GeneralSecurityException
tsa - the timestamp generatorsignatureName - the signature name or null to have a name generated
automaticallyIOException - if some I/O problem occurs or estimation for timestamp signature,
provided with ITSAClient.getTokenSizeEstimate(), is not big enoughGeneralSecurityException - if some problem during apply security algorithms occurs@Deprecated public static void signDeferred(PdfDocument document, String fieldName, OutputStream outs, IExternalSignatureContainer externalSignatureContainer) throws IOException, GeneralSecurityException
signDeferred(PdfReader, String, OutputStream, IExternalSignatureContainer)
should be used instead.document - the original PDFfieldName - the field to sign. It must be the last fieldouts - the output PDFexternalSignatureContainer - the signature container doing the actual signing. Only the
method ExternalSignatureContainer.sign is usedIOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occurspublic static void signDeferred(PdfReader reader, String fieldName, OutputStream outs, IExternalSignatureContainer externalSignatureContainer) throws IOException, GeneralSecurityException
reader - PdfReader that reads the PDF filefieldName - the field to sign. It must be the last fieldouts - the output PDFexternalSignatureContainer - the signature container doing the actual signing. Only the
method ExternalSignatureContainer.sign is usedIOException - if some I/O problem occursGeneralSecurityException - if some problem during apply security algorithms occursprotected Collection<byte[]> processCrl(Certificate cert, Collection<ICrlClient> crlList) throws CertificateEncodingException
cert - a Certificate if one of the CrlList implementations needs to retrieve the CRL URL from it.crlList - a list of CrlClient implementationsCertificateEncodingException - if an encoding error occurs in Certificate.protected void addDeveloperExtension(PdfDeveloperExtension extension)
PdfDocument.extension - PdfDeveloperExtension to be addedprotected boolean isPreClosed()
protected void preClose(Map<PdfName,Integer> exclusionSizes) throws IOException
exclusionSizes must contain at least
the PdfName.CONTENTS key with the size that it will take in the
document. Note that due to the hex string coding this size should be byte_size*2+2.
exclusionSizes - Map with names and sizes to be excluded in the signature
calculation. The key is a PdfName and the value an Integer.
At least the /Contents must be presentIOException - on errorprotected SignatureFieldAppearance getSignatureAppearance()
SignerProperties.setSignatureAppearance(SignatureFieldAppearance) and
customized using PdfSigner properties such as signing date, reason, location and signer name
in case they weren't specified by the user, or, if none was set, returns a new one with default appearance.
To customize the appearance of the signature, create new SignatureFieldAppearance object and set it
using SignerProperties.setSignatureAppearance(SignatureFieldAppearance).
Note that in case you create new signature field (either use SignerProperties.setFieldName(java.lang.String) with the name
that doesn't exist in the document or don't specify it at all) then the signature is invisible by default.
It is possible to set other appearance related properties such as
PdfSignatureFormField.setReuseAppearance(boolean), PdfSignatureFormField.setBackgroundLayer(com.itextpdf.kernel.pdf.xobject.PdfFormXObject) (n0 layer) and
PdfSignatureFormField.setSignatureAppearanceLayer(com.itextpdf.kernel.pdf.xobject.PdfFormXObject) (n2 layer) for the signature field using
getSignatureField(). Page, rectangle and other properties could be set up via SignerProperties.
SignatureFieldAppearance object representing signature appearanceprotected PdfSigFieldLock populateExistingSignatureFormField(PdfAcroForm acroForm)
preClose(Map) method if the signature field already exists.acroForm - PdfAcroForm object in which the signature field will be populatedprotected PdfSigFieldLock createNewSignatureFormField(PdfAcroForm acroForm, String name)
preClose(Map) method if the signature field doesn't exist.acroForm - PdfAcroForm object in which new signature field will be addedname - the name of the fieldprotected InputStream getRangeStream() throws IOException
preClose(Map), getRangeStream() and close(PdfDictionary).InputStream of bytes to be signedIOException - if some I/O problem occursprotected void close(PdfDictionary update) throws IOException
update is a PdfDictionary that must have exactly the
same keys as the ones provided in preClose(Map).
update - a PdfDictionary with the key/value that will fill the holes defined
in preClose(Map)IOException - on errorprotected com.itextpdf.io.source.IRandomAccessSource getUnderlyingSource()
throws IOException
IOException - if some I/O problem occursprotected void addDocMDP(PdfSignature crypto)
crypto - the signature dictionaryprotected void addFieldMDP(PdfSignature crypto, PdfSigFieldLock fieldLock)
crypto - the signature dictionaryfieldLock - the PdfSigFieldLock instance specified the field lock to be setprotected boolean documentContainsCertificationOrApprovalSignatures()
true if document contains certification or approval signatures, false otherwiseprotected Rectangle getWidgetRectangle(PdfWidgetAnnotation widget)
widget - PdfWidgetAnnotation to extract the rectangle fromprotected int getWidgetPageNumber(PdfWidgetAnnotation widget)
widget - PdfWidgetAnnotation from which to extract the page numberprotected void applyAccessibilityProperties(PdfFormField formField, IAccessibleElement modelElement, PdfDocument pdfDocument)
AccessibilityProperties for provided signature field.formField - PdfFormField the form field to which the accessibility properties should be appliedmodelElement - IAccessibleElement the form field layout element with accessibility propertiespdfDocument - PdfDocument the document to which the form field belongsCopyright © 1998–2025 Apryse Group NV. All rights reserved.