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()
view raw test.cshtml hosted with ❤ by GitHub

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 "";
}
view raw test.cs hosted with ❤ by GitHub

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 TX Text Control .NET Server for ASP.NET
Web.MVC.DocumentViewer Namespace
SignatureBox Class
The SignatureBox class represents the shown signature box in the UI.
.

[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();
}
view raw test.cs hosted with ❤ by GitHub

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);
}
}
view raw test.cs hosted with ❤ by GitHub

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();
}
view raw test.cs hosted with ❤ by GitHub

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.