Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 29, 2015 14:26
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/02dce2e6a07f54a360d4 to your computer and use it in GitHub Desktop.
Save bjoerntx/02dce2e6a07f54a360d4 to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------
** Post method
** // POST api/merge?format=[PDF|PDFA|DOCX|DOC]
**---------------------------------------------------------------*/
public ResponseResults Post([FromBody]dynamic mailMergeRequestObject,
[FromUri]string format)
{
// create a new ResponseResults object that contains the
// resulting document as a byte[]
ResponseResults results = new ResponseResults();
// helper object of type 'MailMergeRequestObject' to extract the
// JSON data (template and data)
MailMergeRequestObject mmroJSONResult =
(MailMergeRequestObject)JsonConvert.DeserializeObject(
Convert.ToString(mailMergeRequestObject),
typeof(MailMergeRequestObject));
// create a new ServerTextControl
using (TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
{
tx.Create();
// create a new MailMerge instance
using (TXTextControl.DocumentServer.MailMerge mm =
new TXTextControl.DocumentServer.MailMerge())
{
mm.TextComponent = tx;
// load the template from the helper object
mm.LoadTemplateFromMemory(mmroJSONResult.Template,
TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat);
// create a new DataSet and the given XML data into it
DataSet ds = new DataSet();
XmlDocument doc =
JsonConvert.DeserializeXmlNode(mmroJSONResult.Data);
XmlNodeReader reader = new XmlNodeReader(doc);
ds.ReadXml(reader, XmlReadMode.Auto);
// merge the template with the DataSet
mm.Merge(ds.Tables[0]);
byte[] data;
BinaryStreamType streamType = BinaryStreamType.AdobePDF;
// define the export format based on the given Uri parameter
switch (format.ToUpper())
{
case "PDFA":
streamType = BinaryStreamType.AdobePDFA; break;
case "DOCX":
streamType = BinaryStreamType.WordprocessingML; break;
case "DOC":
streamType = BinaryStreamType.MSWord; break;
}
// save the document and fill the 'ResponseResults' object
mm.SaveDocumentToMemory(out data, streamType, null);
results.Document = data;
}
}
// return the 'ResponseResults' object
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment