Remove empty paragraph and field merge callback

Hi.

I have successfully used

mailMerge.setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);

to remove empty mergeFields and the paragraph (complitly removing the line).

However I had to stop Aspose.Words from trimming my content (data). To fix this I implemented this callback function.

mailMerge.setFieldMergingCallback(new IFieldMergingCallback() {
    @Override
    public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(fieldMergingArgs.getDocument());
        builder.moveToMergeField(fieldMergingArgs.getDocumentFieldName());
        builder.write(fieldMergingArgs.getFieldValue().toString()); // skip trim
        fieldMergingArgs.setText("");
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {
    }
});

Separatly both of these two works find but not together. If I have them both at the same time the paragraphs that is supposed to be removed is left in my document. The mergefield is gone but not the paragraph.

I guess I have to either do the non trimming in another way or somehow be able to remove the paragraphs in another way.

Please advice.

Regards,
Ronny

Hi Ronny,

Thanks for your inquiry. Please use multiple MailMergeCleanupOptions as shown below. Hope this helps you.

mailMerge.setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS | MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS | MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);

If you still face problem, please attach the following resources here for testing:

  • Your input Word document
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a standalone Java application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Hi, again.

I started to create a standalone application to present for you guys. When I did that I found my problem.

The problem is that the data I send to Aspose.Words sometimes have an empty value. This becomes a problem inside the callback. There is two ways to fix this.

  1. Always be sure that data sent to Aspose.Words has values other than empty
  2. Put a simple test around the functionality inside the callback

Code for solution #2

mailMerge.setFieldMergingCallback(new IFieldMergingCallback() {
    @Override
    public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {

        Object val = fieldMergingArgs.getFieldValue();
        if(val != null && StringUtils.isNotBlank(val.toString())) {
            DocumentBuilder builder = new DocumentBuilder(fieldMergingArgs.getDocument());
            builder.moveToMergeField(fieldMergingArgs.getDocumentFieldName());
            builder.write(val.toString());
            fieldMergingArgs.setText("");
        }
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {

    }
});

Hi Ronny,

Thanks for your feedback. It is nice to hear from you that you have found the solution of your issue. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.