MailMergeCleanupOption REMOVE_EMPTY_PARAGRAPHS empty line before the mergefield

Hi,

I have a problem with white lines before a mergefield, when it’s in an if structure and inserted as html.

The MailMergeCleanupOption REMOVE_EMPTY_PARAGRAPHS will remove the empty line before a field that is inserted as HTML.
With the new aspose version (16.10.0) that empty line is not removed when the mergefield (inserted as html) is in an if, when it’s just used as a simple mergefield it works the same. Our previous version where it worked correctly was 14.11.0.

Can you take a look at this please? I have attached a sample project with this problem.

Kind Regards,
Ann

Hi Ann,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14470. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi,

Do you have a solution for our problem, please? We have a new release later this week and we need to fix this. Thanks!

Kind regards,
Ann

Hi Ann,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-14470) as ‘Not a Bug’.

The problematic field has following structure:
{ IF "test" = "" "" "{ MERGEFIELD test }"}

During mail merge the HandleMergeFieldInsertHtml callback inserts following html fragment:
waarde 1
As result, the field result consists of 3 paragraphs:

  1. empty paragraph
  2. waarde 1
  3. empty paragraph

The empty line is not before the field. It is a part of the field result.

Hi,

But when the mergefield is not in an if structure: { MERGEFIELD test } (see also in the template example, I put both of them in it) then their is not an empty line before the value (when using MailMergeCleanupOption REMOVE_EMPTY_PARAGRAPHS). En the value is the same in both cases:
waarde 1
. How can we fix this please?

Kind regards,
Ann

Hi Ann,

Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. When html (
waarde 1
) is inserted in the IF field, the output will be as follow:

{ IF "test" = "" "" " waarde 1 "}

The field result consists of 3 paragraphs:

  1. empty paragraph (It is the part of the field result)
  2. waarde 1
  3. empty paragraph

Hi,

And can you help us with a work around for this please? Because all our existing documents will change otherwise and we don’t want that. Is their a workaround that we can write?

Kind regards,
Ann

Hi Ann,

Thanks for your inquiry. In your case, we suggest you following workaround for this issue.

  1. Please do not use MailMergeCleanupOptions.REMOVE_STATIC_FIELDS in mail merge cleanup options.
  2. Join the two paragraphs if the mail merge is inside IF field - paragraph of IF field and first paragraph inserted by insertHtml.
  3. Use FieldsHelper.convertFieldsToStaticText to replace IF filed with static text.

Please read about FieldsHelper.convertFieldsToStaticText from here:
How to Replace or Modify Hyperlinks and Replace Fields with Static Text

Hope this helps you.

Document doc = new Document(MyDir + "sjabloon-test-if-whitelines.doc");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);

doc.getMailMerge().setFieldMergingCallback(new FieldMergingCallback());

MailMergeDataSource2 dataSource = new MailMergeDataSource("root");
doc.getMailMerge().executeWithRegions(dataSource);
doc.updateFields();
FieldsHelper.convertFieldsToStaticText(doc, FieldType.FIELD_IF);

doc.save(MyDir + "out v16.11.0.docx");
public void fieldMerging(FieldMergingArgs args) throws Exception

{

    DocumentBuilder builder = new DocumentBuilder(args.getDocument());

    builder.moveToMergeField(args.getDocumentFieldName(), true, false);

    String fieldValue = (String) args.getFieldValue();

    Node node = builder.getCurrentNode();

    if(node != null && node.getPreviousSibling() != null)

    {

        Paragraph paragraph = builder.getCurrentParagraph();

        System.out.println(""+node.getText());

        builder.insertHtml(fieldValue, true);

        if (paragraph.getNextSibling() != null)

        {

            Paragraph nextPara = (Paragraph)paragraph.getNextSibling();

            // Move all content from the nextPara paragraph into the first.

            while (nextPara.hasChildNodes())

                paragraph.appendChild(nextPara.getFirstChild());

            nextPara.remove();

        }

    }

    else {

        builder.insertHtml(fieldValue, true);

    }

    args.setText("");

}

Hi,

Thanks for the workaround, but I noticed some problems now when using listitems with multiple levels: <ul><li>item one</li><ul><li>subitem one</li><li>subitem two</li></ul><li>item two</li></ul>

The first item has no bullet in front of it because of this workaround.

aspose-listitems-if-problem.zip (12.1 KB)

I have added the output document: sjabloon-test-listitems-if-output-17.4.doc and the template that I’m using: sjabloon-test-listitems-if.doc and also a test project with that piece of code to test it.

Can you help me solve this please?
Thanks a lot!

Kind regards,
Ann

@AnnBaert,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 17.8 with shared HTML snippet. We have not found any issue with output document. Please use Aspose.Words for Java 17.8.

If you still face problem, please create simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue on our side and provide you more information.

@tahir.manzoor,

Thanks for your quick response. I see no difference when using the latest Aspose.Words version 17.8.

I have a simple Java application to reproduce the problem: aspose-listitems-if-problem2.zip (12.3 KB)

What compilation errors in the source code do you have? Then I will fix them. Because I don’t see any when compiling the project in the zip file. I also passed it to a colleague and also had no compilation errors.

