Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active January 27, 2020 10:13
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/aaece9139b5679355923670dc8f10445 to your computer and use it in GitHub Desktop.
Save bjoerntx/aaece9139b5679355923670dc8f10445 to your computer and use it in GitHub Desktop.
using myFirstAzureWebApp.Models;
using System;
using System.Data;
using System.Web.Mvc;
namespace myFirstAzureWebApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
// load MS Word merge fields and included merge blocks
TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings()
{
ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord,
LoadSubTextParts = true
};
// load the template
tx.Load(Server.MapPath("~/App_Data/invoice.docx"),
TXTextControl.StreamType.WordprocessingML, ls);
using (TXTextControl.DocumentServer.MailMerge mailMerge =
new TXTextControl.DocumentServer.MailMerge())
{
mailMerge.TextComponent = tx;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/App_Data/sample_db.xml"), XmlReadMode.Auto);
// merge data into template
mailMerge.Merge(ds.Tables[0]);
byte[] data = null;
tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
// return the document and the document name
DocumentView documentView = new DocumentView()
{
DocumentData = Convert.ToBase64String(data),
DocumentName = ls.LoadedFile
};
return View(documentView);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment