public class FieldIf extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Compares the values designated by the expressions getLeftExpression() / setLeftExpression(java.lang.String) and getRightExpression() / setRightExpression(java.lang.String) in comparison using the operator designated by getComparisonOperator() / setComparisonOperator(java.lang.String).
A field in the following format will be used as a mail merge source: { IF 0 = 0 "{PatientsNameFML}" "" \* MERGEFORMAT }
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
| Modifier and Type | Method and Description |
|---|---|
boolean |
canWorkAsMergeField() |
int |
evaluateCondition()
Evaluates the condition.
|
java.lang.String |
getComparisonOperator()
Gets the comparison operator.
|
java.lang.String |
getFalseText()
Gets the text displayed if the comparison expression is
false. |
java.lang.String |
getLeftExpression()
Gets the left part of the comparison expression.
|
java.lang.String |
getMergeFieldName() |
java.lang.String |
getRightExpression()
Gets the right part of the comparison expression.
|
java.lang.String |
getTrueText()
Gets the text displayed if the comparison expression is true.
|
boolean |
isMergeValueRequired() |
void |
setComparisonOperator(java.lang.String value)
Sets the comparison operator.
|
void |
setFalseText(java.lang.String value)
Sets the text displayed if the comparison expression is
false. |
void |
setLeftExpression(java.lang.String value)
Sets the left part of the comparison expression.
|
void |
setRightExpression(java.lang.String value)
Sets the right part of the comparison expression.
|
void |
setTrueText(java.lang.String value)
Sets the text displayed if the comparison expression is true.
|
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, updatepublic java.lang.String getMergeFieldName()
public boolean canWorkAsMergeField()
throws java.lang.Exception
java.lang.Exceptionpublic boolean isMergeValueRequired()
public java.lang.String getLeftExpression()
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
public void setLeftExpression(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
value - The left part of the comparison expression.java.lang.Exceptionpublic java.lang.String getComparisonOperator()
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
public void setComparisonOperator(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
value - The comparison operator.java.lang.Exceptionpublic java.lang.String getRightExpression()
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
public void setRightExpression(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
value - The right part of the comparison expression.java.lang.Exceptionpublic java.lang.String getTrueText()
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
public void setTrueText(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
value - The text displayed if the comparison expression is true.java.lang.Exceptionpublic java.lang.String getFalseText()
false.
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
false.public void setFalseText(java.lang.String value)
throws java.lang.Exception
false.
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
value - The text displayed if the comparison expression is false.java.lang.Exceptionpublic int evaluateCondition()
throws java.lang.Exception
Examples:
Shows how to insert an IF field.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Statement 1: ");
FieldIf field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("0");
field.setComparisonOperator("=");
field.setRightExpression("1");
// The IF field will display a string from either its "TrueText" property,
// or its "FalseText" property, depending on the truth of the statement that we have constructed.
field.setTrueText("True");
field.setFalseText("False");
field.update();
// In this case, "0 = 1" is incorrect, so the displayed result will be "False".
Assert.assertEquals(" IF 0 = 1 True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.FALSE, field.evaluateCondition());
Assert.assertEquals("False", field.getResult());
builder.write("\nStatement 2: ");
field = (FieldIf) builder.insertField(FieldType.FIELD_IF, true);
field.setLeftExpression("5");
field.setComparisonOperator("=");
field.setRightExpression("2 + 3");
field.setTrueText("True");
field.setFalseText("False");
field.update();
// This time the statement is correct, so the displayed result will be "True".
Assert.assertEquals(" IF 5 = \"2 + 3\" True False", field.getFieldCode());
Assert.assertEquals(FieldIfComparisonResult.TRUE, field.evaluateCondition());
Assert.assertEquals("True", field.getResult());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.IF.docx");
FieldIfComparisonResult value that represents the result of the condition evaluation. The returned value is one of FieldIfComparisonResult constants.java.lang.Exception