Merging Templates using DS Server in ASP.NET Core
This sample shows how to merge templates that have been created using DocumentEditor server-side using the DocumentProcessing package with DS Server.

The ASP.NET Core NuGet packages for DS Server provide all required components to create and merge templates with JSON data. This sample shows how to merge a template server-side with JSON data that has been created with the DocumentEditor.
The following model is used as the merge data model that is loaded into the editor to provide the proper merge field names and also for merging the final document server-side:
public class Report {
public List<Customer> Customers { get; set; } = new List<Customer>();
}
public class Customer {
public string Name { get; set; }
public string Firstname { get; set; }
public string Company { get; set; }
}
In the HomeController, JSON data is loaded and passed as the view model to the actual view that contains the editor:
public IActionResult Index() {
// load the merge data from JSON text file
List<Report> journal = JsonConvert.DeserializeObject<List<Report>>(
System.IO.File.ReadAllText("App_Data/data.json"));
return View(journal);
}
The actual view contains the editor and a button that calls a JavaScript function to merge the document. The JsonData property is used to load the model we created in the controller:
@using TXTextControl.DocumentServices.DocumentEditor;
@model List<DSServerMerge.Models.Report>
<div class="row">
<div class="col-12">
<input class="mb-3" type="button" onclick="mergeDocument()" value="Merge Template" />
@Html.TXTextControl().DocumentEditor(s => {
s.Width = "100%";
s.Height = "40rem";
s.ServiceUrl = "https://trial.dsserver.io";
s.OAuthSettings.ClientId = "";
s.OAuthSettings.ClientSecret = "";
s.JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Model);
}).Render()
</div>
</div>
The JavaScript function mergeDocument saves the document using the save
function mergeDocument() {
// save the document in the internal tx format
TXTextControl.saveDocument(TXTextControl.StreamType.InternalUnicodeFormat,
function (e) {
var serviceURL = "@Url.Action("Merge", "Home")";
// send document to controller
$.ajax({
type: "POST",
url: serviceURL,
data: {
document: e.data // the template
},
success: successFunc,
error: errorFunc
});
});
}
function successFunc(data, status) {
// load the resulting document back into the editor
TXTextControl.loadDocument(TXTextControl.StreamType.InternalUnicodeFormat, data);
}
function errorFunc() {
alert("Error");
}
In the controller method Merge, the document is being sent with JSON data to the DS Server Web API using the Merge method of DocumentProcessing.
[HttpPost]
public IActionResult Merge(Template template) {
// private OAuth settings
var os = new TXTextControl.DocumentServices.DocumentProcessing.OAuth.OAuthSettings(
"dsserver.EMRWLJ123pZirYLDNAF0gP7QzQaB8OfH",
"OGC24l8BcjVpc123Ht0gHJhDjtYJP70a");
// create a new DocumentProcessing object with OAuth settings
DocumentProcessing s_dp = new DocumentProcessing("https://trial.dsserver.io", os);
// load the merge data from JSON text file
List<Report> journal =
JsonConvert.DeserializeObject<List<Report>>(
System.IO.File.ReadAllText("App_Data/data.json"));
// create a new MergeBody object with merge data and template
MergeBody mergeBody = new MergeBody() {
Template = Convert.FromBase64String(template.Document),
MergeData = journal
};
// merge the document
var results = s_dp.Merge(mergeBody, ReturnFormat.TX).Result;
return Ok(results[0]);
}
After the sample is started, find the merge fields in the Reporting tab. Insert some text, fields or repeating merge blocks:
After clicking the button Merge Template, the merged document is loaded back into the editor:
Try this on your own, create a trial token and download the sample from our GitHub account.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- DS Server Trial Token
- Visual Studio 2019
Related Posts
JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps
The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen…
DS Server Released: On-Premise Document Services
We are very excited to announce the release of our new product DS Server. Bring document processing, editing, sharing, collaboration and creation to any app on any platform.
Using DS Server with pure HTML and JavaScript
DS Server comes with client-side libraries for an easy integration into web applications. But DS Server can be also used with pure JavaScript to integrate document processing into web applications.
JavaScriptASP.NET CoreDocument Editor
Format Painter in ASP.NET Core: Building Custom Text Formatting with TX Text…
This article demonstrates how to build a Format Painter feature using the TX Text Control Document Editor, implementing format detection, copy and paste operations, and custom style handling…
Why HTML is not a Substitute for Page-Oriented Formats like DOCX
In this blog post, we will discuss the limitations of HTML as a document format and explain why page-oriented formats, such as DOCX, remain essential for certain use cases. We will explore the…