public class FieldNextIf 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). If the comparison is true, the next data record is merged into the current merge document. (Merge fields that follow the NEXTIF in the main document are replaced by values from the next data record rather than the current data record.) If the comparison is false, the next data record is merged into a new merge document.
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getComparisonOperator()
Gets the comparison operator.
|
java.lang.String |
getLeftExpression()
Gets the left part of the comparison expression.
|
java.lang.String |
getRightExpression()
Gets the right part of the comparison expression.
|
void |
setComparisonOperator(java.lang.String value)
Sets the comparison operator.
|
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.
|
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, updatepublic java.lang.String getLeftExpression()
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
public void setLeftExpression(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
value - The left part of the comparison expression.java.lang.Exceptionpublic java.lang.String getComparisonOperator()
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
public void setComparisonOperator(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
value - The comparison operator.java.lang.Exceptionpublic java.lang.String getRightExpression()
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
public void setRightExpression(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to use NEXT/NEXTIF fields to merge multiple rows into one page during a mail merge.
public void fieldNext() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a data source for our mail merge with 3 rows.
// A mail merge that uses this table would normally create a 3-page document.
DataTable table = new DataTable("Employees");
table.getColumns().add("Courtesy Title");
table.getColumns().add("First Name");
table.getColumns().add("Last Name");
table.getRows().add("Mr.", "John", "Doe");
table.getRows().add("Mrs.", "Jane", "Cardholder");
table.getRows().add("Mr.", "Joe", "Bloggs");
insertMergeFields(builder, "First row: ");
// If we have multiple merge fields with the same FieldName,
// they will receive data from the same row of the data source and display the same value after the merge.
// A NEXT field tells the mail merge instantly to move down one row,
// which means any MERGEFIELDs that follow the NEXT field will receive data from the next row.
// Make sure never to try to skip to the next row while already on the last row.
FieldNext fieldNext = (FieldNext) builder.insertField(FieldType.FIELD_NEXT, true);
Assert.assertEquals(" NEXT ", fieldNext.getFieldCode());
// After the merge, the data source values that these MERGEFIELDs accept
// will end up on the same page as the MERGEFIELDs above.
insertMergeFields(builder, "Second row: ");
// A NEXTIF field has the same function as a NEXT field,
// but it skips to the next row only if a statement constructed by the following 3 properties is true.
FieldNextIf fieldNextIf = (FieldNextIf) builder.insertField(FieldType.FIELD_NEXT_IF, true);
fieldNextIf.setLeftExpression("5");
fieldNextIf.setRightExpression("2 + 3");
fieldNextIf.setComparisonOperator("=");
Assert.assertEquals(" NEXTIF 5 = \"2 + 3\"", fieldNextIf.getFieldCode());
// If the comparison asserted by the above field is correct,
// the following 3 merge fields will take data from the third row.
// Otherwise, these fields will take data from row 2 again.
insertMergeFields(builder, "Third row: ");
doc.getMailMerge().execute(table);
// Our data source has 3 rows, and we skipped rows twice.
// Our output document will have 1 page with data from all 3 rows.
doc.save(getArtifactsDir() + "Field.NEXT.NEXTIF.docx");
}
/// <summary>
/// Uses a document builder to insert MERGEFIELDs for a data source that contains columns named "Courtesy Title", "First Name" and "Last Name".
/// </summary>
public void insertMergeFields(final DocumentBuilder builder, final String firstFieldTextBefore) throws Exception {
insertMergeField(builder, "Courtesy Title", firstFieldTextBefore, " ");
insertMergeField(builder, "First Name", null, " ");
insertMergeField(builder, "Last Name", null, null);
builder.insertParagraph();
}
/// <summary>
/// Uses a document builder to insert a MERRGEFIELD with specified properties.
/// </summary>
public void insertMergeField(final DocumentBuilder builder, final String fieldName, final String textBefore, final String textAfter) throws Exception {
FieldMergeField field = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
field.setFieldName(fieldName);
field.setTextBefore(textBefore);
field.setTextAfter(textAfter);
}
value - The right part of the comparison expression.java.lang.Exception