Hello, I'm using mail merge in document editor. Once I click "Merge Document" it merges document with sample data which I produced. All good till now. After made some changes on document and clicked merge document then saving document to server. when I re-open document, it shows sample data instead of merge fields. Is there a way to reverse merging while saving?
https://ej2.syncfusion.com/aspnetcore/DocumentEditor/MailMerge#/bootstrap5
That is sample I'm using.
Hello, I alternatively changed merge function for download file.
modified server side merge function to make save merged document as preview.docx
right after redirecting to sample download url with javascript at client side.
That is downloading merged document which saved recently.
May can be helpful for others
Here is client side merge function, it same as from sample but 1 change
function mergeDocument() {
documenteditor.saveAsBlob("Docx").then(function (blob) {
var fileReader = new FileReader();
fileReader.onload = function () {
var base64String = fileReader.result;
var data = {
fileName: documenteditor.documentName + '.docx',
documentData: base64String
}
showHideWaitingIndicator(true)
var httpRequest = new XMLHttpRequest();
httpRequest.open('Post', '@Url.Action("MailMerge", "Admin")', true);
httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200 || httpRequest.status === 304) {
//documenteditor.open(httpRequest.responseText);
window.location = '@Url.Action("Preview", "Admin")'; //here is line added to redirect on successfull response code
} else {
// Failed to merge document
ejs.popups.DialogUtility.alert({
title: 'Information',
content: 'Failed to merge document.',
showCloseIcon: true,
closeOnEscape: true,
});
}
showHideWaitingIndicator(false);
}
};
httpRequest.send(JSON.stringify(data));
}
fileReader.readAsDataURL(blob);
});
}
Here is Merging function on server side, saves as preview.docx
[AcceptVerbs("Post")]
[HttpPost]
[Route("Admin/MailMerge")]
public string MailMerge([FromBody] ExportData exportData)
{
Byte[] data = Convert.FromBase64String(exportData.documentData.Split(',')[1]);
MemoryStream stream = new MemoryStream();
using var fs = new FileStream(Path.Combine(env.WebRootPath, "Content", "preview.docx"), FileMode.Create);
stream.Write(data, 0, data.Length);
stream.Position = 0;
var data2 = new MergeStructure()
{
LabelInvoice = NonModelResources.Invoice.GetNameAttribute(),
LabelInvoiceTo = NonModelResources.InvoiceTo.GetNameAttribute(),
LabelOrderDate = NonModelResources.OrderDate.GetNameAttribute(),
LabelOrderID = NonModelResources.OrderID.GetNameAttribute(),
LabelPrice = NonModelResources.Price.GetNameAttribute(),
LabelProduct = NonModelResources.Product.GetNameAttribute(),
LabelQuantity = NonModelResources.Quantity.GetNameAttribute(),
LabelThankYou = NonModelResources.ThankYouForBusiness.GetNameAttribute(),
OrderDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
OrderID = "1",
ProductName = NonModelResources.TextTranslation.GetNameAttribute(),
Quantity = "1",
ShipAddress = "home address",
ShipCity = "Istanbul",
ShipCountry = "Turkey",
ShipName = "Creas Creas",
ShipPostalCode = "12345",
Total = "1,720.00"
};
var data3 = new List<MergeStructure>();
data3.Add(data2);
try
{
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);
document.MailMerge.RemoveEmptyGroup = true;
document.MailMerge.RemoveEmptyParagraphs = true;
document.MailMerge.ClearFields = true;
document.MailMerge.Execute(data3);
document.Save(fs, Syncfusion.DocIO.FormatType.Docx);
}
catch (Exception)
{ }
return "";
}And Preview function to download saved document
public IActionResult Preview()
{
var path = Path.Combine(env.WebRootPath, "Content", "preview.docx");
if (System.IO.File.Exists(path))
{
return File(System.IO.File.ReadAllBytes(path), "application/octet-stream", "preview.docx");
}
return NotFound();
}