Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created February 18, 2016 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/822ffc3ad0eb28c1171c to your computer and use it in GitHub Desktop.
Save bjoerntx/822ffc3ad0eb28c1171c to your computer and use it in GitHub Desktop.
[HttpPost]
public string SignDocument(Document model)
{
byte[] document =
Convert.FromBase64String(model.BinaryDocument);
byte[] signatureDocument = null;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// use MailMerge to merge the signature template
using (MailMerge mailMerge = new MailMerge())
{
mailMerge.TextComponent = tx;
mailMerge.SearchPath = Server.MapPath("/Signature_Images/");
mailMerge.LoadTemplate(
Server.MapPath("/App_Data/documents/signature_template.tx"),
FileFormat.InternalUnicodeFormat);
// create a new signature object
Signature signature = new Signature();
signature.Name = "Peter Chadwick";
signature.SignatureImage =
signature.Name.ToLower().Replace(" ", "_") + ".png";
// merge and save the resulting document
mailMerge.MergeObject(signature);
mailMerge.SaveDocumentToMemory(
out signatureDocument,
BinaryStreamType.InternalUnicodeFormat,
null);
}
// load the original document from the editor
tx.Load(document, BinaryStreamType.InternalUnicodeFormat);
// find the "signature" text frame with the name "txsig"
foreach (TextFrame frame in tx.TextFrames)
{
if (frame.Name == "txsig")
{
frame.Tables.Clear();
frame.Selection.Start = 0;
frame.Selection.Length = -1;
// load the merged signature template into the
// text frame and save the complete document
frame.Selection.Load(signatureDocument,
BinaryStreamType.InternalUnicodeFormat);
tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
break;
}
}
}
// finally, return the signed document
return Convert.ToBase64String(document);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment