Sign PDF with Kotlin

This guide will assist you in integrating DocuSeal document e-signing capabilities into your Android app. With DocuSeal, you can create document templates from your PDF files, send them to the signers, and receive signed copies. Templates can be created either using our web UI or through the API. For instance, you can design a document template with various field types using HTML and CSS or PDF upload. More information on this can be found in our guides.

How to sign PDFs with Kotlin

  • Create a DocuSeal account DocuSeal account.
  • Create a document template using web UI or API.
  • Embed a signing form in your app.

Embedded Signing Form

To embed a signing form in your Android app, you need to use a WebView. The WebView will load the script, and the user can sign the document using the DocuSeal Signing Form. Therefore, you first need to add dependencies to your project's build.gradle file.

To do this, open the build.gradle file in the app folder of your project and add the following code to the dependencies block:

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'androidx.webkit:webkit:1.4.0'

Now you need to allow your app to use the internet. To do this, open the AndroidManifest.xml file in the app/src/main folder and add the following code:

<uses-permission android:name="android.permission.INTERNET"/>

If necessary, create a layout file activity_main.xml in your project and add a WebView to it:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

This is all you need to create the layout. Below is the code that you can use as an example to add a signing form to your app:

package com.example.docusealapp

import android.os.Bundle
import androidx.activity.ComponentActivity
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient

class MainActivity : ComponentActivity() {
    private lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView = findViewById(R.id.webView)
        setupWebView()
        loadHtmlContent()
    }

    private fun setupWebView() {
        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                return false
            }
        }
    }

    private fun loadHtmlContent() {
        val html = """
            <html>
                <head>
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <script src="https://cdn.docuseal.co/js/form.js"></script>
                </head>
                <body>
                    <docuseal-form
                        id="docusealForm"
                        data-src="https://docuseal.co/d/LEVGR9rhZYf86M"
                        data-email="signer@example.com">
                    </docuseal-form>
                </body>
            </html>
        """
        webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)
    }
}

After adding this code to your app, you will see such a signing form in your app. However, you can customize its appearance and behavior using many parameters. You can read more about this in our documentation or check out a complete example of an Android app in our repository.

Conclusions

In this guide, we have only covered the most basic example of using DocuSeal in an Android app. DocuSeal has many other capabilities that you can use to enhance your app. For example, you can embed not only the Signing form on your app but also the Form builder. You can also use the API to fully automate the process of generating custom documents using HTML and then sending them for signer without using the DocuSeal web UI.