Products Technologies Demo Docs Blog Support Company

Common Web API Methods for Handling E-Signature Workflows in ASP.NET Core C#

Capturing electronic signatures and signing signature fields with certificates is a common feature of the TX Text Control Document Viewer. The most common server-side Web API methods for handling electronic signatures are described in this article.

Common Web API Methods for Handling E-Signature Workflows in ASP.NET Core C#

In addition to the standard Document Viewer signing process, which merges the signature image and returns the document, custom Web API calls can be used to handle custom requests. The RedirectUrlAfterSignature property can be used to provide an endpoint that is used to forward the signature data and the signed document.

The following MVC Razor code shows how to set the RedirectUrlAfterSignature property to pass the signature data to the HandleSignature controller method.

@using TXTextControl.Web.MVC.DocumentViewer

@Html.TXTextControl().DocumentViewer(settings => {
     settings.DocumentPath = "App_Data\\template.tx";
     settings.SignatureSettings = new SignatureSettings() {
      ShowSignatureBar = true,
      OwnerName = "Josh Jackson",
      SignerName = "Tim Typer",
      SignerInitials = "TT",
      UniqueId = "12345-12345-12345-12345",
      RedirectUrlAfterSignature = this.Url.Action(
        "HandleSignature",
        "Signature",
        null,
        Context.Request.Scheme,
        null),
      SignatureBoxes = new SignatureBox[] {
        new SignatureBox("txsign") { SigningRequired = true, Style = SignatureBox.SignatureBoxStyle.Signature },
        new SignatureBox("txsigninit") { SigningRequired = true, Style = SignatureBox.SignatureBoxStyle.Initials }
      }};
}).Render()

Custom Signing Process

After the user signs the document, the electronic signature is attached to the document and forwarded to the specified URL, along with the signature data such as the signature image and timestamp information.

Custom Signature Process

To handle the signature data, a custom HttpPost method must be implemented.

[HttpPost]
public string HandleSignature(
  [FromBody] TXTextControl.Web.MVC.DocumentViewer.Models.SignatureData data)
{
   var test = data.SignedDocument.Document; // signed document

   // ...

   return "";
}

SignatureData Object

The Document Viewer calls this endpoint and passes an object of type SignatureData in the payload.

Custom Signature Process

This SignatureData object can be used to process the signed document in a custom process without sending the document back from the client side for further processing.

Signing Signature Fields

The following HttpPost method loads the signed document and applies a certificate to all signature fields by looping through the SignatureBoxes.

[HttpPost]
public IActionResult HandleSignature([FromBody] SignatureData data) {

  byte[] bPDF;

  // create temporary ServerTextControl
  using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
    tx.Create();

    // load the document
    tx.Load(Convert.FromBase64String(data.SignedDocument.Document),
      TXTextControl.BinaryStreamType.InternalUnicodeFormat);

    X509Certificate2 cert = new X509Certificate2("App_Data/textcontrolself.pfx", "123");

    var signatureFields = new List<DigitalSignature>();

    foreach (SignatureBox box in data.SignatureBoxes) {
      signatureFields.Add(new DigitalSignature(cert, null, box.Name));
    }

    TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
      CreatorApplication = "Your Application",
      SignatureFields = signatureFields.ToArray()
    };

    // store the PDF in the database or send it to the client
    tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDFA, saveSettings);

    // alternatively, save the PDF to a file
    tx.Save("App_Data/signed.pdf", TXTextControl.StreamType.AdobePDFA, saveSettings);
  }

  // return any value to the client
  return Ok();
}

The result of this implementation is that all signature fields are digitally signed. The signature field image is the default image created by the Document Viewer.

Creating documents with TX Text Control

Flatten Form Fields

If the document contains form fields, you may want to flatten the form fields and convert the selected values to text in the final PDF document. Before exporting to PDF using the Save method, the following helper function can be called in the above code.

private void FlattenFormFields(ServerTextControl textControl) {
   int fieldCount = textControl.FormFields.Count;

   for (int i = 0; i < fieldCount; i++) {
      TextFieldCollectionBase.TextFieldEnumerator fieldEnum =
        textControl.FormFields.GetEnumerator();
      fieldEnum.MoveNext();

      FormField curField = (FormField)fieldEnum.Current;
      textControl.FormFields.Remove(curField, true);
   }
}

Custom Signature Stamps

The following code uses the signature image captured by the Document Viewer. It uses this image only as the signature stamp.

[HttpPost]
public IActionResult CustomSignature([FromBody] SignatureData data) {

  byte[] bPDF;

  // create temporary ServerTextControl
  using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
    tx.Create();

    // load the document
    tx.Load(Convert.FromBase64String(data.SignedDocument.Document), TXTextControl.BinaryStreamType.InternalUnicodeFormat);

    var signatureImage = System.Text.Encoding.UTF8.GetString(
       Convert.FromBase64String(data.SignatureImage));

    var stamp = Encoding.ASCII.GetBytes(signatureImage);

    // create a memory stream from SVG
    using (MemoryStream ms = new MemoryStream(
       stamp, 0, stamp.Length, writable: false, publiclyVisible: true)) {

      foreach (SignatureField field in tx.SignatureFields) {
        field.Image = new SignatureImage(ms);
      }
    }

    X509Certificate2 cert = new X509Certificate2("App_Data/textcontrolself.pfx", "123");

    var signatureFields = new List<DigitalSignature>();

    foreach (SignatureBox box in data.SignatureBoxes) {
      signatureFields.Add(new DigitalSignature(cert, null, box.Name));
    }

    TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
      CreatorApplication = "Your Application",
      SignatureFields = signatureFields.ToArray()
    };

    // store the PDF in the database or send it to the client
    tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDFA, saveSettings);

    // alternatively, save the PDF to a file
    tx.Save("App_Data/signed.pdf", TXTextControl.StreamType.AdobePDFA, saveSettings);
  }

  // return any value to the client
  return Ok();
}

All of the signature fields will be merged with the captured signature image only, without the default signature stamp.

Creating documents with TX Text Control

From our GitHub repository, you can download the sample project that contains the controller methods for your own tests.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET Server
  • Visual Studio 2022

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreDocument Viewer

Securing the Signature Endpoint with Custom ActionFilterAttributes

The HttpPost endpoint to which the signed document is forwarded can be in the same application or a completely different application. This tutorial will show you how to secure this endpoint…


ASP.NETASP.NET CoreDocument Viewer

High-Performance Text Replacement in Large DOCX Files using C# .NET

Learn how to efficiently replace text in large DOCX files using C# .NET and the ServerTextControl component from Text Control. This article demonstrates the performance benefits of using the…


ASP.NETASP.NET CoreDocument Viewer

Document Viewer 33.2.1 Released: New Event and Bug Fixes

This service pack includes important bug fixes and improvements to enhance the stability and performance of the Document Viewer. In addition, a new event has been introduced to provide developers…


AngularASP.NETBlazor

Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…

This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…


ASP.NETBlazorASP.NET Core

TX Text Control Document Editor and Viewer for Blazor Released

We are very happy to announce the immediate availability of TX Text Control packages for Blazor. This article gives an overview of the available packages and how to use them.