Add digital signature to PDF in Kotlin for Android
Register and use a digital certificate for signing a PDF document. The certificate password is: test. Get additional resources by visiting our guide on adding a digital signature to a PDF in Android and our guide on generating a self-signed certificate for signing in Android(opens in a new tab)
/* * Copyright © 2020-2025 PSPDFKit GmbH. All rights reserved. * * The PSPDFKit Sample applications are licensed with a modified BSD license. * Please see License for details. This notice may not be removed from this file. */
package com.pspdfkit.catalog.examples.kotlin
import android.content.Contextimport android.net.Uriimport android.widget.Toastimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.DocumentSourceimport com.pspdfkit.document.PdfDocumentLoaderimport com.pspdfkit.document.providers.AssetDataProviderimport com.pspdfkit.signatures.SignerOptionsimport com.pspdfkit.signatures.SigningManagerimport com.pspdfkit.signatures.getPrivateKeyFromFileimport com.pspdfkit.signatures.loadCertificateFromStreamimport com.pspdfkit.ui.PdfActivityIntentBuilderimport com.pspdfkit.utils.PdfLogimport java.io.Fileimport java.security.PrivateKeyimport java.security.cert.X509Certificate
/** * An example that shows how to digitally sign a PDF document using [SigningManager]. * This is a Simple implementation where user provides Private key in [SignerOptions]. */class DigitalSignatureExample(context: Context) : SdkExample(context, R.string.digitalSignatureExampleTitle, R.string.digitalSignatureExampleDescription) {
override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { val assetName = "Form_example.pdf"
val unsignedDocument = PdfDocumentLoader.openDocument(context, DocumentSource(AssetDataProvider(assetName))) val privateKey = getPrivateKey(context) val certificate = getCertificate(context) val signatureFormFields = unsignedDocument.documentSignatureInfo.signatureFormFields val outputFile = File(context.filesDir, "signedDocument.pdf") outputFile.delete() // make sure output is deleted from previous runs. val signerOptions = SignerOptions.Builder(signatureFormFields[0], Uri.fromFile(outputFile)) .setPrivateKey(privateKey) .setCertificates(listOf(certificate)) .setType(digitalSignatureType) .build()
/** [SignerOptions] contains all the required configuration for [SigningManager]*/ SigningManager.signDocument( context = context, signerOptions = signerOptions, onFailure = { e -> Toast.makeText(context, "Error launching example. See logcat for details.", Toast.LENGTH_SHORT).show() PdfLog.e("DigitalSignatureExample", e, "Error while launching example.") } ) { val signedDocument = Uri.fromFile(outputFile) // Load and show the signed document. val intent = PdfActivityIntentBuilder.fromUri(context, signedDocument) .configuration(configuration.build()) .build() context.startActivity(intent) } }
private fun getPrivateKey(context: Context): PrivateKey { val privateKeyFile = context.assets.open("digital-signatures/self-signed/demo.pkcs8") return getPrivateKeyFromFile(privateKeyFile) } private fun getCertificate(context: Context): X509Certificate { val certificateFile = context.assets.open("digital-signatures/self-signed/demo.cer") return loadCertificateFromStream(certificateFile) }}
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.