com.lowagie.text.pdf

Class PdfSignatureAppearance

public class PdfSignatureAppearance extends Object

This class takes care of the cryptographic options and appearances that form a signature.
Nested Class Summary
static classPdfSignatureAppearance.RangeStream
interfacePdfSignatureAppearance.SignatureEvent
An interface to retrieve the signature dictionary for modification.
Field Summary
booleanacro6Layers
Holds value of property acro6Layers.
PdfTemplate[]app
byte[]bout
intboutLen
Certificate[]certChain
intcertificationLevel
Stringcontact
Holds value of property contact.
CRL[]crlList
PdfDictionarycryptoDictionary
static intCERTIFIED_FORM_FILLING
static intCERTIFIED_FORM_FILLING_AND_ANNOTATIONS
static intCERTIFIED_NO_CHANGES_ALLOWED
StringdigestEncryptionAlgorithm
HashMapexclusionLocations
byte[]externalDigest
byte[]externalRSAdata
StringfieldName
PdfNamefilter
PdfTemplatefrm
Imageimage
Holds value of property image.
floatimageScale
Holds value of property imageScale.
Fontlayer2Font
Holds value of property layer2Font.
Stringlayer2Text
Stringlayer4Text
Holds value of property layer4Text.
Stringlocation
static floatMARGIN
booleannewField
static intNOT_CERTIFIED
OutputStreamoriginalout
intpage
RectanglepageRect
booleanpreClosed
PrivateKeyprivKey
Stringprovider
static StringquestionMark
Commands to draw a yellow question mark in a stream content
RandomAccessFileraf
int[]range
Stringreason
Rectanglerect
intrender
intrunDirection
Holds value of property runDirection.
PdfSignatureAppearance.SignatureEventsignatureEvent
Holds value of property signatureEvent.
ImagesignatureGraphic
CalendarsignDate
ByteBuffersigout
PdfSigGenericPKCSsigStandard
PdfStamperstamper
static PdfNameSELF_SIGNED
The self signed filter.
static intSignatureRenderDescription
The rendering mode is just the description
static intSignatureRenderGraphicAndDescription
The rendering mode is an image and the description
static intSignatureRenderNameAndDescription
The rendering mode is the name of the signer and the description
FiletempFile
static floatTOP_SECTION
static PdfNameVERISIGN_SIGNED
The VeriSign filter.
PdfStamperImpwriter
static PdfNameWINCER_SIGNED
The Windows Certificate Security.
Constructor Summary
PdfSignatureAppearance(PdfStamperImp writer)
Method Summary
voidaddDocMDP(PdfDictionary crypto)
voidclose(PdfDictionary update)
This is the last method to be called when using external signatures.
static floatfitText(Font font, String text, Rectangle rect, float maxFontSize, int runDirection)
Fits the text to some rectangle adjusting the font size as needed.
PdfTemplategetAppearance()
Gets the main appearance layer.
Certificate[]getCertChain()
Gets the certificate chain.
intgetCertificationLevel()
Gets the certified status of this document.
StringgetContact()
Gets the signing contact.
CRL[]getCrlList()
Gets the certificate revocation list.
PdfDictionarygetCryptoDictionary()
Gets the user made signature dictionary.
StringgetFieldName()
Gets the field name.
PdfNamegetFilter()
Gets the filter used to sign the document.
ImagegetImage()
Gets the background image for the layer 2.
floatgetImageScale()
Gets the scaling to be applied to the background image.
PdfTemplategetLayer(int layer)
Gets a template layer to create a signature appearance.
FontgetLayer2Font()
Gets the n2 and n4 layer font.
StringgetLayer2Text()
Gets the signature text identifying the signer if set by setLayer2Text().
StringgetLayer4Text()
Gets the text identifying the signature status if set by setLayer4Text().
StringgetLocation()
Gets the signing location.
StringgetNewSigName()
Gets a new signature fied name that doesn't clash with any existing name.
OutputStreamgetOriginalout()
intgetPage()
Gets the page number of the field.
RectanglegetPageRect()
Gets the rectangle that represent the position and dimension of the signature in the page.
PrivateKeygetPrivKey()
Gets the private key.
StringgetProvider()
Returns the Cryptographic Service Provider that will sign the document.
InputStreamgetRangeStream()
Gets the document bytes that are hashable when using external signatures.
StringgetReason()
Gets the signing reason.
RectanglegetRect()
Gets the rectangle representing the signature dimensions.
intgetRender()
Gets the rendering mode for this signature.
intgetRunDirection()
Gets the run direction.
PdfSignatureAppearance.SignatureEventgetSignatureEvent()
Getter for property signatureEvent.
ImagegetSignatureGraphic()
Gets the Image object to render.
CalendargetSignDate()
Gets the signature date.
ByteBuffergetSigout()
PdfSigGenericPKCSgetSigStandard()
Gets the instance of the standard signature dictionary.
PdfStampergetStamper()
Gets the PdfStamper associated with this instance.
FilegetTempFile()
Gets the temporary file.
PdfTemplategetTopLayer()
Gets the template that aggregates all appearance layers.
booleanisAcro6Layers()
Gets the Acrobat 6.0 layer mode.
booleanisInvisible()
Gets the visibility status of the signature.
booleanisNewField()
Checks if a new field was created.
booleanisPreClosed()
Checks if the document is in the process of closing.
voidpreClose()
This is the first method to be called when using external signatures.
voidpreClose(HashMap exclusionSizes)
This is the first method to be called when using external signatures.
voidsetAcro6Layers(boolean acro6Layers)
Acrobat 6.0 and higher recommends that only layer n2 and n4 be present.
voidsetCertificationLevel(int certificationLevel)
Sets the document type to certified instead of simply signed.
voidsetContact(String contact)
Sets the signing contact.
voidsetCrypto(PrivateKey privKey, Certificate[] certChain, CRL[] crlList, PdfName filter)
Sets the cryptographic parameters.
voidsetCryptoDictionary(PdfDictionary cryptoDictionary)
Sets a user made signature dictionary.
voidsetExternalDigest(byte[] digest, byte[] RSAdata, String digestEncryptionAlgorithm)
Sets the digest/signature to an external calculated value.
voidsetImage(Image image)
Sets 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.
voidsetOriginalout(OutputStream originalout)
voidsetProvider(String provider)
Sets the Cryptographic Service Provider that will sign the document.
voidsetReason(String reason)
Sets the signing reason.
voidsetRender(int render)
Sets the rendering mode for this signature.
voidsetRunDirection(int runDirection)
Sets the run direction in the n2 and n4 layer.
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 to SignatureRenderGraphicAndDescription
voidsetSignDate(Calendar signDate)
Sets the signature date.
voidsetSigout(ByteBuffer sigout)
voidsetStamper(PdfStamper stamper)
voidsetTempFile(File tempFile)
voidsetVisibleSignature(Rectangle pageRect, int page, String fieldName)
Sets the signature to be visible.
voidsetVisibleSignature(String fieldName)
Sets the signature to be visible.

Field Detail

acro6Layers

private boolean acro6Layers
Holds value of property acro6Layers.

app

private PdfTemplate[] app

bout

private byte[] bout

boutLen

private int boutLen

certChain

private Certificate[] certChain

certificationLevel

private int certificationLevel

contact

private String contact
Holds value of property contact.

crlList

private CRL[] crlList

cryptoDictionary

private PdfDictionary cryptoDictionary

CERTIFIED_FORM_FILLING

public static final int CERTIFIED_FORM_FILLING

CERTIFIED_FORM_FILLING_AND_ANNOTATIONS

public static final int CERTIFIED_FORM_FILLING_AND_ANNOTATIONS

CERTIFIED_NO_CHANGES_ALLOWED

public static final int CERTIFIED_NO_CHANGES_ALLOWED

digestEncryptionAlgorithm

private String digestEncryptionAlgorithm

exclusionLocations

private HashMap exclusionLocations

externalDigest

private byte[] externalDigest

externalRSAdata

private byte[] externalRSAdata

fieldName

private String fieldName

filter

private PdfName filter

frm

private PdfTemplate frm

image

private Image image
Holds value of property image.

imageScale

private float imageScale
Holds value of property imageScale.

layer2Font

private Font layer2Font
Holds value of property layer2Font.

