Not able to remove empty lines for merge fields in table from using Java aspose words

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.

@kselvaraj

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:

  1. Set Cleanup Options: Before executing the mail merge, configure the cleanup options to remove empty table rows.

  2. 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 is null, 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.

1 Like