com.aspose.words

Interface IFieldUserPromptRespondent

  • public interface IFieldUserPromptRespondent 

Represents the respondent to user prompts during field update.
The ASK and FILLIN fields are the examples of fields that prompt the user for some response. Implement this interface and assign it to the FieldOptions.UserPromptRespondent property to establish interaction between field update and the user.

Example:

Shows how to create an ASK field and set its properties.
@Test
public void fieldAsk() throws Exception {
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Place a field where the response to our ASK field will be placed
    FieldRef fieldRef = (FieldRef) builder.insertField(FieldType.FIELD_REF, true);
    fieldRef.setBookmarkName("MyAskField");
    builder.writeln();

    // Insert the ASK field and edit its properties, making sure to reference our REF field
    FieldAsk fieldAsk = (FieldAsk) builder.insertField(FieldType.FIELD_ASK, true);
    fieldAsk.setBookmarkName("MyAskField");
    fieldAsk.setPromptText("Please provide a response for this ASK field");
    fieldAsk.setDefaultResponse("Response from within the field.");
    fieldAsk.setPromptOnceOnMailMerge(true);
    builder.writeln();

    // ASK fields apply the default response to their respective REF fields during a mail merge
    DataTable table = new DataTable("My Table");
    table.getColumns().add("Column 1");
    table.getRows().add("Row 1");
    table.getRows().add("Row 2");

    FieldMergeField fieldMergeField = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
    fieldMergeField.setFieldName("Column 1");

    // We can modify or override the default response in our ASK fields with a custom prompt responder, which will take place during a mail merge
    doc.getFieldOptions().setUserPromptRespondent(new MyPromptRespondent());
    doc.getMailMerge().execute(table);

    doc.updateFields();
    doc.save(getArtifactsDir() + "Fields.AskField.docx");

    Assert.assertEquals(fieldAsk.getFieldCode(),
            " ASK  MyAskField \"Please provide a response for this ASK field\" \\d \"Response from within the field.\" \\o");

}

/// <summary>
/// IFieldUserPromptRespondent implementation that appends a line to the default response of an ASK field during a mail merge
/// </summary>
private static class MyPromptRespondent implements IFieldUserPromptRespondent {
    public String respond(final String promptText, final String defaultResponse) {
        return "Response from MyPromptRespondent. " + defaultResponse;
    }
}

Method Summary
abstract java.lang.Stringrespond(java.lang.String promptText, java.lang.String defaultResponse)
When implemented, returns a response from the user on prompting. Your implementation should return null to indicate that the user has not responded to the prompt (i.e. the user has pressed the Cancel button in the prompt window).
 

    • Method Detail

      • respond

        public abstract java.lang.String respond(java.lang.String promptText, java.lang.String defaultResponse)
        When implemented, returns a response from the user on prompting. Your implementation should return null to indicate that the user has not responded to the prompt (i.e. the user has pressed the Cancel button in the prompt window).
        Parameters:
        promptText - Prompt text (i.e. title of the prompt window).
        defaultResponse - Default user response (i.e. initial value contained in the prompt window).
        Returns:
        User response (i.e. confirmed value contained in the prompt window).

        Example:

        Shows how to create an ASK field and set its properties.
        @Test
        public void fieldAsk() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            // Place a field where the response to our ASK field will be placed
            FieldRef fieldRef = (FieldRef) builder.insertField(FieldType.FIELD_REF, true);
            fieldRef.setBookmarkName("MyAskField");
            builder.writeln();
        
            // Insert the ASK field and edit its properties, making sure to reference our REF field
            FieldAsk fieldAsk = (FieldAsk) builder.insertField(FieldType.FIELD_ASK, true);
            fieldAsk.setBookmarkName("MyAskField");
            fieldAsk.setPromptText("Please provide a response for this ASK field");
            fieldAsk.setDefaultResponse("Response from within the field.");
            fieldAsk.setPromptOnceOnMailMerge(true);
            builder.writeln();
        
            // ASK fields apply the default response to their respective REF fields during a mail merge
            DataTable table = new DataTable("My Table");
            table.getColumns().add("Column 1");
            table.getRows().add("Row 1");
            table.getRows().add("Row 2");
        
            FieldMergeField fieldMergeField = (FieldMergeField) builder.insertField(FieldType.FIELD_MERGE_FIELD, true);
            fieldMergeField.setFieldName("Column 1");
        
            // We can modify or override the default response in our ASK fields with a custom prompt responder, which will take place during a mail merge
            doc.getFieldOptions().setUserPromptRespondent(new MyPromptRespondent());
            doc.getMailMerge().execute(table);
        
            doc.updateFields();
            doc.save(getArtifactsDir() + "Fields.AskField.docx");
        
            Assert.assertEquals(fieldAsk.getFieldCode(),
                    " ASK  MyAskField \"Please provide a response for this ASK field\" \\d \"Response from within the field.\" \\o");
        
        }
        
        /// <summary>
        /// IFieldUserPromptRespondent implementation that appends a line to the default response of an ASK field during a mail merge
        /// </summary>
        private static class MyPromptRespondent implements IFieldUserPromptRespondent {
            public String respond(final String promptText, final String defaultResponse) {
                return "Response from MyPromptRespondent. " + defaultResponse;
            }
        }