Class CreateVisibleSignature
java.lang.Object
org.apache.pdfbox.examples.signature.CreateSignatureBase
org.apache.pdfbox.examples.signature.CreateVisibleSignature
- All Implemented Interfaces:
SignatureInterface
This is an example for visual signing a pdf.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PDDocumentprivate booleanprivate SignatureOptionsprivate final PDVisibleSigPropertiesprivate PDVisibleSignDesigner -
Constructor Summary
ConstructorsConstructorDescriptionCreateVisibleSignature(KeyStore keystore, char[] pin) Initialize the signature creator with a keystore (pkcs12) and pin that should be used for the signature. -
Method Summary
Modifier and TypeMethodDescriptionprivate PDSignaturefindExistingSignature(PDDocument doc, String sigFieldName) Get the function to be used to create an instance of the choosen stream cache.booleanstatic voidArguments are [0] key store [1] pin [2] document that will be signed [3] image of visible signaturevoidsetLateExternalSigning(boolean lateExternalSigning) Set late external signing.voidSet the function to be used to create an instance of the choosen stream cache.voidsetVisibleSignatureProperties(String name, String location, String reason, boolean visualSignEnabled) Set visible signature properties for existing signature fields.voidsetVisibleSignatureProperties(String name, String location, String reason, int preferredSize, int page, boolean visualSignEnabled) Set visible signature properties for new signature fields.voidsetVisibleSignDesigner(int zoomPercent, InputStream imageStream) Set visible signature designer for an existing signature field.voidsetVisibleSignDesigner(String filename, int x, int y, int zoomPercent, InputStream imageStream, int page) Open the PDF, create and set the visible signature designer for a new signature field.voidSign pdf file and create new file that ends with "_signed.pdf".voidSign pdf file and create new file that ends with "_signed.pdf".private static voidusage()This will print the usage for this program.Methods inherited from class org.apache.pdfbox.examples.signature.CreateSignatureBase
getCertificateChain, isExternalSigning, setCertificateChain, setExternalSigning, setPrivateKey, setTsaUrl, sign
-
Field Details
-
signatureOptions
-
visibleSignDesigner
-
visibleSignatureProperties
-
lateExternalSigning
private boolean lateExternalSigning -
streamCache
-
doc
-
-
Constructor Details
-
CreateVisibleSignature
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- is a pkcs12 keystore.pin- is the pin for the keystore / private key- Throws:
KeyStoreException- if the keystore has not been initialized (loaded)NoSuchAlgorithmException- if the algorithm for recovering the key cannot be foundUnrecoverableKeyException- if the given password is wrongCertificateException- if the certificate is not valid as signing timeIOException- if no certificate could be found
-
-
Method Details
-
isLateExternalSigning
public boolean isLateExternalSigning() -
setLateExternalSigning
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.- Parameters:
lateExternalSigning-
-
getStreamCacheCreateFunction
Get the function to be used to create an instance of the choosen stream cache.- Returns:
- the function to be used to create an instance of the choosen stream cache
-
setStreamCacheCreateFunction
public void setStreamCacheCreateFunction(RandomAccessStreamCache.StreamCacheCreateFunction streamCache) Set the function to be used to create an instance of the choosen stream cache.- Parameters:
streamCache- the function to be used to create an instance of the choosen stream cache
-
setVisibleSignDesigner
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- path of the PDF filex- position of the signature fieldy- position of the signature fieldzoomPercent- increase (positive value) or decrease (negative value) image with x percent.imageStream- input stream of an image.page- the signature should be placed on (1-based)- Throws:
IOException
-
setVisibleSignDesigner
Set visible signature designer for an existing signature field.- Parameters:
zoomPercent- increase (positive value) or decrease (negative value) image with x percent.imageStream- input stream of an image.- Throws:
IOException
-
setVisibleSignatureProperties
public void setVisibleSignatureProperties(String name, String location, String reason, int preferredSize, int page, boolean visualSignEnabled) Set visible signature properties for new signature fields.- Parameters:
name-location-reason-preferredSize-page- the signature should be placed on (1-based)visualSignEnabled-
-
setVisibleSignatureProperties
public void setVisibleSignatureProperties(String name, String location, String reason, boolean visualSignEnabled) Set visible signature properties for existing signature fields.- Parameters:
name-location-reason-visualSignEnabled-
-
signPDF
Sign pdf file and create new file that ends with "_signed.pdf".- Parameters:
inputFile- The source pdf document file.signedFile- The file to be signed.tsaUrl- optional TSA url- Throws:
IOException
-
signPDF
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- The source pdf document file. It will be opened if it hasn't been opened before insetVisibleSignDesigner(java.lang.String, int, int, int, java.io.InputStream, int).signedFile- The file to be signed.tsaUrl- optional TSA urlsignatureFieldName- optional name of an existing (unsigned) signature field- Throws:
IOException
-
findExistingSignature
-
main
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- Parameters:
args-- Throws:
KeyStoreExceptionCertificateExceptionIOExceptionNoSuchAlgorithmExceptionUnrecoverableKeyException
-
usage
private static void usage()This will print the usage for this program.
-