I have merge fields in a table inside word document template and I am trying to populate the rows only when the data is present. I am able to clean up the merge field references in the template after my document has finished processing but the table has still empty rows which is not getting removed. Attached is the template image where I am looking to populate fields highlighted in yellow.
To remove empty lines or rows in a table during a mail merge operation using Aspose.Words for Java, you can utilize the cleanup options provided by the library. Specifically, you should set the CleanupOptions
to include the removal of empty table rows.
Here’s how you can implement this:
-
Set Cleanup Options: Before executing the mail merge, configure the cleanup options to remove empty table rows.
-
Execute Mail Merge: Perform the mail merge operation with your data.
Here’s a code example demonstrating this process:
import com.aspose.words.Document;
import com.aspose.words.MailMergeCleanupOptions;
public class MailMergeExample {
public static void main(String[] args) throws Exception {
// Load the document
Document doc = new Document("path/to/your/template.docx");
// Set cleanup options to remove empty table rows
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
// Execute the mail merge with your data
doc.getMailMerge().execute(new String[] { "Field1", "Field2", "Field3" },
new Object[] { "Value1", null, "Value3" });
// Save the output document
doc.save("path/to/your/output.docx");
}
}
In this example:
- Replace
"path/to/your/template.docx"
with the path to your Word document template. - The
execute
method is called with an array of field names and their corresponding values. If a field value isnull
, the corresponding row will be removed if the cleanup options are set correctly.
By using the MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
, any rows that contain empty merge fields will be automatically removed during the mail merge process, ensuring that your final document does not contain unwanted empty rows.
For more detailed information, you can refer to the documentation on cleaning up before or during a mail merge operation here [1].
Sources:
[1]: Clean Up Before or During a Mail Merge Operation
@kselvaraj Could you please attach your template document here for testing? We will check the issue and provide you more information. Have you tried using MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
?
yes I have tried [MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
] and it is not working. Attached here is my template
POLICY_ALP.docx (66.3 KB)
I cannot remove empty rows inside the table for merge fields or the regular ## fields if data is not present. Although clean up options for removing the text works though.
I do the clean up after processing all fields in the document as a final step
asposeDoc.getMailMerge().deleteFields(); // delete unused fields
asposeDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
asposeDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);
asposeDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
asposeDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
@kselvaraj Mail merge cleanup options should be set before executing mail merge. Please see the following code:
DataTable dt = new DataTable("Surcharges");
dt.getColumns().add("surchargeName");
dt.getColumns().add("surchargeAmount");
dt.getRows().add("First", "1");
dt.getRows().add("Second", "2");
// Empty row
dt.getRows().add("", "");
dt.getRows().add("Third", "3");
Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
| MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
doc.getMailMerge().executeWithRegions(dt);
doc.save("C:\\Temp\\out.docx");
Thanks it works now.
One more question - Is there a way I can remove the entire table based on a condition? Attached image has a section that I would need to remove based on a condition from Java? How to set any conditions in template and code to remove the same?
@kselvaraj If you would like to remove the table is it is empty, you can use MailMergeCleanupOptions.REMOVE_EMPTY_TABLES
. It specifies whether to remove from the document tables that contain mail merge regions that were removed using either the REMOVE_UNUSED_REGIONS
or the REMOVE_EMPTY_TABLE_ROWS
option.
In my last attached image, I have the TableStart:fwOption at the row level. For instance, If I wanted to remove the entire Additional Coverage Information(Line item next to it and the table below should I need to set a parent table on top of ‘Additional Coverage Information’ and set the mail merge to remove empty table? Is there a conditional statement like ‘IF’ I can place in template and aspose can render the document based on that?
@kselvaraj You can use standard MS Word IF field and wrap the required content into such field. Aspose.Words will update the field value.
Thanks! I will try this out.
Hi. I am trying to see how should I create a IF field in MS word template that I have? I need to set the field based on condition and want to set the same in my mail merge before execute. So I tried to create a IF condition based on a mail merge field, it is forcing me to connect me to a data source before I can do Inserts in Mailings tab.
I can see a similar use case in the link If Statement in Microsoft Word
How to create the mail merge fields and link them to the IF field?
I am using a Merge field named to check the count and if it is true it should display Additional Coverage Information and table below. Problem here is the IF Curly braces ends right after >0 and I am not able to manually edit the same to include my contents
@kselvaraj Here is a simple template created manually of how it can work:
Document doc = new Document("Field sample - MERGEFIELD.docx");
doc.MailMerge.Execute(new string[] { "field" }, new object[] { 1 });
doc.UpdateFields();
doc.Save("output.docx");
Field sample - MERGEFIELD.docx (19.5 KB)
Can I know how did you create the IF condition in your template? IF text appears in my result
Quote_ALP.docx (62.7 KB)
@kselvaraj You want to hide some rows of the table using the IF field, but this is not possible. In this case you can create a part of the table as a separate table and set a hidden font for the IF field.
Here’s an updated document for a quick example, you just need to put your mergfield inside the if field:
Quote_ALP.docx (63.4 KB)
That’s awesome. It works. I am a Java developer and I am not good in adding conditions in word template. Can you please let me know how did you create the IF field and set a hidden font?
@kselvaraj In MS Word I create a simple IF field:
Then remove one of the output results depending on condition and insert a table between a quotation marks:
After that I update field and press Alt+F9 to hide field codes and on the empty paragraph with field I set Font->Hidden.