If , ElseIf in MergeField Aspose Word

Can anyone give me a proper syntax for
if, else if and else Mergefields used in a Word document.
For example:

I need a if and another if or else if condition to check whether the questiontype is text or picklist, based on that i need to print the values.
ifelse json.docx (12.2 KB)

@ArunaAP In MS Word there is no ElseIf fields. You can achieve your requirements using nested IF fields. For example you can use field code like this:

{ IF "{ MERGEFIELD condition }" = "true" "{ IF "{ MERGEFIELD condition2 }" = "true" "ElseIf True Value" "ElseIf False Value" }" "False Value" }

Your requirements can be easily implemented using LINQ Reporting Engine and the following syntax:

<<if [conditional_expression1]>>
template_option1
<<elseif [conditional_expression2]>>
template_option2
...
<<else>>
default_template_option
<</if>>

Please see our documentation for more information:
https://docs.aspose.com/words/net/using-conditional-blocks/

Can you please refer the doc which I attached, I need to fetch the value based on the question Type

@ArunaAP If I understand correctly you are using Aspose.Words for Cloud. Could you please confirm. If so I will move your question into the appropriate forum.

As for LINQ Reporting Engine you can use the following syntax:

<<foreach [q in listOfQuestions]>>
<<if [q.QuestionType == “picklist”]>>
This is picklist question
<<elseif [q.QuestionType == “text”]>>
This is text question
<<else>>
This is default option
<</if>>
<</foreach>>

Here is the code to fill the document with data:

JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");
Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "listOfQuestions");
doc.Save(@"C:\Temp\out.docx");

If it is required to use mail merge, then you can simply use several IF fields even without nesting them:

{ IF "{ MERGEFIELD QuestionType }" = "picklist" "This is picklist question" "" }
{ IF "{ MERGEFIELD QuestionType }" = "text" "This is picklist question" "" }

The template might be look like this (press Alf+F9 to see field codes):

in.docx (12.8 KB)
out.docx (10.1 KB)
mail_merge.docx (12.6 KB)

Yes, I am using Aspose.Words for cloud.

and this is my syntax, i need to print it in a single table
QuestionTable.docx (14.0 KB)

@ArunaAP

It seems you have already raised a similar query in your other post. Please follow it in that post.

1 Like