Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created December 21, 2022 13:58
Show Gist options
  • Save bjoerntx/94a48659272080ad3638e87083485d43 to your computer and use it in GitHub Desktop.
Save bjoerntx/94a48659272080ad3638e87083485d43 to your computer and use it in GitHub Desktop.
[HttpGet]
public string PDFPrepare(bool flatten) {
byte[] bDocument;
// create temporary ServerTextControl
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
// loading the template
tx.Load(Server.MapPath("~/App_Data/Documents/patient_history.tx"), StreamType.InternalUnicodeFormat);
// fire up the MailMerge engine
using (MailMerge mailMerge = new MailMerge()) {
mailMerge.TextComponent = tx; // connect to ServerTextControl
// pre-selecting form fields
if (flatten == false)
mailMerge.FormFieldMergeType = FormFieldMergeType.Preselect;
else // flatten form fields
mailMerge.FormFieldMergeType = FormFieldMergeType.Replace;
string jsonData = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/Documents/data.json"));
// merge JSON into fields
mailMerge.MergeJsonData(jsonData);
}
// export to PDF
tx.Save(out bDocument, BinaryStreamType.InternalUnicodeFormat);
}
// return as Base64 encoded string
return Convert.ToBase64String(bDocument);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment