Top Description Fields Constructors Methods
org.apache.pdfbox.examples.signature

public Class CreateVisibleSignature

extends CreateSignatureBase
Class Inheritance
Imports
java.io.File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, .RandomAccessFile, java.security.KeyStore, .KeyStoreException, .NoSuchAlgorithmException, .UnrecoverableKeyException, java.security.cert.CertificateException, java.util.Calendar, org.apache.pdfbox.Loader, org.apache.pdfbox.cos.COSName, org.apache.pdfbox.io.IOUtils, .RandomAccessStreamCache.StreamCacheCreateFunction, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport, .PDSignature, .SignatureInterface, .SignatureOptions, org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties, .PDVisibleSignDesigner, org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm, .PDSignatureField, org.apache.pdfbox.util.Hex

This is an example for visual signing a pdf.
Author
Vakhtang Koroghlishvili
See Also
CreateSignature

Field Summary

Modifier and TypeField and Description
private PDDocument
private boolean
private SignatureOptions
private RandomAccessStreamCache.StreamCacheCreateFunction
private final PDVisibleSigProperties
private PDVisibleSignDesigner

Constructor Summary

AccessConstructor and Description
public
CreateVisibleSignature(KeyStore
is a pkcs12 keystore.
keystore
,
char[]
is the pin for the keystore / private key
pin
)

Initialize the signature creator with a keystore (pkcs12) and pin that should be used for the signature.

Method Summary

Modifier and TypeMethod and Description
private PDSignature
public RandomAccessStreamCache.StreamCacheCreateFunction

Returns:

the function to be used to create an instance of the choosen stream cache
getStreamCacheCreateFunction
()

Get the function to be used to create an instance of the choosen stream cache.

public boolean
public static void
main(String[] args)

Arguments are [0] key store [1] pin [2] document that will be signed [3] image of visible signature

public void
setLateExternalSigning(boolean lateExternalSigning)

Set late external signing.

public void
setStreamCacheCreateFunction(RandomAccessStreamCache.StreamCacheCreateFunction
the function to be used to create an instance of the choosen stream cache
streamCache
)

Set the function to be used to create an instance of the choosen stream cache.

public void
setVisibleSignatureProperties(String name, String location, String reason, int preferredSize, int
the signature should be placed on (1-based)
page
,
boolean visualSignEnabled)

Set visible signature properties for new signature fields.

public void
setVisibleSignatureProperties(String name, String location, String reason, boolean visualSignEnabled)

Set visible signature properties for existing signature fields.

public void
setVisibleSignDesigner(String
path of the PDF file
filename
,
int
position of the signature field
x
,
int
position of the signature field
y
,
int
increase (positive value) or decrease (negative value) image with x percent.
zoomPercent
,
InputStream
input stream of an image.
imageStream
,
int
the signature should be placed on (1-based)
page
)

Open the PDF, create and set the visible signature designer for a new signature field.

public void
setVisibleSignDesigner(int
increase (positive value) or decrease (negative value) image with x percent.
zoomPercent
,
InputStream
input stream of an image.
imageStream
)

Set visible signature designer for an existing signature field.

public void
signPDF(File
The source pdf document file.
inputFile
,
File
The file to be signed.
signedFile
,
String
optional TSA url
tsaUrl
)

Sign pdf file and create new file that ends with "_signed.pdf".

public void
signPDF(File
The source pdf document file. It will be opened if it hasn't been opened before in setVisibleSignDesigner(java.lang.String, int, int, int, java.io.InputStream, int).
inputFile
,
File
The file to be signed.
signedFile
,
String
optional TSA url
tsaUrl
,
String
optional name of an existing (unsigned) signature field
signatureFieldName
)

Sign pdf file and create new file that ends with "_signed.pdf".

private static void
usage()

This will print the usage for this program.

Inherited from org.apache.pdfbox.examples.signature.CreateSignatureBase:
getCertificateChainisExternalSigningsetCertificateChainsetExternalSigningsetPrivateKeysetTsaUrlsign

Field Detail

docback to summary
private PDDocument doc
lateExternalSigningback to summary
private boolean lateExternalSigning
signatureOptionsback to summary
private SignatureOptions signatureOptions
streamCacheback to summary
private RandomAccessStreamCache.StreamCacheCreateFunction streamCache
visibleSignaturePropertiesback to summary
private final PDVisibleSigProperties visibleSignatureProperties
visibleSignDesignerback to summary
private PDVisibleSignDesigner visibleSignDesigner

