The Mail
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. engine is able to merge images in the same way like merge fields or form fields in a document template. Image placeholders are added at design-time and merged with data from files, databases or memory.
Image Placeholders
An image placeholder is basically an image that is added to a template with a given Name ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ FrameBase Class
╰ Name Property
Gets or sets a name for the frame. . The data column must contain either the binary image data as a byte array, an object of type System.Drawing.Image, a file name, a hex or Base64 encoded string. These options cover most typical image representations in databases. Merged images are scaled up or down to match the width of the placeholder image in the template.
Adding Placeholders with UI
To insert an image placeholder, a pre-designed RibbonBar can be used:
After the placeholder has been added, the name of the object can be defined in the Frame Formatting ribbon tab.
In case a data source is already loaded using the Load
╰ Web.MVC Namespace
╰ TextControl Class
╰ LoadDataFromJson Method
Uses data given as a JSON string as a data source. method (MVC) or using the JavaScript method load
╰ JavaScript API
╰ TXTextControl Object
╰ loadJsonData Method
Loads JSON data which is used to create table and field name menu items in the mailings ribbon tab. , the image name can be directly defined through the right-click context menu.
Adding Placeholders Programmatically
Image placeholders can be also inserted programmatically using the JavaScript API. A placeholder is nothing more than an image with a name that should match the data column in the used data source. The following code inserts an image placeholder at a character location with the name image1.
var imageData = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAyNy4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3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
} | |
}) |
Merging Placeholders
Image placeholders are merged automatically when calling one of the Merge methods of MailMerge. The following simple JSON data source is used in the following example:
[ | |
{ | |
"image1" : "iVBORw0KGgoAAAANSUhEUgAAAS0AAABHCAIAAADdmYYxAAAOzklEQVR42u2d+XsURRrH/QN213PxeHR9dN31BjwQhAVEBVS8EXBx1VUQwxUJIsFA5AgoCAqEKx6BhBzkMndCYu5gyEFAEswFuY/JOTM5Z3IN2Xdtn7Lp7qqunklkZp73+9Qv6amq7q6qT9VbVW91rhlBoVBXW9dgEaBQyCEKhUIOUSjkEIVCIYcoFHKIQqGQQxQKOUShUMghCoUcolAo5BCFQg5RKBRyiEIhhygUCjlEoZBDFAqFHKJQyCEKhUIOUSh75PDg8VT+0G7oJilPnanQlLasUkfSXqptIdcLi2s0PXFrRxdJm3P2IlyJSj7L/xjnS+s13a63t7esrCwrKzM6OiowMODYsaO+vt9D8PM7FhYWdvJkYn5+XnV1tdls1pTt4OBgXV1dYeGZ1NSUmJiY8PCw0NCQiIjwmJjolJQUuF5bWwtxOHPr7jEzXrmgqIqdXLEqoVSFX/sHhjRVNE8gt9ZUd34RpyJOnoGnrdfpbWn0VXWthwPT3l///ZTXt90xze1PDy0Two2Prhj/wqZXP9y/1TsamtawxcKTG618jkf+pIFD8hA8oajs90a8fmeoprSB0TniR39y/jbh+j2zPtEbe/mfeMGqg0LC26euaWwxwJXn/ruH/zHEjYCturrahIR4gTqekJiYWF1dZVGrvNbW1vT0NMBYNUNgHiiF+OqP2tTBeOW7Z64zdvUxkitWJZSq8Gtnd5+miuYJ5Naa6k4cHpjz6Zb9UW36bv6Wc/ny5cjkwlmLd3Le4p9Pr9/lk9Db18/OllY+8IT2ziEIhkfofoSfoGfifNyAqBySIXSNVtQlD4ednZ3x8XH8BIrDiRPBHR0ditn29fUBV1ZkCAlt4RDChxuPORmHQrh1iut3IZk8LediTctTi7+w4hZ3zfg4+sezY87hjkOxkkAy8vz6B8lPLe1d8sqDopRnIg9ihgWBbUDuBR0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UriaHp85UkDWM7QfVMwmJzyPPdoD+fa4RG77XxrkDEZ5Y8KbrYVq1yU+HMD7QoClIlmds4XDkyi8DOQqHjJmb8LULuY9beZWO5qOrKdBOD9sLh9CdPDl/m2pIuHJeZ+zqu/cZd7IKz/PVGbGZAdPl4vKGP5JDAb/Fa47QjmkzOBQ6EZpnDOch+v1+P2rdcWZzCFpEnwjZ7TnghmYDgyv51zGENQiepSlauPHRFWCvMiwLu+DQOj9vss0KL8n51RlhJ4BYU4wlx1HkkB8/oXdY4u5L88I7X1pPPnCsKdw/e0OGbBl5VDiERyWfZnYUDkFpp0sZnyQ9EpSuWI9u24Nph8gYYdbinSUi7xmn4hB6F04LU6749PMk7dodwWPKYWpOCfEuYIcZi3ZAy1P9Rx3DFgvYqPw0PjjXw9s/xdQ/qOoqYB2HI0oHPu2fQxCY6LRCu26CC82Zu6K62XVr4K1TXFVLHjh/ccnXYMSprtWNFYdQeUJg+3wYOntJTJ4gPkbY0t7FeReaZSKkpf3fnw5Dj/ym1gnIKSiqglYF9ffyB3sFG3vqG17QgN5e+81W7+ik7AuML/DRBCYAAAZm9uTXtor/5dC4x1c98qLnglUHd/rEFxbXcBYO2AWKZ6/N/epnjqGdwURdnpbzfBAUsuKt2U7YRHAXxeSqFUd7bCEw5izCJDM9txRKGGoQZpsT53k+MOdTCNMWeL3u4u2+KzQ0IZ//P9gMDVt4zsGzA/7/QxTq6gs5RKGQQxQKhRyiUMghCoVCDlEo5BCFQiGHKBRyiEKhkEMUCjlEoVDIIQqFHKJQKOQQhUIOUSgUcohCIYcoFAo5RKGQQxQKhRyiUPao/wGX3M0uFVqECgAAAABJRU5ErkJggg==" | |
} | |
] |
The image data is part of the data source as a base64 encoded string.
The following code uses the MailMerge method to merge the image data into the placeholder:
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { | |
tx.Create(); | |
tx.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat); | |
using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge()) { | |
mailMerge.TextComponent = tx; | |
var jsonData = System.IO.File.ReadAllText("data.json"); | |
mailMerge.MergeJsonData(jsonData); | |
} | |
} |
Image References
Images can be also merged from file paths. The following data source contains an image file name:
[ | |
{ | |
"image1" : "result.png" | |
} | |
] |
In this case, the MailMerge class expects this image as a physical file at the location defined through the Search
╰ DocumentServer Namespace
╰ MailMerge Class
╰ SearchPath Property
Specifies a directory name where sub-templates and images (for image merging) should be searched. property.
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { | |
tx.Create(); | |
tx.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat); | |
using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge()) { | |
mailMerge.TextComponent = tx; | |
mailMerge.SearchPath = Application.StartupPath; | |
var jsonData = System.IO.File.ReadAllText("data.json"); | |
mailMerge.MergeJsonData(jsonData); | |
} | |
} |
Removing Empty Images
In case the data source doesn't contain an image with the given name in the template, the placeholder is maintained by default. To remove empty image placeholders, the Remove
╰ DocumentServer Namespace
╰ MailMerge Class
╰ RemoveEmptyImages Property
Specifies whether images which don't have merge data should be removed from the template or not. property can be set to true.
mailMerge.RemoveEmptyImages = true; |
Positioning and Object Size
The positioning and the insertion mode of the image placeholder is maintained during the merge process. If the destination image width is larger or smaller, the width is adjusted based on the width of the placeholder image.