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

Free Support Forum - aspose.com

Remove Empty Merge fields

Hi,


I want to remove empty merge fields from the document. However, to remove empty regions is working fine.

step1: Here If the First Name is empty.
String[] mergeFields = new String[numberOfFields];
Object[] valueFields = new Object[numberOfFields];
int i=0;
if (linkedInProfile != null) {
if (!WamHelper.isNullOrEmpty(linkedInProfile.getLinkedInFirstName())) {
mergeFields[i] = “FirstName”;
valueFields[i++]=linkedInProfile.getLinkedInFirstName();
}

Step2: First name needs to removed here but is not removing….
doc.getMailMerge().setRemoveEmptyRegions(true);
// Fill the fields in the document with user data.
doc.getMailMerge().deleteFields();
doc.getMailMerge().execute(mergeFields, valueFields);

Hi

Thanks for your request. Could you please attach your template, output and expected output document? We will check them and provide you more information.

Also, as I can see, in your code, you are calling deleteFields() before executing mail merge. But you should call it after to remove all non-merged fields.

Best regards,

HI,


Please see attached template, output and expected output document. In the expected output, I want to remove Summary, Speciality, Association and Certifications. If you see, it is showing headings with two blank lines. It is very critical that we fix it. We are going to be in production in two weeks. I am unable to figure out how to fix it.

-Jaspal Sandhu

Hi

Thanks for your request. To resolve this problem, you can use “Text to be inserted before” option of mergefield. In this case you can insert a needed text as a part of mergefield. Please see the following field code:

{ MERGEFIELD Summary \b "Summary:" }

Then you can try using MailMerge.RemoveEmptyParagraphs option to remove empty paragraphs during mail merge process. See the following link for more information:

http://www.aspose.com/documentation/java-components/aspose.words-for-java/com/aspose/words/mailmerge.html#RemoveEmptyParagraphs

Please let me know if you need more assistance, I will be glad to help you.

Best regards,

How to do I specify

<span style=“font-size:14.0pt;mso-bidi-font-size:12.0pt;font-family:“Arial”,“sans-serif””>Summary

«Summary»

as

{ MERGEFIELD Summary \b "Summary:" }

I am not clear on this...

How do I code It?

If I put this field like { MERGEFIELD Summary \b “Summary:” }


I have Summary merge field defined as

<span style=“font-size:14.0pt;mso-bidi-font-size:12.0pt;font-family:“Arial”,“sans-serif””>Summary<o:p></o:p>

«Summary»


Questions is If Summary is empty , How can i remove Summary Title and blank line

-Jaspal Sandhu

Hi Jasper,

Thanks for your inqury.

The reason why the paragraphs are not being removed is because the executeCustomLogicOnEmptyRegions method only calls the handler when an unmerged region in encountered. The fields in your document like "Summary" are not regions and are just simple merge fields therefore your handler never gets called in that case.

You can add a bit of extra code to the methods below to allow the handler to be called for simple mail merge fields as well.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

public static void executeCustomLogicOnEmptyRegions(Document doc, IFieldMergingCallback handler, ArrayList regionsList) throws Exception {

doc.getMailMerge().setRemoveEmptyRegions(true);

// Set the user's handler which is called for each unmerged region.

doc.getMailMerge().setFieldMergingCallback(handler);

String[] fieldNames = doc.getMailMerge().getFieldNames();

Object[] values = new Object[fieldNames.length];

Arrays.fill(values, "Field");

doc.getMailMerge().execute(fieldNames, values);

doc.getMailMerge().executeWithRegions(createDataSourceFromDocumentRegions(doc, regionsList));

}

This code is to be inserted anywhere into your handler, this will generally remove any simple mail merge field left over along with the title before it if there is one and the empty paragraph which preceeds that.

if("".equals(args.getTableName()))

{

Paragraph para = (Paragraph)args.getField().getStart().getAncestor(NodeType.PARAGRAPH);

// Check if this section has been removed from the document already.

if (para.getParentNode() != null) {

// Remove the title for this section

if (para.getPreviousSibling() != null && para.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH) {

Paragraph previousPara = (Paragraph)para.getPreviousSibling();

previousPara.remove();

}

// Remove the blank line preceding the title

if (para.getPreviousSibling() != null && para.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH) {

Paragraph previousPara = (Paragraph)para.getPreviousSibling();

if (previousPara.getText() != null){

if (previousPara.getText().trim().length() == 0){

previousPara.remove();

}

}

}

para.remove();

}

}

Also please note with your original code you only were required to change the call from "Summary".equals(args.getTableName) to "Summary".equals(args.getFieldName) in order for it to work.

Thanks,

Thanks,