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.Context
import android.net.Uri
import android.widget.Toast
import com.pspdfkit.catalog.R
import com.pspdfkit.catalog.SdkExample
import com.pspdfkit.configuration.activity.PdfActivityConfiguration
import com.pspdfkit.document.DocumentSource
import com.pspdfkit.document.PdfDocumentLoader
import com.pspdfkit.document.providers.AssetDataProvider
import com.pspdfkit.signatures.SignerOptions
import com.pspdfkit.signatures.SigningManager
import com.pspdfkit.signatures.getPrivateKeyFromFile
import com.pspdfkit.signatures.loadCertificateFromStream
import com.pspdfkit.ui.PdfActivityIntentBuilder
import com.pspdfkit.utils.PdfLog
import java.io.File
import java.security.PrivateKey
import 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.