Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created October 16, 2023 13:45
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/1adc796f7aafcf64d4e79cd28cfbc76d to your computer and use it in GitHub Desktop.
Save bjoerntx/1adc796f7aafcf64d4e79cd28cfbc76d to your computer and use it in GitHub Desktop.
[HttpGet]
public IActionResult MergeDocument()
{
// create a ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
// load the template
tx.Load("App_Data/template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
// create the mail merge engine
using (TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge())
{
// connect to ServerTextControl instance
mm.TextComponent = tx;
// merge data
var jsonData = System.IO.File.ReadAllText("App_Data/data.json");
mm.MergeJsonData(jsonData);
}
byte[] bDocument = null;
// save in the internal format
tx.Save(out bDocument, TXTextControl.BinaryStreamType.AdobePDF);
// create memory stream from byte array
MemoryStream stream = new MemoryStream(bDocument);
stream.Position = 0;
//Download Word document in the browser
return File( stream, "application/pdf", "results.pdf");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment