Summary
I’m trying to hide a paragraph (conditional content) when a condition is not met. Even with MailMerge.CleanupOptions enabled, the paragraph space remains in the final DOCX. I need the paragraph to be fully removed (no blank line or extra spacing) if the condition evaluates to false.
Steps to Reproduce
- Open in.docx (attached). It has a conditional merge field/paragraph for coverage limit.
- Run the code below with the sample JSON.
- Save to out.docx (attached).
Code
var doc = new Document(@"in.docx");
// Read JSON
var jsonText = System.IO.File.ReadAllText(@"data.json");
var rootNode = System.Text.Json.Nodes.JsonNode.Parse(jsonText);
// Flatten JSON to name/value arrays (custom helper)
var data = JsonHelper.FlattenJson(rootNode);
var fieldNames = data.Keys.ToArray();
var fieldValues = data.Values.Cast<object>().ToArray();
// Cleanup options tried
doc.MailMerge.CleanupOptions =
MailMergeCleanupOptions.RemoveEmptyParagraphs |
MailMergeCleanupOptions.RemoveUnusedRegions |
MailMergeCleanupOptions.RemoveContainingFields;
// Execute mail merge
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(@"out.docx");
Sample JSON (data.json)
{
"formData": {
"coverageInformation": {
"limit": {
"value": "$15,000,000/$16,000,000"
}
}
}
}
Questions
- What is the recommended way in Aspose.Words to completely remove a conditional paragraph with no leftover spacing during Mail Merge?
- Is there a specific pattern we should use in the template (e.g.,
IF field structure, merge region boundary, or a LINQ Reporting <<if [cond]>> block) so that RemoveEmptyParagraphs actually deletes the paragraph?
- Are there any known limitations when the paragraph contains an
IF field result or extra runs/whitespace that prevent the cleanup from removing it? Any template examples would help.
Attachments
- in.docx (template)
- out.docx (result showing leftover space)
@lalit2207
To suppress blank space in your document when a condition is not satisfied, you can effectively use the MailMerge.CleanupOptions property. It is essential to check the conditions for each field before executing the mail merge to ensure that only relevant fields are included.
Here’s a refined approach:
- Before executing the mail merge, verify if the condition for each field is satisfied.
- If the condition is not met, skip that field or set it to an empty string.
Here’s a sample code snippet to illustrate this:
Document doc = new Document(@"in.docx");
var jsonText = System.IO.File.ReadAllText(@"data.json");
var rootNode = JsonNode.Parse(jsonText);
var data = JsonHelper.FlattenJson(rootNode);
var fieldNames = data.Keys.ToArray();
var fieldValues = data.Values.Cast<object>().ToArray();
// Create a list to hold the final field names and values
var finalFieldNames = new List<string>();
var finalFieldValues = new List<object>();
for (int i = 0; i < fieldNames.Length; i++)
{
// Check your condition here
if (/* condition for fieldValues[i] */)
{
finalFieldNames.Add(fieldNames[i]);
finalFieldValues.Add(fieldValues[i]);
}
}
// Set cleanup options to remove empty paragraphs and unused regions
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs
| MailMergeCleanupOptions.RemoveUnusedRegions
| MailMergeCleanupOptions.RemoveContainingFields;
// Execute mail merge with the filtered lists
doc.MailMerge.Execute(finalFieldNames.ToArray(), finalFieldValues.ToArray());
doc.Save(@"out.docx");
Make sure to replace /* condition for fieldValues[i] */ with your actual condition logic. This way, if a condition is not satisfied, the corresponding field will not take up any space in the final document.
If you need further assistance, feel free to ask!
@lalit2207
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28648
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
I have updated the template which should work with RemoveEmptyParagraphs, but it looks like a bug:
in.docx (14.9 KB).
As a workaround you can use:
var doc = new Document("in.docx");
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.Execute(new string[] { "value" }, new object[] { string.Empty });
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph p in paragraphs)
{
// Remove paragraphs with no visible text.
if (string.IsNullOrEmpty(p.ToString(SaveFormat.Text).Trim()))
p.Remove();
}
doc.Save("out.docx");