Created
August 4, 2015 12:49
-
-
Save bjoerntx/0cb87d3917007731836c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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