We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail Merge & Conditionally Remove Merge Fields' Parent Paragraph in Word Document using Java

Hi

I use Aspose.Words for Java to make Microsoft Word document which has mailmerge fields like below.
How can I remove empty 2 lines as below on RESULT:Useslip.docx.
I don’t want to change the Word template because we have so many ones
in our system.
Please help us. Thanks.
-----------------------------------------------------------------------------------------

(Word template:Template.docx)

ADDRESS:{MERGEFIELD “Address”}
NAME:{MERGEFIELD “Name”}

DATE SHOP AMUONT
{MERGEFIELD “Date”} {MERGEFIELD “Store”} {MERGEFIELD “Amount”}
{NEXT *MERGEFORMAT}{MERGEFIELD “Date”} {MERGEFIELD “Store”}
{MERGEFIELD “Amount”}
{NEXT *MERGEFORMAT}{MERGEFIELD “Date”} {MERGEFIELD “Store”}
{MERGEFIELD “Amount”}
{NEXT *MERGEFORMAT}{MERGEFIELD “Date”} {MERGEFIELD “Store”}
{MERGEFIELD “Amount”}
{NEXT *MERGEFORMAT}{MERGEFIELD “Date”} {MERGEFIELD “Store”}
{MERGEFIELD “Amount”}

TOTAL:{MERGEFIELD “Total”}

-----------------------------------------------------------------------------------------
(DATASOURCE:DataSource)

Address,Name,Date,Store,Amount,Total
“OSAKA-CITY”,“YAMADA”,“2011/11/01”,“SHOPabc”,“58,000”,“855,800”
“OSAKA-CITY”,“YAMADA”,“2011/11/02”,“SHOPjkl”,“550,000”,“855,800”
“OSAKA-CITY”,“YAMADA”,“2011/11/01”,“SHOPxyz”,“247,800”,“855,800”

-----------------------------------------------------------------------------------------
(RESULT:Useslip.docx)

ADDRESS:OSAKA-CITY
NAME:YAMADA

DATE SHOP AMOUNT
2011/11/01 SHOPabc 58,000
2011/11/02 SHOPjkl 550,000
2011/11/01 SHOxyz 247,800
(don’t need this line)
(don’t need this line)

TOTAL:855,800

-----------------------------------------------------------------------------------------
(CODE)

Document doc = new Document(getMyDir() + "Template.docx");
DataTable table = new DataTable(DataSource);
doc.getMailMerge().setRemoveEmptyParagraphs(true);
doc.getMailMerge().execute(table);
doc.getMailMerge().deleteFields();
doc.save(getMyDir() + "Useslip.docx", SaveFormat.DOCX);

Hello
Thanks for your request. Could you please attach your input and expected documents here? I will investigate the problem and provide you more information.
Best regards,

Hi AndreyN,
Thanks for your quick reply.
I’ll attach the files soon.

Hi there,

Thanks for your inquiry.

I managed to reproduce the issue on my side. Your request has been linked to the appropriate issue.

In the mean time you can replace the call to deleteField with the code below which should achieve what you are looking for:

Node[] fields = doc.getChildNodes(NodeType.FIELD_START, true).toArray();
for (FieldStart field : (Iterable) fields)
{
    if (field.getFieldType() == FieldType.FIELD_MERGE_FIELD &&
        field.getParentNode() != null)
        field.getParentNode().remove();
}

Thanks,

Hi Adam,
Thanks for your quick reply for this question.
I could manage it using the code which you provided.
I really thank you again.
Regards,
yama0120

Hi Andrey,
I could find the way to solve the problem with the quick reply from Adam.
I appreciate your reply.
Thanks again.
Regards,
Yama0120

Hi there,

It’s great things are working properly now. Please feel free to ask anytime you have any queries.

Thanks,

The issues you have found earlier (filed as WORDSNET-979) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(2)

Now we have another problem.
Using IFieldMergingCallback class, when mergefield value is null or “”, the entire paragraph is removed.
However if text other than mergefields are included in the same paragraph, we like to avoid removing.
Although the code shown in the following (eventHandler code) was implemented to realize above,
it does not operate, as meant.
Please instruct us the right way.
(Word document template : Template.docx)
Name: {MERGEFIELD “Name”}
Tel: {MERGEFIELD “TelNumber”}
Mail: {MERGEFIELD “MailAddress”}
Name: {NEXT *MERGEFORMAT}{MERGEFIELD “Name”}
Tel: {MERGEFIELD “TelNumber”}
Mail: {MERGEFIELD “MailAddress”}

-----------------------------------------------------------------------------------------------------------------------------
(Data source : source.csv)
Name,TelNumber,Adress,Node
“Mary Smith”,“03-1111-xxxx”,“xxx-oooo@geegle.co.jo”
“Jack Johnson”,“06-4444-xxxx”,""

-----------------------------------------------------------------------------------------------------------------------------
(Word document actual result : Result.docx)
Name: Mary Smith
Tel: 03-1111-xxxx
mail: xxx-oooo@geegle.co.jo
Name: Jack Johnson
Tel: 06-4444-xxxx
*↑Letters ‘mail :’ are removed.

-----------------------------------------------------------------------------------------------------------------------------
(Word document result to mean : Target.docx)
Name: Mary Smith
Tel: 03-1111-xxxx
090-2222-xxxx
mail: xxx-oooo@geegle.co.jo
Name: Jack Johnson
Tel: 06-4444-xxxx
mail:
-----------------------------------------------------------------------------------------------------------------------------
(eventHandler code)

class HandleMergeFieldRemoveEmptyLine implements IFieldMergingCallback
{
    
    public void fieldMerging(FieldMergingArgs args) throws Exception
    {
        String fieldValue = (String) args.getFieldValue();
        Paragraph parent = args.getField().getStart().getParentParagraph();
        // If the field value is null or empty string and the text of this paragraph is null or empty String then remove the entire paragraph.
        if (fieldValue == null || fieldValue.equals(""))
        {
            
            if (parent.getParentNode() != null)
            {
                
                if (parent.getText().equals("") || parent.getText() == null)
                {
                    parent.remove();
                }
            }
        }
        else if (fieldValue.trim() == null || fieldValue.trim().equals(""))
        {
            // Otherwise if the field value contains special chars only, then remove all content and leave a blank line
            parent.removeAllChildren();
        }
    }
    
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {}
}

I made new thread of this question.

Hi there,

Thanks for your inquiry.

We apologise for the late reply as we mistakenly missed your post. We have replied in your more recent post.

Thanks,

Hi,
That’s OK, no problem at all. We got kindly reply last week and are testing it.
Thank you very much.