layer2Text

private String layer2Text

layer4Text

private String layer4Text
Holds value of property layer4Text.

location

private String location

MARGIN

private static final float MARGIN

newField

private boolean newField

NOT_CERTIFIED

public static final int NOT_CERTIFIED

originalout

private OutputStream originalout

page

private int page

pageRect

private Rectangle pageRect

preClosed

private boolean preClosed

privKey

private PrivateKey privKey

provider

private String provider

questionMark

public static final String questionMark
Commands to draw a yellow question mark in a stream content

raf

private RandomAccessFile raf

range

private int[] range

reason

private String reason

rect

private Rectangle rect

render

private int render

runDirection

private int runDirection
Holds value of property runDirection.

signatureEvent

private PdfSignatureAppearance.SignatureEvent signatureEvent
Holds value of property signatureEvent.

signatureGraphic

private Image signatureGraphic

signDate

private Calendar signDate

sigout

private ByteBuffer sigout

sigStandard

private PdfSigGenericPKCS sigStandard

stamper

private PdfStamper stamper

SELF_SIGNED

public static final PdfName SELF_SIGNED
The self signed filter.

SignatureRenderDescription

public static final int SignatureRenderDescription
The rendering mode is just the description

SignatureRenderGraphicAndDescription

public static final int SignatureRenderGraphicAndDescription
The rendering mode is an image and the description

SignatureRenderNameAndDescription

public static final int SignatureRenderNameAndDescription
The rendering mode is the name of the signer and the description

tempFile

private File tempFile

TOP_SECTION

private static final float TOP_SECTION

VERISIGN_SIGNED

public static final PdfName VERISIGN_SIGNED
The VeriSign filter.

writer

private PdfStamperImp writer

WINCER_SIGNED

public static final PdfName WINCER_SIGNED
The Windows Certificate Security.

Constructor Detail

PdfSignatureAppearance

PdfSignatureAppearance(PdfStamperImp writer)

Method Detail

addDocMDP

private void addDocMDP(PdfDictionary crypto)

close

public void close(PdfDictionary update)
This is the last method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().

update is a PdfDictionary that must have exactly the same keys as the ones provided in preClose.

Parameters: update a PdfDictionary with the key/value that will fill the holes defined in preClose

Throws: DocumentException on error IOException on error

fitText

public static float fitText(Font font, String text, Rectangle rect, float maxFontSize, int runDirection)
Fits the text to some rectangle adjusting the font size as needed.

Parameters: font the font to use text the text rect the rectangle where the text must fit maxFontSize the maximum font size runDirection the run direction

Returns: the calculated font size that makes the text fit

getAppearance

public PdfTemplate getAppearance()
Gets the main appearance layer.

Consult PPKAppearances.pdf for further details.

Returns: the main appearance layer

Throws: DocumentException on error

getCertChain

public Certificate[] getCertChain()
Gets the certificate chain.

Returns: the certificate chain

getCertificationLevel

public int getCertificationLevel()
Gets the certified status of this document.

Returns: the certified status

getContact

public String getContact()
Gets the signing contact.

Returns: the signing contact

getCrlList

public CRL[] getCrlList()
Gets the certificate revocation list.

Returns: the certificate revocation list

getCryptoDictionary

public PdfDictionary getCryptoDictionary()
Gets the user made signature dictionary. This is the dictionary at the /V key.

Returns: the user made signature dictionary

getFieldName

public String getFieldName()
Gets the field name.

Returns: the field name

