Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created August 4, 2015 12:49
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/0cb87d3917007731836c to your computer and use it in GitHub Desktop.
Save bjoerntx/0cb87d3917007731836c to your computer and use it in GitHub Desktop.
private void MergeTemplate(string templateName, string JsonData)
{
// creating a new ServerTextControl and MailMerge class
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
MailMerge mailMerge = new MailMerge();
mailMerge.TextComponent = tx;
// load the template
mailMerge.LoadTemplate(Server.MapPath("templates/" + templateName),
FileFormat.InternalUnicodeFormat);
// create a new DataSet object
DataSet ds = new DataSet();
XmlDocument doc;
// convert the JSON string to an XML document
// object using Newtonsoft.Json
try
{
doc = JsonConvert.DeserializeXmlNode(JsonData);
lblError.Visible = false;
}
catch (Exception exc)
{
lblError.Visible = true;
lblError.Text = exc.Message;
DocumentViewer1.Visible = false;
return;
}
// convert the XML to a DataSet
XmlNodeReader reader = new XmlNodeReader(doc);
ds.ReadXml(reader, XmlReadMode.Auto);
// merge the template with the DataSet
mailMerge.Merge(ds.Tables[0]);
// load the resulting document into the DocumentViewer
byte[] data;
mailMerge.SaveDocumentToMemory(out data, BinaryStreamType.InternalUnicodeFormat, null);
DocumentViewer1.LoadDocumentFromMemory(data, FileFormat.InternalUnicodeFormat);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment