Conditional display of paragraph

I want to display a text/paragraph based on some condition. I will put if else condition in my template and in Java will read that condition and ensure if this condition is true based on some business logic then the paragraph should be shown else do not show this paragraph.

For exampel: If text "The total number of mgs per dose form is determined by multiplying the dose of the oral formulation by the total sales volume of drug. " needs to display based on some condition
[IF test=“marketed”] show the above text. else show some other text

Kindly reply.

@diwanmail,

Thanks for your inquiry. Please ZIP and upload your simplified input Word document and expected Word document showing the desired output here for testing. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create this expected document by using MS Word application. We will then provide you code to achieve the same by using Aspose.Words for Java.

Aspose_Query.zip (36.7 KB)

Hi Hafeez,

Thanks for your reply. I have attached Aspose_Query.zip which has template and generated expected output. I need conditional text/paragraph display where in I can also read if condition in JAVA code and check against business logic. Please also guide me the best way to write this IF condition.

@diwanmail,

Please try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Some regular text");
builder.writeln();

// { IF {MERGEFIELD amount >= 100 "true text" "false text" }
Field field = builder.insertField("IF ", null);
builder.moveTo(field.getStart().getNextSibling().getNextSibling());
builder.insertField("MERGEFIELD amount", null);
builder.write(" >= 100 \"");
builder.write("true text");
builder.write("\" \"false text\"");

doc.getMailMerge().execute(new String[]{"amount"}, new Object[]{99});

doc.save("D:\\Temp\\awjava-18.4.docx");

Hi Hafeez,

Thanks for your reply.

I have tried with above code and it just added “>= 100 “true text” “false text”” text at end of the document. One thing, I would like to know as I need conditional display of some paragraph so do I need to put the condition in my template . Condition is “{ IF {MERGEFIELD amount >= 100 “true text” “false text” }”, where text is the paragraph text. But I see you are doing this in code but I need to put this type of condition in my template and code should search this condition and based on condition display text.

I did not understand what is above code is doing, are we writing the condition in main document using above code bacause using above code it added below text in my main generated document.
“>= 100 “true text” “false text””

Please reply. We have also license of Aspose and using extensively.

Thanks,
Vaibhav Diwan

Hi Hafeez,

In my template document using
{ IF {MERGEFIELD GENDER = “MALE” “true text” “false text” }
And in Java code below code
wordTemplateDoc.getMailMerge().execute(new String[]{“GENDER”}, new String[]{“MALE”});

But in Generated document same IF condition gets displayed instead of “true text”.

Am I doing wrong something.

Thanks.
Vaibhav DIwan

@diwanmail,

Yes, the code in my previous post programmatically inserts a nested merge field inside IF field and then executes the mail merge operation. When you toggle field codes in generated Word document, it should display content of the false part.

You can insert the same IF field manually by using MS Word.

You can also call document.UpdateFields() method before Save method. Hope this helps.

HI Hafeez,

Thanks for your reply. Can you please send me working example with template doc(where are you putting IF condition), generated doc and complete Java code.

As Here I tried with adding IF condition using MS word in my template and it generated below IF condition
{ IF GENDER = “MALE” “true text” “false text” * MERGEFORMAT }

And below Java code
wordTemplateDoc.getMailMerge().execute(new String[]{“GENDER”}, new String[]{“MALE”});

	wordTemplateDoc.updateFields();

but nothing happen.

@diwanmail,

Please see these documents (nested-field-test.zip (17.8 KB)) and try running the following code:

Document doc = new Document("D:\\Temp\\nested-field-test.docx");
doc.getMailMerge().execute(new String[]{"GENDER"}, new Object[]{"MALE"});
doc.save("D:\\Temp\\awjava-18.4.docx");

It worked.

Thanks for your favor to solve this.

Vaibhav

Hi Hafeez,

Is there any way by which we can achieve the mail merge functionality by not using MS-WORD mail merge. Can We write our own conditional query language like “<insert-if condition” , where Aspose can read this conditional text and parse and show content based on condition.

Please reply.

Thanks,
Vaibhav

@diwanmail,

You can try performing Mail Merge using ‘Mustache’ Template Syntax. Please see this document, screenshot (Mustache-Template-Syntax.zip (29.9 KB)) and the following article:

Mail Merge using ‘Mustache’ Template Syntax

Code:

Document doc = new Document("D:\\Temp\\input.docx");
doc.getMailMerge().setUseNonMergeFields(true);
doc.getMailMerge().execute(new String[]{"GENDER"}, new Object[]{"MALE"});
doc.save("D:\\Temp\\awjava-18.4.docx");

2 posts were merged into an existing topic: Issue with Mustache “IF” statement