Package com.itextpdf.text.pdf
Class PdfSignatureAppearance
java.lang.Object
com.itextpdf.text.pdf.PdfSignatureAppearance
Class that takes care of the cryptographic options
and appearances that form a signature.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSignature rendering modesstatic interfaceAn interface to retrieve the signature dictionary for modification. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanAppearance compliant with the recommendations introduced in Acrobat 6?private PdfTemplate[]Layers for a visible signature.private byte[]The bytes of the file right before the signature is added (if raf is null)private intLength of the output.private intThe certification levelstatic final intAuthor signature, form filling allowedstatic final intAuthor signature, form filling and annotations allowedstatic final intAuthor signature, no changes allowedprivate StringThe contact name of the signer.private PdfDictionaryThe crypto dictionaryprivate HashMap<PdfName, PdfLiteral> Name and content of keys that can only be added in the close() method.private PdfSigLockDictionarySignature field lock dictionaryprivate StringThe name of the fieldprivate PdfTemplateTemplate containing all layers drawn on top of each other.private ImageA background image for the text in layer 2.private floatthe scaling to be applied to the background image.tprivate FontFont for the text in Layer 2.private StringThe text that goes in Layer 2 of the signature appearance.private StringThe text that goes in Layer 4 of the appearance.private StringHolds value of property location.private StringThe caption for the location of signing.private static final floatmargin for the content inside the signature rectangle.static final intApproval signatureprivate OutputStreamOutputStream for the bytes of the stamper.private intThe page where the signature will appear.private Rectanglerectangle that represent the position and dimension of the signature in the page.private booleanIndicates if the stamper has already been pre-closed.static final StringAn appearance that can be used for layer 1 (if acro6Layers is false).private RandomAccessFileThe file right before the signature is added (can be null).private long[]Array containing the byte positions of the bytes that need to be hashed.private StringThe reason for signing.private StringThe caption for the reason for signing.private RectangleThe coordinates of the rectangle for a visible signature, or a zero-width, zero-height rectangle for an invisible signature.The rendering mode chosen for visible signaturesprivate booleanIndicates if we need to reuse the existing appearance as layer 0.private intRun direction for the text in layers 2 and 4.private StringHolds value of the application that creates the signatureHolds value of property signatureEvent.private ImageThe image that needs to be used for a visible signatureprivate CertificateThe signing certificateprivate CalendarHolds value of property signDate.private ByteBufferA byte buffer containing the bytes of the Stamper.private PdfStamperThe PdfStamper that creates the signed PDF.private FileTemporary file in case you don't want to sign in memory.private static final floatextra space at the top.private PdfStamperImpThe PdfStamperImp object corresponding with the stamper. -
Constructor Summary
ConstructorsConstructorDescriptionPdfSignatureAppearance(PdfStamperImp writer) Constructs a PdfSignatureAppearance object. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the appropriate developer extension.private voidaddDocMDP(PdfDictionary crypto) Adds keys to the signature dictionary that define the certification level and the permissions.private voidaddFieldMDP(PdfDictionary crypto, PdfDictionary fieldLock) Adds keys to the signature dictionary that define the field permissions.voidclose(PdfDictionary update) This is the last method to be called when using external signatures.private voidGets the main appearance layer.intGets the certified status of this document.Gets the signing contact.Gets the user made signature dictionary.Getter for the field lock dictionary.Gets the field name.getImage()Gets the background image for the layer 2.floatGets the scaling to be applied to the background image.getLayer(int layer) Gets a template layer to create a signature appearance.Gets the n2 and n4 layer font.Gets the signature text identifying the signer if set by setLayer2Text().Gets the text identifying the signature status if set by setLayer4Text().Gets the signing location.Gets a new signature field name that doesn't clash with any existing name.(package private) OutputStreamGetter for the OutputStream.intgetPage()Gets the page number of the field.Gets the rectangle that represent the position and dimension of the signature in the page.Gets the document bytes that are hashable when using external signatures.Gets the signing reason.getRect()Gets the rectangle representing the signature dimensions.Gets the rendering mode for this signature.intGets the run direction.Gets the signature creator.Getter for property signatureEvent.Gets the Image object to render.Gets the signature date.(package private) ByteBufferGetter for the byte buffer.Gets thePdfStamperassociated with this instance.Gets the temporary file.Gets the template that aggregates all appearance layers.private RandomAccessSourcebooleanGets the Acrobat 6.0 layer mode.booleanGets the visibility status of the signature.booleanChecks if the document is in the process of closing.voidThis is the first method to be called when using external signatures.voidsetAcro6Layers(boolean acro6Layers) Deprecated.Adobe no longer supports Adobe Acrobat / Reader versions older than 9voidsetCertificate(Certificate signCertificate) Sets the certificate used to provide the text in the appearance.voidsetCertificationLevel(int certificationLevel) Sets the document type to certified instead of simply signed.voidsetContact(String contact) Sets the signing contact.voidsetCryptoDictionary(PdfDictionary cryptoDictionary) Sets a user made signature dictionary.voidsetFieldLockDict(PdfSigLockDictionary fieldLock) Setter for the field lock dictionary.voidSets the background image for the layer 2.voidsetImageScale(float imageScale) Sets the scaling to be applied to the background image.voidsetLayer2Font(Font layer2Font) Sets the n2 and n4 layer font.voidsetLayer2Text(String text) Sets the signature text identifying the signer.voidsetLayer4Text(String text) Sets the text identifying the signature status.voidsetLocation(String location) Sets the signing location.voidsetLocationCaption(String locationCaption) Sets the caption for the signing location.(package private) voidsetOriginalout(OutputStream originalout) Setter for the OutputStream.voidSets the signing reason.voidsetReasonCaption(String reasonCaption) Sets the caption for signing reason.voidsetRenderingMode(PdfSignatureAppearance.RenderingMode renderingMode) Sets the rendering mode for this signature.voidsetReuseAppearance(boolean reuseAppearance) Indicates that the existing appearances needs to be reused as layer 0.voidsetRunDirection(int runDirection) Sets the run direction in the n2 and n4 layer.voidsetSignatureCreator(String signatureCreator) Sets the name of the application used to create the signature.voidsetSignatureEvent(PdfSignatureAppearance.SignatureEvent signatureEvent) Sets the signature event to allow modification of the signature dictionary.voidsetSignatureGraphic(Image signatureGraphic) Sets the Image object to render when Render is set toRenderingMode.GRAPHICorRenderingMode.GRAPHIC_AND_DESCRIPTION.voidsetSignDate(Calendar signDate) Sets the signature date.(package private) voidsetSigout(ByteBuffer sigout) Setter for the byte buffer.(package private) voidsetStamper(PdfStamper stamper) Sets the PdfStamper(package private) voidsetTempFile(File tempFile) Setter for the temporary file.voidsetVisibleSignature(Rectangle pageRect, int page, String fieldName) Sets the signature to be visible.voidsetVisibleSignature(String fieldName) Sets the signature to be visible.
-
Field Details
-
NOT_CERTIFIED
public static final int NOT_CERTIFIEDApproval signature- See Also:
-
CERTIFIED_NO_CHANGES_ALLOWED
public static final int CERTIFIED_NO_CHANGES_ALLOWEDAuthor signature, no changes allowed- See Also:
-
CERTIFIED_FORM_FILLING
public static final int CERTIFIED_FORM_FILLINGAuthor signature, form filling allowed- See Also:
-
CERTIFIED_FORM_FILLING_AND_ANNOTATIONS
public static final int CERTIFIED_FORM_FILLING_AND_ANNOTATIONSAuthor signature, form filling and annotations allowed- See Also:
-
certificationLevel
private int certificationLevelThe certification level -
reasonCaption
The caption for the reason for signing. -
locationCaption
The caption for the location of signing. -
reason
The reason for signing. -
location
Holds value of property location. -
signDate
Holds value of property signDate. -
signatureCreator
Holds value of the application that creates the signature -
contact
The contact name of the signer. -
raf
The file right before the signature is added (can be null). -
bout
private byte[] boutThe bytes of the file right before the signature is added (if raf is null) -
range
private long[] rangeArray containing the byte positions of the bytes that need to be hashed. -
signCertificate
The signing certificate -
cryptoDictionary
The crypto dictionary -
signatureEvent
Holds value of property signatureEvent. -
fieldName
The name of the field -
page
private int pageThe page where the signature will appear. -
rect
The coordinates of the rectangle for a visible signature, or a zero-width, zero-height rectangle for an invisible signature. -
pageRect
rectangle that represent the position and dimension of the signature in the page. -
renderingMode
The rendering mode chosen for visible signatures -
signatureGraphic
The image that needs to be used for a visible signature -
acro6Layers
private boolean acro6LayersAppearance compliant with the recommendations introduced in Acrobat 6? -
app
Layers for a visible signature. -
reuseAppearance
private boolean reuseAppearanceIndicates if we need to reuse the existing appearance as layer 0. -
questionMark
An appearance that can be used for layer 1 (if acro6Layers is false).- See Also:
-
image
A background image for the text in layer 2. -
imageScale
private float imageScalethe scaling to be applied to the background image.t -
layer2Text
The text that goes in Layer 2 of the signature appearance. -
layer2Font
Font for the text in Layer 2. -
runDirection
private int runDirectionRun direction for the text in layers 2 and 4. -
layer4Text
The text that goes in Layer 4 of the appearance. -
frm
Template containing all layers drawn on top of each other. -
TOP_SECTION
private static final float TOP_SECTIONextra space at the top.- See Also:
-
MARGIN
private static final float MARGINmargin for the content inside the signature rectangle.- See Also:
-
stamper
The PdfStamper that creates the signed PDF. -
writer
The PdfStamperImp object corresponding with the stamper. -
sigout
A byte buffer containing the bytes of the Stamper. -
originalout
OutputStream for the bytes of the stamper. -
tempFile
Temporary file in case you don't want to sign in memory. -
exclusionLocations
Name and content of keys that can only be added in the close() method. -
boutLen
private int boutLenLength of the output. -
preClosed
private boolean preClosedIndicates if the stamper has already been pre-closed. -
fieldLock
Signature field lock dictionary
-
-
Constructor Details
-
PdfSignatureAppearance
PdfSignatureAppearance(PdfStamperImp writer) Constructs a PdfSignatureAppearance object.- Parameters:
writer- the writer to which the signature will be written.
-
-
Method Details
-
setCertificationLevel
public void setCertificationLevel(int certificationLevel) Sets the document type to certified instead of simply signed.- Parameters:
certificationLevel- the values can be:NOT_CERTIFIED,CERTIFIED_NO_CHANGES_ALLOWED,CERTIFIED_FORM_FILLINGandCERTIFIED_FORM_FILLING_AND_ANNOTATIONS
-
getCertificationLevel
public int getCertificationLevel()Gets the certified status of this document.- Returns:
- the certified status
-
getReason
Gets the signing reason.- Returns:
- the signing reason
-
setReason
Sets the signing reason.- Parameters:
reason- the signing reason
-
setReasonCaption
Sets the caption for signing reason.- Parameters:
reasonCaption- the signing reason caption
-
getLocation
Gets the signing location.- Returns:
- the signing location
-
setLocation
Sets the signing location.- Parameters:
location- the signing location
-
setLocationCaption
Sets the caption for the signing location.- Parameters:
locationCaption- the signing location caption
-
getSignatureCreator
Gets the signature creator.- Returns:
- the signature creator
-
setSignatureCreator
Sets the name of the application used to create the signature.- Parameters:
signatureCreator- the name of the signature creating application
-
getContact
Gets the signing contact.- Returns:
- the signing contact
-
setContact
Sets the signing contact.- Parameters:
contact- the signing contact
-
getSignDate
Gets the signature date.- Returns:
- the signature date
-
setSignDate
Sets the signature date.- Parameters:
signDate- the signature date
-
getRangeStream
Gets the document bytes that are hashable when using external signatures. The general sequence is: preClose(), getRangeStream() and close().- Returns:
- the document bytes that are hashable
- Throws:
IOException
-
getUnderlyingSource
- Returns:
- the underlying source
- Throws:
IOException
-
addDeveloperExtension
Adds the appropriate developer extension. -
getCryptoDictionary
Gets the user made signature dictionary. This is the dictionary at the /V key.- Returns:
- the user made signature dictionary
-
setCryptoDictionary
Sets a user made signature dictionary. This is the dictionary at the /V key.- Parameters:
cryptoDictionary- a user made signature dictionary
-
setCertificate
Sets the certificate used to provide the text in the appearance. This certificate doesn't take part in the actual signing process.- Parameters:
signCertificate- the certificate
-
getCertificate
-
getSignatureEvent
Getter for property signatureEvent.- Returns:
- Value of property signatureEvent.
-
setSignatureEvent
Sets the signature event to allow modification of the signature dictionary.- Parameters:
signatureEvent- the signature event
-
getFieldName
Gets the field name.- Returns:
- the field name
-
getNewSigName
Gets a new signature field name that doesn't clash with any existing name.- Returns:
- a new signature field name
-
getPage
public int getPage()Gets the page number of the field.- Returns:
- the page number of the field
-
getRect
Gets the rectangle representing the signature dimensions.- Returns:
- the rectangle representing the signature dimensions. It may be
nullor have zero width or height for invisible signatures
-
getPageRect
Gets the rectangle that represent the position and dimension of the signature in the page.- Returns:
- the rectangle that represent the position and dimension of the signature in the page
-
isInvisible
public boolean isInvisible()Gets the visibility status of the signature.- Returns:
- the visibility status of the signature
-
setVisibleSignature
Sets the signature to be visible. It creates a new visible signature field.- Parameters:
pageRect- the position and dimension of the field in the pagepage- the page to place the field. The fist page is 1fieldName- the field name ornullto generate automatically a new field name
-
setVisibleSignature
Sets the signature to be visible. An empty signature field with the same name must already exist.- Parameters:
fieldName- the existing empty signature field name
-
getRenderingMode
Gets the rendering mode for this signature.- Returns:
- the rendering mode for this signature
- Since:
- 5.0.1
-
setRenderingMode
Sets the rendering mode for this signature.- Parameters:
renderingMode- the rendering mode- Since:
- 5.0.1
-
getSignatureGraphic
Gets the Image object to render.- Returns:
- the image
-
setSignatureGraphic
Sets the Image object to render when Render is set toRenderingMode.GRAPHICorRenderingMode.GRAPHIC_AND_DESCRIPTION.- Parameters:
signatureGraphic- image rendered. Ifnullthe mode is defaulted toRenderingMode.DESCRIPTION
-
isAcro6Layers
public boolean isAcro6Layers()Gets the Acrobat 6.0 layer mode.- Returns:
- the Acrobat 6.0 layer mode
-
setAcro6Layers
public void setAcro6Layers(boolean acro6Layers) Deprecated.Adobe no longer supports Adobe Acrobat / Reader versions older than 9Acrobat 6.0 and higher recommends that only layer n0 and n2 be present. Use this method with valuefalseif you want to ignore this recommendation.- Parameters:
acro6Layers- iftrueonly the layers n0 and n2 will be present
-
getLayer
Gets a template layer to create a signature appearance. The layers can go from 0 to 4, but only layer 0 and 2 will be used if acro6Layers is true.Consult PPKAppearances.pdf for further details.
- Parameters:
layer- the layer- Returns:
- a template
-
setReuseAppearance
public void setReuseAppearance(boolean reuseAppearance) Indicates that the existing appearances needs to be reused as layer 0. -
getImage
Gets the background image for the layer 2.- Returns:
- the background image for the layer 2
-
setImage
Sets the background image for the layer 2.- Parameters:
image- the background image for the layer 2
-
getImageScale
public float getImageScale()Gets the scaling to be applied to the background image.- Returns:
- the scaling to be applied to the background image
-
setImageScale
public void setImageScale(float imageScale) Sets the scaling to be applied to the background image. If it's zero the image will fully fill the rectangle. If it's less than zero the image will fill the rectangle but will keep the proportions. If it's greater than zero that scaling will be applied. In any of the cases the image will always be centered. It's zero by default.- Parameters:
imageScale- the scaling to be applied to the background image
-
setLayer2Text
Sets the signature text identifying the signer.- Parameters:
text- the signature text identifying the signer. Ifnullor not set a standard description will be used
-
getLayer2Text
Gets the signature text identifying the signer if set by setLayer2Text().- Returns:
- the signature text identifying the signer
-
getLayer2Font
Gets the n2 and n4 layer font.- Returns:
- the n2 and n4 layer font
-
setLayer2Font
Sets the n2 and n4 layer font. If the font size is zero, auto-fit will be used.- Parameters:
layer2Font- the n2 and n4 font
-
setRunDirection
public void setRunDirection(int runDirection) Sets the run direction in the n2 and n4 layer.- Parameters:
runDirection- the run direction
-
getRunDirection
public int getRunDirection()Gets the run direction.- Returns:
- the run direction
-
setLayer4Text
Sets the text identifying the signature status. Will be ignored if acro6Layers is true.- Parameters:
text- the text identifying the signature status. Ifnullor not set the description "Signature Not Verified" will be used
-
getLayer4Text
Gets the text identifying the signature status if set by setLayer4Text().- Returns:
- the text identifying the signature status
-
getTopLayer
Gets the template that aggregates all appearance layers. This corresponds to the /FRM resource.Consult PPKAppearances.pdf for further details.
- Returns:
- the template that aggregates all appearance layers
-
getAppearance
Gets the main appearance layer.Consult PPKAppearances.pdf for further details.
- Returns:
- the main appearance layer
- Throws:
DocumentException- on error
-
createBlankN0
private void createBlankN0() -
getStamper
Gets thePdfStamperassociated with this instance.- Returns:
- the
PdfStamperassociated with this instance
-
setStamper
Sets the PdfStamper- Parameters:
stamper- PdfStamper
-
getSigout
ByteBuffer getSigout()Getter for the byte buffer. -
setSigout
Setter for the byte buffer. -
getOriginalout
OutputStream getOriginalout()Getter for the OutputStream. -
setOriginalout
Setter for the OutputStream. -
getTempFile
Gets the temporary file.- Returns:
- the temporary file or
nullis the document is created in memory
-
setTempFile
Setter for the temporary file.- Parameters:
tempFile-
-
getFieldLockDict
Getter for the field lock dictionary.- Returns:
- Field lock dictionary.
-
setFieldLockDict
Setter for the field lock dictionary.Be aware: if a signature is created on an existing signature field, then its /Lock dictionary takes the precedence (if it exists).
- Parameters:
fieldLock- Field lock dictionary.
-
isPreClosed
public boolean isPreClosed()Checks if the document is in the process of closing.- Returns:
trueif the document is in the process of closing,falseotherwise
-
preClose
This is the first method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().If calling preClose() dont't call PdfStamper.close().
exclusionSizesmust contain at least thePdfName.CONTENTSkey 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.- Parameters:
exclusionSizes- aHashMapwith names and sizes to be excluded in the signature calculation. The key is aPdfNameand the value anInteger. At least thePdfName.CONTENTSmust be present- Throws:
IOException- on errorDocumentException- on error
-
addDocMDP
Adds keys to the signature dictionary that define the certification level and the permissions. This method is only used for Certifying signatures.- Parameters:
crypto- the signature dictionary
-
addFieldMDP
Adds keys to the signature dictionary that define the field permissions. This method is only used for signatures that lock fields.- Parameters:
crypto- the signature dictionary
-
close
This is the last method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().updateis aPdfDictionarythat must have exactly the same keys as the ones provided inpreClose(HashMap).- Parameters:
update- aPdfDictionarywith the key/value that will fill the holes defined inpreClose(HashMap)- Throws:
DocumentException- on errorIOException- on error
-