Created
June 9, 2023 13:31
-
-
Save bjoerntx/e72751a5cfb07df18d7d3d75752a324a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public string ExportPDF([FromBody] TXTextControl.Web.MVC.DocumentViewer.Models.SignatureData data) { | |
byte[] bPDF; | |
// Extract the completed form field values and create Claim model. | |
// This data could be stored in a database at this point | |
Claim completedClaim = new Claim(); | |
foreach (PropertyInfo propertyInfo in completedClaim.GetType().GetProperties()) { | |
var dataPoint = data.FormFields.Find(i => i.Name == propertyInfo.Name); | |
if (dataPoint != null && propertyInfo.PropertyType == typeof(DateTime) && | |
DateTime.TryParse(dataPoint.Value, out var parsedDate)) { | |
propertyInfo.SetValue(completedClaim, parsedDate); | |
} | |
else if (dataPoint != null) { | |
propertyInfo.SetValue(completedClaim, dataPoint.Value); | |
} | |
} | |
// 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); | |
FlattenFormFields(tx); | |
// save the document as PDF | |
tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDFA); | |
} | |
return Convert.ToBase64String(bPDF); | |
} | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment