Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created March 25, 2024 16:11
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/273a4222e0deeeda39b36ab4e6cd02f2 to your computer and use it in GitHub Desktop.
Save bjoerntx/273a4222e0deeeda39b36ab4e6cd02f2 to your computer and use it in GitHub Desktop.
[HttpGet]
[Route("Prepare")]
public DocumentData Prepare()
{
// create a new CustomerData object and serialize it to JSON
var customer = new CustomerData {
Name = "Smith",
FirstName = "John",
Street = "Main Street 1",
City = "New York",
Zip = "10001",
Country = "United States"
};
var customerJson = JsonSerializer.Serialize(customer);
byte[] documentBytes;
using (var tx = new ServerTextControl()) {
tx.Create();
tx.Load("App_Data/template.tx", StreamType.InternalUnicodeFormat);
// create a new MailMerge object and merge the JSON data
var merge = new MailMerge {
TextComponent = tx,
FormFieldMergeType = FormFieldMergeType.Preselect,
RemoveEmptyFields = false,
RemoveEmptyImages = false
};
merge.MergeJsonData(customerJson);
// save the document
tx.Save(out documentBytes, BinaryStreamType.InternalUnicodeFormat);
}
var documentBase64 = Convert.ToBase64String(documentBytes);
return new DocumentData { Name = "template.pdf", Document = documentBase64 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment