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

public Class CreateVisibleSignature2

extends CreateSignatureBase
Class Inheritance
Imports
java.awt.Color, java.awt.geom.AffineTransform, .Rectangle2D, java.io.ByteArrayInputStream, .ByteArrayOutputStream, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, .RandomAccessFile, java.security.KeyStore, .KeyStoreException, .NoSuchAlgorithmException, .UnrecoverableKeyException, java.security.cert.CertificateException, .X509Certificate, java.util.Calendar, .List, org.apache.pdfbox.Loader, org.apache.pdfbox.cos.COSName, org.apache.pdfbox.io.IOUtils, org.apache.pdfbox.pdmodel.PDDocument, .PDPage, .PDPageContentStream, .PDResources, org.apache.pdfbox.pdmodel.common.PDRectangle, .PDStream, org.apache.pdfbox.pdmodel.font.PDFont, .PDType1Font, .Standard14Fonts.FontName, org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject, org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject, org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget, .PDAppearanceDictionary, .PDAppearanceStream, org.apache.pdfbox.pdmodel.interactive.digitalsignature.ExternalSigningSupport, .PDSignature, .SignatureInterface, .SignatureOptions, org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm, .PDField, .PDSignatureField, org.apache.pdfbox.util.Hex, .Matrix, org.bouncycastle.asn1.x500.RDN, .X500Name, org.bouncycastle.asn1.x500.style.BCStyle, .IETFUtils

This is a second example for visual signing a pdf. It doesn't use the "design pattern" influenced PDVisibleSignDesigner, and doesn't create its complex multilevel forms described in the Adobe document Digital Signature Appearances, because this isn't required by the PDF specification. See the discussion in December 2017 in PDFBOX-3198.
Authors
Vakhtang Koroghlishvili, Tilman Hausherr

Field Summary

Modifier and TypeField and Description
private File
private boolean
private SignatureOptions

Constructor Summary

AccessConstructor and Description
public
CreateVisibleSignature2(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 PDRectangle
private InputStream
createVisualSignatureTemplate(PDDocument srcDoc, int pageNum, PDRectangle rect, PDSignature signature)

private PDSignature
findExistingSignature(PDAcroForm acroForm, String sigFieldName)

public File
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
setImageFile(File imageFile)

public void
setLateExternalSigning(boolean lateExternalSigning)

Set late external signing.

public void
signPDF(File
The source pdf document file.
inputFile
,
File
The file to be signed.
signedFile
,
Rectangle2D
rectangle from a human viewpoint (coordinates start at top left)
humanRect
,
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.
inputFile
,
File
The file to be signed.
signedFile
,
Rectangle2D
rectangle from a human viewpoint (coordinates start at top left)
humanRect
,
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

imageFileback to summary
private File imageFile
lateExternalSigningback to summary
private boolean lateExternalSigning
signatureOptionsback to summary
private SignatureOptions signatureOptions

Constructor Detail

CreateVisibleSignature2back to summary
public CreateVisibleSignature2(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

createSignatureRectangleback to summary
private PDRectangle createSignatureRectangle(PDDocument doc, Rectangle2D humanRect)
createVisualSignatureTemplateback to summary
private InputStream createVisualSignatureTemplate(PDDocument srcDoc, int pageNum, PDRectangle rect, PDSignature signature) throws IOException
findExistingSignatureback to summary
private PDSignature findExistingSignature(PDAcroForm acroForm, String sigFieldName)
getImageFileback to summary
public File getImageFile()
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

setImageFileback to summary
public void setImageFile(File imageFile)
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.

signPDFback to summary
public void signPDF(File inputFile, File signedFile, Rectangle2D humanRect, 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.

humanRect:Rectangle2D

rectangle from a human viewpoint (coordinates start at top left)

tsaUrl:String

optional TSA url

signPDFback to summary
public void signPDF(File inputFile, File signedFile, Rectangle2D humanRect, 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.

signedFile:File

The file to be signed.

humanRect:Rectangle2D

rectangle from a human viewpoint (coordinates start at top left)

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.