Customizing the electronic signature UI on Android
The Electronic Signatures UI can be customized to fit your needs.
You can customize aspects of signature creation, such as the available modes and their order, and the signing fonts to offer for typed signatures. You can also set the stroke thickness and opacity of created signatures.
These options are set via the ElectronicSignatureOptions class. You can change the available mode via signatureCreationModes, the fonts via setAvailableFonts, and the colors with signatureColorOptions.
Both signatureCreationModes and signatureColorOptions can also be configured on initialization using PdfConfiguration and PdfActivityConfiguration. setAvailableFonts is a companion function that applies to every signature UI, so it can be called at any point.
Setting available signature creation modes
signatureCreationModes accepts an array of List<SignatureCreationMode> members. Based on these entries, the UI will display the tabs specified in the array, respecting the order in which they were added. It defaults to [SignatureCreationMode.DRAW, SignatureCreationMode.IMAGE, SignatureCreationMode.TYPE].
Here’s an example offering Type, followed by Image, as signature creation modes:
// Using `PdfConfiguration`:val configuration = PdfConfiguration.Builder() .signatureCreationModes(listOf(SignatureCreationMode.DRAW, SignatureCreationMode.TYPE)) .build()
// Using `ElectronicSignatureOptions`:val signatureOptions = ElectronicSignatureOptions( signatureCreationModes = listOf(SignatureCreationMode.DRAW, SignatureCreationMode.TYPE), )// Using `PdfConfiguration`:final PdfConfiguration configuration = new PdfConfiguration.Builder() .signatureCreationModes(Arrays.asList(SignatureCreationMode.DRAW, SignatureCreationMode.TYPE)) .build();
// Using `ElectronicSignatureOptions`:final ElectronicSignatureOptions signatureOptions = new ElectronicSignatureOptions( SignatureSavingStrategy.SAVE_IF_SELECTED, SignatureColorOptions.fromDefaults(), Arrays.asList(SignatureCreationMode.DRAW, SignatureCreationMode.TYPE));You can also set the available signature colors with signatureColorOptions on the configuration builder:
... .signatureColorOptions(SignatureColorOptions.fromColorInt(Color.RED, Color.GREEN, Color.BLUE)) .build()... .signatureColorOptions(SignatureColorOptions.fromColorInt(Color.RED, Color.GREEN, Color.BLUE)) .build();Setting Available Fonts to Sign With
To configure the available fonts for the typing tab of the signing UI, use ElectronicSignatureOptions#setAvailableFonts. By default, four predefined fonts are bundled with the SDK. However, you can change, add, or remove these fonts however you like:
ElectronicSignatureOptions.setAvailableFonts( LinkedHashSet( listOf( Font("Caveat", ResourcesCompat.getFont(context, CAVEAT_FONT_RES)!!), Font("Pacifico", ResourcesCompat.getFont(context, PACIFICO_FONT_RES)!!), ) ))ElectronicSignatureOptions.Companion.setAvailableFonts( new LinkedHashSet<>( Arrays.asList( new Font("Caveat", Objects.requireNonNull(ResourcesCompat.getFont(context, CAVEAT_FONT_RES))), new Font("Pacifico", Objects.requireNonNull(ResourcesCompat.getFont(context, PACIFICO_FONT_RES))) ) ));Setting the stroke thickness and opacity
ElectronicSignatureOptions has defaultThickness and defaultAlpha properties. defaultThickness is the stroke thickness of drawn signatures in PDF points, and it defaults to 4. defaultAlpha is the opacity of drawn, typed, and image signatures, where 1 (the default) is fully opaque.
The signature dialog that opens from the signature tool or from a signature form field takes these values from the annotation configuration registry. Register an ink configuration for AnnotationTool.SIGNATURE to change them. It takes precedence over a configuration registered for AnnotationType.INK, which still applies to signatures when there’s no signature-specific one:
override fun onDocumentLoaded(document: PdfDocument) { super.onDocumentLoaded(document) requirePdfFragment().annotationConfiguration.put( AnnotationTool.SIGNATURE, InkAnnotationConfiguration.builder(this) .setDefaultThickness(2f) .setDefaultAlpha(0.8f) .build() )}@Overridepublic void onDocumentLoaded(@NonNull PdfDocument document) { super.onDocumentLoaded(document); requirePdfFragment().getAnnotationConfiguration().put( AnnotationTool.SIGNATURE, InkAnnotationConfiguration.builder(this) .setDefaultThickness(2f) .setDefaultAlpha(0.8f) .build());}The opacity of a created signature is stored in Signature.alpha and applied to the ink or stamp annotation created from it. Signatures saved to a signature storage by versions before 12.1 load fully opaque.
Stepping into the signature flow
To let users choose the thickness and opacity in a UI of your own, or to decide where a signature is placed, set PdfFragment#signatureCreationInterceptor. A SignatureCreationInterceptor has two methods:
onSignatureCreationRequested()is called before the signature dialog is shown. Callproceed()on the request, optionally with adjustedElectronicSignatureOptions, or callcancel().onSignaturePlacementRequested()is called after a signature is created, before it’s added to the document. Callproceed()on the request, optionally with another page index and bounding box, or callcancel()to add the signature yourself.
Resolve each request exactly once, either right away or after showing your own UI. Nothing happens until you do. Both methods proceed unchanged by default:
class MyActivity : PdfActivity(), SignatureCreationInterceptor {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requirePdfFragment().signatureCreationInterceptor = this }
override fun onSignatureCreationRequested(request: SignatureCreationRequest) { // Show the built-in dialog with a thinner, semi-transparent stroke. request.proceed(request.options.copy(defaultThickness = 2f, defaultAlpha = 0.8f)) }}public class MyActivity extends PdfActivity implements SignatureCreationInterceptor {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); requirePdfFragment().setSignatureCreationInterceptor(this); }
@Override public void onSignatureCreationRequested(@NonNull SignatureCreationRequest request) { // Show the built-in dialog with a thinner, semi-transparent stroke. final ElectronicSignatureOptions options = request.getOptions(); request.proceed(new ElectronicSignatureOptions( options.getSignatureSavingStrategy(), options.getSignatureColorOptions(), options.getSignatureCreationModes(), 2f, 0.8f)); }
@Override public void onSignaturePlacementRequested(@NonNull SignaturePlacementRequest request) { request.proceed(); }}For a complete example, see SignatureCreationInterceptorExample in the Catalog app. It asks for the thickness and opacity before the dialog opens and enables the user to pick where the signature goes.