I'm not sure if this is an issue with the Angular Document Editor or the .Net Core DocIODocument.
In our application rather than the users specifying separate mail merge items for each part of an address block we provide a merge field called "EEAddress" which builds the address block with line breaks - C# string with \r\\n between lines. (this should say \ r \ n , the \ n is lost on save)
When merging into the document this is adding an extra line above the merge field and losing all formatting.
So for example to get the address starting immediately below the Private and Confidential we have to set the template up as:
This results in:
As you can see, not only was an extra line added before the address was inserted (this isn't an issue in the data provided to the mail merge) the font colour changed and lost its styling (underline and italic) - these aren't actually required, just help emphasise the issue. We've got an issue where the font is larger than required.
Single line merge fields, such as
retain their styling:
As an extra test, to confirm the line break isn't in the provided data I tried this:
which resulted in:
If the line break had been in the merged in data, I would have expected the second exclamation mark to be after the post code.
Any ideas what is causing this and how to resolve it?
Cheers
Chris
Hi Chris,
We tried to reproduce the reported problem using
the given details. But it works properly at our end. For your reference, we
have attached the sample application which we used at our end to reproduce the
reported problem and it can be downloaded from the below link
Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Mail-merge1671614288
Could you please check and confirm us
whether you
have faced mentioned problem in above sample also. If no, then share us the modified sample of above along with input
Word document which used at your end to replicate the same problem. Also,
we request you to share the steps to reproduce the problem. Thereby, we
will proceed further to replicate the same problem at our end and will provide
the more details.
Note: If you have any confidential
data in your document, please replace with some dummy data and provide us the
same. We just need your document to recreate the problem you face.
Regards,
Suriya Balamurugan.
Hi Suriya,
cheers for your response. Your sample works perfectly so I've been playing with my code.
If I add code to save the DocIO document (either to FileStream or MemoryStream) the formatting issue disappears.
I wasn't saving it as I just need to send it to a web page using Synfusion's the DocumentEditor Angular component and, aside from this formatting issue, not saving the document doesn't appear to cause any issues.
The lines I've added which resolved the issue are between the code comments 'New Code' and 'End of New Code'. The OPLetterTemplate is just a Entity Framework model used to read a record from the database with the template stored as binary data in the Template property.
Any ideas why saving the document to a stream before loading it into an EJ2Document would make any difference?
// Read binary docx data into Memory then turn into DocIO document, required to run merge
MemoryStream stream = new MemoryStream(opLetterTemplate.Template);
DocIODocument docIODocument = new DocIODocument(stream, Syncfusion.DocIO.FormatType.Docx);
stream.Close();
string[] mergeFieldNames = docIODocument.MailMerge.GetMergeFieldNames();
mergeFieldNames = mergeFieldNames.Distinct().ToArray();
List<string> mergeFieldValues = new();
foreach (string mergeFieldName in mergeFieldNames)
{
string mergeFieldValue = await _opLetterParamMapper.GetLetterValue(mergeFieldName, opLetterValuesDto);
mergeFieldValues.Add(mergeFieldValue);
}
docIODocument.MailMerge.MergeField += new MergeFieldEventHandler(SyncfusionHelper.SetErrorsToRed);
docIODocument.MailMerge.Execute(mergeFieldNames, mergeFieldValues.ToArray());
docIODocument.UpdateDocumentFields();
// New Code
using (MemoryStream outputStream = new())
{
//Saves the Word document to memory stream - stops loss of mailmerge field formatting
docIODocument.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);
outputStream.Close();
}
// End of New Code
// Turn docIO document back into ej2document (SFDT format), the format required by the viewer in the web app.
EJ2Document ej2Document = EJ2Document.Load(docIODocument);
opLetterResultsDto.Letter = Newtonsoft.Json.JsonConvert.SerializeObject(ej2Document);
docIODocument.Dispose();
ej2Document.Dispose();
Oh, just to add - the sole formatting issue caused by not saving to memory stream was line breaks were being lost.
The additional line break and loss of font styling was the result of some hacky code added to the SetErrorsToRed merge field event handler, which I never looked at due to its name :S:
// For some reason line breaks in the merge field aren't resulting in line breaks in the output.
if (args.Text.Contains("\r\n"))
{
WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
string[] parts = args.Text.Split("\r\n");
args.Text = "";
foreach (string part in parts)
{
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendText(part);
}
}
Hi Chris,
Currently, we are checking your query. We will update further details on
or before 26th July 2022.
Regards,
Anto Nihil S
Hi Chris,
We have found that the code used in the SetErrorsToRed merge field event causes
the reported problem at your end.
In the given SetErrorsToRed merge field event code, you have simply added text range
and line breaks for each spitted string which added the text range and line
break at the last item of the paragraph without any formatting. So, the
reported problem occurs at your side. To resolve the reported issue, in SetErrorsToRed
merge field event, we suggest you to apply the merge field character format to the
newly created text range. Also suggest to inserting the text range and line
breaks in the proper index of the paragraph.
Please find the below modified code snippet to resolve the reported issue,
|
// For some reason line breaks in the merge field aren't resulting in line breaks in the output. if (args.Text.Contains("\r")) { WParagraph paragraph = args.CurrentMergeField.OwnerParagraph;
string[] parts = args.Text.Split("\r"); args.Text = "";
//Gets merge field character format. WCharacterFormat fieldCharFormat = args.CurrentMergeField.CharacterFormat; //Gets the merge field index. int fieldIndex = paragraph.ChildEntities.IndexOf(args.CurrentMergeField);
//Iterates each string part for (int i = 0; i <= parts.Length - 1; i++) { //Creates new textrange. WTextRange textRange = new WTextRange(args.Document); textRange.Text = parts[i]; //Applies merge field character format to the textrange. textRange.ApplyCharacterFormat(fieldCharFormat); //Inserts text at field index. paragraph.ChildEntities.Insert(fieldIndex, textRange);
if (i != parts.Length - 1) //Not insert the break after the last text range { //Inserts line break next to the textrange. fieldIndex++; Break lineBreak = new Break(args.Document, BreakType.LineBreak); paragraph.ChildEntities.Insert(fieldIndex, lineBreak); } fieldIndex++; } } |
Could you
please try the above mentioned solution and let us know whether the reported
issue gets resolved.
Regards,
Suriya Balamurugan.