Constructor Detail

CreateVisibleSignatureback to summary
public CreateVisibleSignature(KeyStore keystore, char[] pin) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, IOException, CertificateException

Initialize the signature creator with a keystore (pkcs12) and pin that should be used for the signature.

Parameters
keystore:KeyStore

is a pkcs12 keystore.

pin:char[]

is the pin for the keystore / private key

Exceptions
KeyStoreException:
if the keystore has not been initialized (loaded)
UnrecoverableKeyException:
if the given password is wrong
NoSuchAlgorithmException:
if the algorithm for recovering the key cannot be found
IOException:
if no certificate could be found
CertificateException:
if the certificate is not valid as signing time

Method Detail

findExistingSignatureback to summary
private PDSignature findExistingSignature(PDDocument doc, String sigFieldName)
getStreamCacheCreateFunctionback to summary
public RandomAccessStreamCache.StreamCacheCreateFunction getStreamCacheCreateFunction()

Get the function to be used to create an instance of the choosen stream cache.

Returns:RandomAccessStreamCache.StreamCacheCreateFunction

the function to be used to create an instance of the choosen stream cache

isLateExternalSigningback to summary
public boolean isLateExternalSigning()
mainback to summary
public static void main(String[] args) throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException

Arguments are [0] key store [1] pin [2] document that will be signed [3] image of visible signature

setLateExternalSigningback to summary
public void setLateExternalSigning(boolean lateExternalSigning)

Set late external signing. Enable this if you want to activate the demo code where the signature is kept and added in an extra step without using PDFBox methods. This is disabled by default.

setStreamCacheCreateFunctionback to summary
public void setStreamCacheCreateFunction(RandomAccessStreamCache.StreamCacheCreateFunction streamCache)

Set the function to be used to create an instance of the choosen stream cache.

Parameters
streamCache:RandomAccessStreamCache.StreamCacheCreateFunction

the function to be used to create an instance of the choosen stream cache

setVisibleSignaturePropertiesback to summary
public void setVisibleSignatureProperties(String name, String location, String reason, int preferredSize, int page, boolean visualSignEnabled)

Set visible signature properties for new signature fields.

Parameters
page:int

the signature should be placed on (1-based)

setVisibleSignaturePropertiesback to summary
public void setVisibleSignatureProperties(String name, String location, String reason, boolean visualSignEnabled)

Set visible signature properties for existing signature fields.

setVisibleSignDesignerback to summary
public void setVisibleSignDesigner(String filename, int x, int y, int zoomPercent, InputStream imageStream, int page) throws IOException

Open the PDF, create and set the visible signature designer for a new signature field.

Parameters
filename:String

path of the PDF file

x:int

position of the signature field

y:int

position of the signature field

zoomPercent:int

increase (positive value) or decrease (negative value) image with x percent.

imageStream:InputStream

input stream of an image.

page:int

the signature should be placed on (1-based)

setVisibleSignDesignerback to summary
public void setVisibleSignDesigner(int zoomPercent, InputStream imageStream) throws IOException

Set visible signature designer for an existing signature field.

Parameters
zoomPercent:int

increase (positive value) or decrease (negative value) image with x percent.

imageStream:InputStream

input stream of an image.

signPDFback to summary
public void signPDF(File inputFile, File signedFile, String tsaUrl) throws IOException

Sign pdf file and create new file that ends with "_signed.pdf".

Parameters
inputFile:File

The source pdf document file.

signedFile:File

The file to be signed.

tsaUrl:String

optional TSA url

signPDFback to summary
public void signPDF(File inputFile, File signedFile, String tsaUrl, String signatureFieldName) throws IOException

Sign pdf file and create new file that ends with "_signed.pdf".

Parameters
inputFile:File

The source pdf document file. It will be opened if it hasn't been opened before in setVisibleSignDesigner(java.lang.String, int, int, int, java.io.InputStream, int).

signedFile:File

The file to be signed.

tsaUrl:String

optional TSA url

signatureFieldName:String

optional name of an existing (unsigned) signature field

usageback to summary
private static void usage()

This will print the usage for this program.