Electronic and Hand-Drawn PDF Signatures in a .NET MAUI Mobile App with Office File API (v23.1)

Signing PDF files is an increasingly common usage requirement on mobile devices. This blog post will describe the differences between auto generated/"hand drawn" signatures and how to quickly incorporate signature/signing support in a .NET MAUI app using the DevExpress Office File API.

Draw_a_Custom_Signature

GitHub Example: DevExpress .NET MAUI Controls - Sign PDF Files

Note that the example above uses our free .NET MAUI suite and the DevExpress Office File API, which requires a purchase/license.

Electronic vs Hand-Drawn PDF Signatures

Though both types of signatures are similar, they have functional/practical differences. 

Electronic signature (e-sign) uses cryptographic methods to confirm that the file was sent from a specific source and was not changed once sent. The list below describes a simplified sequence of actions performed when you sign a file with e-sign:

  1. The sender calculates the hash code of the PDF file that they want to send.
  2. The sender creates a public/private key pair and encrypts the hash code with the private key.
  3. The sender sends the PDF file, the encrypted hash code (signature), public key, and a certificate. The certificate contains information about the sender. The recipient can use it to confirm authenticity of the public key.
  4. The recipient decrypts the encrypted hash code (signature) with the public key, calculates the document's hash code, and compares these hashes.
  5. If they are equal, the document is authentic. If the file was modified, the document hash code will not match the hash code decrypted with the public key from the signature.

For additional protection, you can use a certification service. Such services help make certain that the public key belongs to the user who sent the document.

Hand drawn signatures are much like "wet" signatures on paper. In the case of PDF, it is just an image inserted into a specific area of the document. You can combine both of these signature types to confirm the authenticity of hand drawn signatures.

Add a PDF Signature

Our Office File API includes classes designed to work with different document types (PDF, Word, Excel, HTML). In this blog post, I'll use the Office File API to sign PDF files with both signature types mentioned a moment ago.

The PdfDocumentProcessor class is the entry point for PDF file modification. In this project, we use the following members of this class:

As you know, Android and iOS applications are sandboxed. They have limited access to the device file system. To work with application bundle files, you need to copy them to a Cache or AppDataDirectory (used in this example) folder. In our case, these files are PDF and PFX certificate files. The following code sample defines the method that copies the specified PDF file from the application bundle to the AppDataDirectory folder:


public async Task<string> CopyWorkingFilesToAppData(string fileName) {
    using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(fileName);
    string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
    using FileStream outputStream = File.OpenWrite(targetFile);
    fileStream.CopyTo(outputStream);
    return targetFile;
}

Once implemented, you can use the PdfDocumentProcessor.LoadDocument method to open the PDF file.

The DevExpress Office File API allows you to detect PDF AcroForm fields and populate them as needed. We use the following members to locate the first available signature field:

In this particular usage scenario, PdfSignatureBuilder is another important class. It stores the following information about the signature itself:

  • PFX certificate file. Refer to the following Microsoft help topic for more information on how to create a PFX certificate: Create PFX certificate profiles using a certificate authority.
  • Location of the person who signs the document
  • Name of the person who signs the document
  • Reason for signing the document
  • Signature image that is embedded in the PDF file

As you can see in the animation at the beginning of this post, the user can "draw" a signature that will be embedded in the PDF file. This is achieved through the use of the DrawingView control. As its name implies, the control can save a user's drawing to an image - an image we later pass to the PdfSignatureBuilder.

Once the document is signed, the PdfDocumentProcessor.SaveDocument method saves the signed PDF file.

Should you have any questions about this post or the DevExpress Office File API, please submit a support ticket via the DevExpress Support Center. We'll be happy to follow up.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.