getFilter

public PdfName getFilter()
Gets the filter used to sign the document.

Returns: the filter used to sign the document

getImage

public Image getImage()
Gets the background image for the layer 2.

Returns: 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

getLayer

public PdfTemplate getLayer(int layer)
Gets a template layer to create a signature appearance. The layers can go from 0 to 4.

Consult PPKAppearances.pdf for further details.

Parameters: layer the layer

Returns: a template

getLayer2Font

public Font getLayer2Font()
Gets the n2 and n4 layer font.

Returns: the n2 and n4 layer font

getLayer2Text

public String getLayer2Text()
Gets the signature text identifying the signer if set by setLayer2Text().

Returns: the signature text identifying the signer

getLayer4Text

public String getLayer4Text()
Gets the text identifying the signature status if set by setLayer4Text().

Returns: the text identifying the signature status

getLocation

public String getLocation()
Gets the signing location.

Returns: the signing location

getNewSigName

public String getNewSigName()
Gets a new signature fied name that doesn't clash with any existing name.

Returns: a new signature fied name

getOriginalout

OutputStream getOriginalout()

getPage

public int getPage()
Gets the page number of the field.

Returns: the page number of the field

getPageRect

public Rectangle 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

getPrivKey

public PrivateKey getPrivKey()
Gets the private key.

Returns: the private key

getProvider

public String getProvider()
Returns the Cryptographic Service Provider that will sign the document.

Returns: provider the name of the provider, for example "SUN", or null to use the default provider.

getRangeStream

public InputStream 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

getReason

public String getReason()
Gets the signing reason.

Returns: the signing reason

getRect

public Rectangle getRect()
Gets the rectangle representing the signature dimensions.

Returns: the rectangle representing the signature dimensions. It may be null or have zero width or height for invisible signatures

getRender

public int getRender()
Gets the rendering mode for this signature.

Returns: the rendering mode for this signature

getRunDirection

public int getRunDirection()
Gets the run direction.

Returns: the run direction

getSignatureEvent

public PdfSignatureAppearance.SignatureEvent getSignatureEvent()
Getter for property signatureEvent.

Returns: Value of property signatureEvent.

getSignatureGraphic

public Image getSignatureGraphic()
Gets the Image object to render.

Returns: the image

getSignDate

public Calendar getSignDate()
Gets the signature date.

Returns: the signature date

getSigout

ByteBuffer getSigout()

getSigStandard

public PdfSigGenericPKCS getSigStandard()
Gets the instance of the standard signature dictionary. This instance is only available after pre close.

The main use is to insert external signatures.

Returns: the instance of the standard signature dictionary

getStamper

public PdfStamper getStamper()
Gets the PdfStamper associated with this instance.

Returns: the PdfStamper associated with this instance

getTempFile

public File getTempFile()
Gets the temporary file.

Returns: the temporary file or null is the document is created in memory

getTopLayer

public PdfTemplate 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

isAcro6Layers

public boolean isAcro6Layers()
Gets the Acrobat 6.0 layer mode.

Returns: the Acrobat 6.0 layer mode

isInvisible

public boolean isInvisible()
Gets the visibility status of the signature.

Returns: the visibility status of the signature

isNewField

public boolean isNewField()
Checks if a new field was created.

Returns: true if a new field was created, false if signing an existing field or if the signature is invisible

isPreClosed

public boolean isPreClosed()
Checks if the document is in the process of closing.

Returns: true if the document is in the process of closing, false otherwise

preClose

public void 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().

No external signatures are allowed if this method is called.

Throws: IOException on error DocumentException on error

preClose

public void preClose(HashMap exclusionSizes)
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().

If using an external signature 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.

Parameters: exclusionSizes a HashMap with names and sizes to be excluded in the signature calculation. The key is a PdfName and the value an Integer. At least the PdfName.CONTENTS must be present

Throws: IOException on error DocumentException on error

setAcro6Layers

