public class FieldNext extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Merges the next data record into the current resulting merged document, rather than starting a new merged 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);
}
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, update