Suppress blank merge-field output and remove empty lines in JSON mail merge

Scenario:
We generate PDFs from Word templates using JsonMailMergeDataSource. The templates contain merge fields populated via JPath from our JSON payload.

Issue:
When a JSON property is empty or missing, the merge field is replaced with a blank value. This leaves an empty line/space in the document. We need the line to collapse so the following content shifts up (i.e., no visible blank line or extra spacing).

Question:
Is there a built-in setting/flag in Aspose.Words to suppress output and remove the paragraph/line when a merge field’s value is empty? (For example, an option similar to removing empty fields/paragraphs during mail merge.)

Expected behavior
If a field’s value is empty, the corresponding line/paragraph should be removed (or not rendered), with surrounding content flowing up automatically.

@lalit2207

To suppress blank merge-field output and remove empty lines during a JSON mail merge in Aspose.Words, you can utilize the IF field in your Word template. This allows you to conditionally display content based on whether the merge field is empty or not.

Cause: Blank merge fields can lead to unwanted empty lines in your final document, disrupting the flow of content.

Solution: You can wrap your merge fields with an IF field in your Word template. For example:

{ IF { MERGEFIELD YourFieldName } <> "" "{ MERGEFIELD YourFieldName }" "" }

This checks if YourFieldName is not empty. If it has a value, it displays it; otherwise, it outputs nothing. Additionally, ensure that your JSON data source is correctly set up to provide the necessary fields.

Next Steps: Implement the IF field in your Word template for each merge field that may be empty. Also, consider using the IF field to conditionally include paragraphs based on the merge field values to remove any empty paragraphs.

By following this approach, any empty merge fields will not create visible blank lines in your final document, allowing the surrounding content to flow up seamlessly.

If you have any further questions or need additional assistance, feel free to ask!

But adding too many ifs/else won’t take time in document processing and slow down the document generation

@lalit2207 Yes, you can configure Aspose.Words to remove empty paragraph produced upon executing mail merge. Please see MailMergeCleanupOptions.RemoveEmptyParagraphs option.

Let Me explain you my senario;
When generating a document, I need to print the following fields in multiple lines:
<<Name
<<Email
<<Address
<<Address2
<<Address3
Expected Behavior:

Address2 and Address3 may sometimes be blank.
If Address2 is blank, the line for Address2 should be suppressed, and Address3 should move up to take its place, ensuring there are no empty lines between Address and Address3.
The output should not contain any blank lines where address fields are missing.

Please advise on how to dynamically suppress blank lines using Aspose, so that the output document remains clean and properly formatted, regardless of which fields are empty.

@lalit2207 As it was mentioned, you can use MailMergeCleanupOptions.RemoveEmptyParagraphs option. In addition, you can use text before option of mergefield. For example see the attached template code and generated output document:

string[] names = new string[] { "Name", "Email", "Address", "Address2", "Address3" };
string[] values = new string[] { "James Bond", "j.bond@mi5.org", "London", "", "" };

Document doc = new Document(@"C:\Temp\in.docx");

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.Execute(names, values);

doc.Save(@"C:\Temp\out.docx");

in.docx (14.1 KB)

out.docx (11.1 KB)

Could you answer my earlier question: Will adding many if/else conditions in the document template to pick the correct value slow down generation, or will performance remain unaffected?

@lalit2207 There should not be any significant performance impact. But it is better to test with your real documents and data.