public void setAcro6Layers(boolean acro6Layers)
Acrobat 6.0 and higher recommends that only layer n2 and n4 be present. This method sets that mode.

Parameters: acro6Layers if true only the layers n2 and n4 will be present

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_FILLING and CERTIFIED_FORM_FILLING_AND_ANNOTATIONS

setContact

public void setContact(String contact)
Sets the signing contact.

Parameters: contact the signing contact

setCrypto

public void setCrypto(PrivateKey privKey, Certificate[] certChain, CRL[] crlList, PdfName filter)
Sets the cryptographic parameters.

Parameters: privKey the private key certChain the certificate chain crlList the certificate revocation list. It may be null filter the crytographic filter type. It can be SELF_SIGNED, VERISIGN_SIGNED or WINCER_SIGNED

setCryptoDictionary

public void setCryptoDictionary(PdfDictionary cryptoDictionary)
Sets a user made signature dictionary. This is the dictionary at the /V key.

Parameters: cryptoDictionary a user made signature dictionary

setExternalDigest

public void setExternalDigest(byte[] digest, byte[] RSAdata, String digestEncryptionAlgorithm)
Sets the digest/signature to an external calculated value.

Parameters: digest the digest. This is the actual signature RSAdata the extra data that goes into the data tag in PKCS#7 digestEncryptionAlgorithm the encryption algorithm. It may must be null if the digest is also null. If the digest is not null then it may be "RSA" or "DSA"

setImage

public void setImage(Image image)
Sets the background image for the layer 2.

Parameters: image the background image for the layer 2

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

setLayer2Font

public void setLayer2Font(Font layer2Font)
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

setLayer2Text

public void setLayer2Text(String text)
Sets the signature text identifying the signer.

Parameters: text the signature text identifying the signer. If null or not set a standard description will be used

setLayer4Text

public void setLayer4Text(String text)
Sets the text identifying the signature status.

Parameters: text the text identifying the signature status. If null or not set the description "Signature Not Verified" will be used

setLocation

public void setLocation(String location)
Sets the signing location.

Parameters: location the signing location

setOriginalout

void setOriginalout(OutputStream originalout)

setProvider

public void setProvider(String provider)
Sets the Cryptographic Service Provider that will sign the document.

Parameters: provider the name of the provider, for example "SUN", or null to use the default provider.

setReason

public void setReason(String reason)
Sets the signing reason.

Parameters: reason the signing reason

setRender

public void setRender(int render)
Sets the rendering mode for this signature. The rendering modes can be the constants SignatureRenderDescription, SignatureRenderNameAndDescription or SignatureRenderGraphicAndDescription. The two last modes should be used with Acrobat 6 layer type.

Parameters: render the render mode

setRunDirection

public void setRunDirection(int runDirection)
Sets the run direction in the n2 and n4 layer.

Parameters: runDirection the run direction

setSignatureEvent

public void setSignatureEvent(PdfSignatureAppearance.SignatureEvent signatureEvent)
Sets the signature event to allow modification of the signature dictionary.

Parameters: signatureEvent the signature event

setSignatureGraphic

public void setSignatureGraphic(Image signatureGraphic)
Sets the Image object to render when Render is set to SignatureRenderGraphicAndDescription

Parameters: signatureGraphic image rendered. If null the mode is defaulted to SignatureRenderDescription

setSignDate

public void setSignDate(Calendar signDate)
Sets the signature date.

Parameters: signDate the signature date

setSigout

void setSigout(ByteBuffer sigout)

setStamper

void setStamper(PdfStamper stamper)

setTempFile

void setTempFile(File tempFile)

setVisibleSignature

public void setVisibleSignature(Rectangle pageRect, int page, String fieldName)
Sets the signature to be visible. It creates a new visible signature field.

Parameters: pageRect the position and dimension of the field in the page page the page to place the field. The fist page is 1 fieldName the field name or null to generate automatically a new field name

setVisibleSignature

public void setVisibleSignature(String fieldName)
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