I’m using following versions:
~>mvn -version
Java HotSpot™ 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in
8.0
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Maven home: C:\app\apache-maven-3.3.3
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_91\jre
Default locale: nl_BE, platform encoding: Cp1252
OS name: “windows 8.1”, version: “6.3”, arch: “amd64”, family: “dos”

Kind regards,
Ann

@AnnBaert,

Thanks for your inquiry. Please check the following commented code. This causes the issue that you are facing. There is no need of this code. Please remove this code from your application to get the desired output.

if (node != null && node.getPreviousSibling() != null)
{
    Paragraph paragraph = builder.getCurrentParagraph();

    builder.insertHtml(fieldValue, true);
    //    if (paragraph.getNextSibling() != null)
    //    {
    //        Paragraph nextPara = (Paragraph)paragraph.getNextSibling();
    //        // Move all content from the nextPara paragraph into the first.
    //        while (nextPara.hasChildNodes())
    //            paragraph.appendChild(nextPara.getFirstChild());
    //        nextPara.remove();
    //    }
}
else
{
    builder.insertHtml(fieldValue, true);
}

@tahir.manzoor,

I know that the code you put in comment causes the problem, but that was a workaround because we had problems that there was an empty line before our html (when using in an if, otherwise not), wich was different in previous versions of aspose. And we can not afford that the output is different with that upgrade to the new aspose version, that’s why we had to implement that workaround. See the beginning of this thread please. That’s why I put my comments here. Is there another way to fix that first problem then? Thanks!

Kind regards,
Ann

1 Like

@AnnBaert,

Thanks for your inquiry. In this case, the current paragraph node is not list item. You need to set the list value to it. Please check the following line in code snippet.

paragraph.getListFormat().setList(nextPara.getListFormat().getList());

if (node != null && node.getPreviousSibling() != null)
{
    Paragraph paragraph = builder.getCurrentParagraph();

    builder.insertHtml(fieldValue, true);
    if (paragraph.getNextSibling() != null)
    {
        Paragraph nextPara = (Paragraph)paragraph.getNextSibling();
        // Move all content from the nextPara paragraph into the first.
        while (nextPara.hasChildNodes())
            paragraph.appendChild(nextPara.getFirstChild());

        if (nextPara.isListItem())
            paragraph.getListFormat().setList(nextPara.getListFormat().getList());
        nextPara.remove();
    }
}
else
{
    builder.insertHtml(fieldValue, true);
}

@tahir.manzoor

Thanks for the help. This works now when the mergefield is in an IF.

I also need it in an IF COMPARE:

{ IF { = OR ( { COMPARE { MERGEFIELD item } = “item” }, { COMPARE { MERGEFIELD item2 } = “item” }) } = 1 “” “{ MERGEFIELD test}” }

But when using the mergefield with that piece of code, I get the following error: ‘java.lang.IllegalStateException: Cannot remove because there is no parent.’

I am not able to fix it so that there are no empty lines when using a COMPARE. Can you help with the COMPARE too please? I added the part with the compare in the template and added the project again. Thanks in advance!

Kind regards,
Ann

aspose-listitems-if-problem-compare.zip (10.5 KB)

@AnnBaert,

Thanks for your inquiry. We have tested the scenario using following code example and have not found the shared issue. We have added following IF condition in the code example. The output document is also attached with this post. output.zip (7.4 KB)

if(args.getDocumentFieldName().toLowerCase().equals("test"))
Document doc = new Document(MyDir + "sjabloon-test-listitems-if.doc");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
        | MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS | MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);

doc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback()
{
    public void fieldMerging(FieldMergingArgs args) throws Exception
    {
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());

        builder.moveToMergeField(args.getDocumentFieldName(), true, false);

        String fieldValue = (String) args.getFieldValue();

        Node node = builder.getCurrentNode();
        if(args.getDocumentFieldName().toLowerCase().equals("test"))
        {
            if(node != null && node.getPreviousSibling() != null)
            {
                Paragraph paragraph = builder.getCurrentParagraph();

                builder.insertHtml(fieldValue, true);
                if (paragraph.getNextSibling() != null)
                {
                    Paragraph nextPara = (Paragraph)paragraph.getNextSibling();
                    // Move all content from the nextPara paragraph into the first.
                    while (nextPara.hasChildNodes())
                        paragraph.appendChild(nextPara.getFirstChild());

                    if(nextPara.isListItem())
                        paragraph.getListFormat().setList(nextPara.getListFormat().getList());
                    nextPara.remove();
                }
            }
            else {
                builder.insertHtml(fieldValue, true);
            }
        }

        // Prevent empty newlines after inserting html (\r\n)
        if (builder.getCurrentParagraph() != null && (builder.getCurrentParagraph().toString(SaveFormat.TEXT).matches("\\s*"))) {
            builder.getCurrentParagraph().remove();
        }

        args.setText("");
    }

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

    }
});

MailMergeDataSource dataSource = new MailMergeDataSource("root");
doc.getMailMerge().executeWithRegions(dataSource);
doc.updateFields();

doc.save(MyDir + "output.docx");