Lock Some Merge Fields to Apply Mail Merge Cleanup Options on Specific Text in Word Document | Java

Hello,

I want to apply document mail-merge cleanup options [REMOVE_CONTAINING_FIELDS, REMOVE_UNUSED_REGIONS, REMOVE_UNUSED_FIELDS, REMOVE_EMPTY_TABLE_ROWS] only on some of the text of template but not on all.
For example, there are 2 texts in template: {{#demoA}} and {{#demoB}}. And cleanup options should be applied only on {{#demoA}} but not on {{#demoB}} and keep it as it is in template.
And also, I have kept {{#demoB}} as hidden.

Can you please guide how to achieve the same?

@nobilex,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-22407. We will further look into the details of this requirement and will keep you updated here on the status of the linked ticket. We apologize for any inconvenience.

Hello @awais.hafeez ,

Thanks for the quick response.
But is there any work around or any alternative way to achieve the same, which can be used by the time, as it’s somewhat urgent to fix it.

Thanks.

@nobilex,

Please check the following code:

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

// first lock the merge field(s) that you want to set Cleanup Options for
for (Field field : doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {
        FieldMergeField mergeField = (FieldMergeField) field;
        if (mergeField.getFieldName().equals("demoB"))
            mergeField.isLocked(true);
    }
}

// executing normal mail merge will have no effect on locked merge field
// all other merge fields will be populated with their values from data source
doc.getMailMerge().execute(new String[]{"demoA", "demoB"}, new Object[]{"A", "B"});

// now, unlock the the merge fields that you want to set Cleanup Options for
for (Field field : doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_MERGE_FIELD) {
        FieldMergeField mergeField = (FieldMergeField) field;
        if (mergeField.getFieldName().equals("demoB"))
            mergeField.isLocked(false);
    }
}

// specify Cleanup Options and perform mail merge again
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
doc.getMailMerge().execute(new String[]{"demoA", "demoB"}, new Object[]{"A", "B"});

doc.save("C:\\Temp\\awjava-21.6.docx");

@nobilex,

Regarding WORDSNET-22407, we are not sure that we fully understand your requirement; however, if we understand it correctly, then we can offer implementing a cleanup callback interface looking like the following (rough prototype):

public interface IMailMergeCleanupCallback
{
    boolean OnEmptyParagraphRemoving(Paragraph paragraph);
    boolean OnUnusedRegionRemoving(String name, Field start, Field end);
    boolean OnUnusedFieldRemoving(Field field);
    boolean OnContainingFieldRemoving(Field field);
    boolean OnStaticFieldRemoving(Field field);
    boolean OnEmptyTableRowRemoving(Row row);
}

Can you please tell if such interface will satisfy your needs? Or, please elaborate your requirement in a bit more details. Thanks for your cooperation.

@nobilex,

Regarding WORDSNET-22407, it is to inform you that we have decided to close this issue because of lack of further information from your end. In case you may have further inquiries or may need any help in future, please let us know. Then we will create a new ticket and look into it further.