java.lang.Object
com.aspose.words.FormFieldCollection
public class FormFieldCollection
Example:
Document doc = new Document(getMyDir() + "FormFields.doc");
FormFieldCollection formFields = doc.getRange().getFormFields();
| Property Getters/Setters Summary | ||
|---|---|---|
int | getCount() | |
| Returns the number of form fields in the collection. | ||
FormField | get(int index) | |
| Returns a form field at the specified index. | ||
FormField | get(java.lang.String bookmarkName) | |
| Returns a form field by bookmark name. | ||
| Method Summary | ||
|---|---|---|
void | clear() | |
| Removes all form fields from this collection and from the document. | ||
java.util.Iterator<FormField> | iterator() | |
| Returns an enumerator object. | ||
void | remove(java.lang.String formField) | |
| Removes a form field with the specified name. | ||
void | removeAt(int index) | |
| Removes a form field at the specified index. | ||
| Property Getters/Setters Detail |
|---|
getCount | |
public int getCount() | |
Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}get | |
public FormField get(int index) | |
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
index - An index into the collection.Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}get | |
public FormField get(java.lang.String bookmarkName) | |
bookmarkName - Case-insensitive bookmark name.Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}| Method Detail |
|---|
clear | |
public void clear()
throws java.lang.Exception | |
Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}iterator | |
public java.util.Iterator<FormField> iterator() | |
Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}remove | |
public void remove(java.lang.String formField)
throws java.lang.Exception | |
formField - The case-insensitive name of the form field to remove.Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}removeAt | |
public void removeAt(int index)
throws java.lang.Exception | |
index - The zero-based index of the form field to remove.Example:
Shows how insert different kinds of form fields into a document and process them with a visitor implementation.
public void formField() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box
FormField comboBox = builder.insertComboBox("MyComboBox", new String[]{"One", "Two", "Three"}, 0);
comboBox.setCalculateOnExit(true);
Assert.assertEquals(comboBox.getDropDownItems().getCount(), 3);
Assert.assertEquals(comboBox.getDropDownSelectedIndex(), 0);
Assert.assertEquals(comboBox.getEnabled(), true);
builder.writeln();
// Use a document builder to insert a check box
FormField checkBox = builder.insertCheckBox("MyCheckBox", false, 50);
checkBox.isCheckBoxExactSize(true);
checkBox.setHelpText("Right click to check this box");
checkBox.setOwnHelp(true);
checkBox.setStatusText("Checkbox status text");
checkBox.setOwnStatus(true);
Assert.assertEquals(checkBox.getCheckBoxSize(), 50.0d);
Assert.assertEquals(checkBox.getChecked(), false);
Assert.assertEquals(checkBox.getDefault(), false);
builder.writeln();
// Use a document builder to insert text input form field
FormField textInput = builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Your text goes here", 50);
Assert.assertEquals(doc.getRange().getFields().getCount(), 3);
textInput.setEntryMacro("EntryMacro");
textInput.setExitMacro("ExitMacro");
textInput.setTextInputDefault("Regular");
textInput.setTextInputFormat("FIRST CAPITAL");
textInput.setTextInputValue("This value overrides the one we set during initialization");
Assert.assertEquals(textInput.getTextInputType(), TextFormFieldType.REGULAR);
Assert.assertEquals(textInput.getMaxLength(), 50);
// Get the collection of form fields that has accumulated in our document
FormFieldCollection formFields = doc.getRange().getFormFields();
Assert.assertEquals(formFields.getCount(), 3);
// Iterate over the collection with an enumerator, accepting a visitor with each form field
FormFieldVisitor formFieldVisitor = new FormFieldVisitor();
Iterator<FormField> fieldEnumerator = formFields.iterator();
while (fieldEnumerator.hasNext()) {
fieldEnumerator.next().accept(formFieldVisitor);
}
System.out.println(formFieldVisitor.getText());
doc.updateFields();
doc.save(getArtifactsDir() + "Field.FormField.docx");
}
/// <summary>
/// Visitor implementation that prints information about visited form fields.
/// </summary>
public static class FormFieldVisitor extends DocumentVisitor {
public FormFieldVisitor() {
mBuilder = new StringBuilder();
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
public int visitFormField(final FormField formField) {
appendLine(formField.getType() + ": \"" + formField.getName() + "\"");
appendLine("\tStatus: " + (formField.getEnabled() ? "Enabled" : "Disabled"));
appendLine("\tHelp Text: " + formField.getHelpText());
appendLine("\tEntry macro name: " + formField.getEntryMacro());
appendLine("\tExit macro name: " + formField.getExitMacro());
switch (formField.getType()) {
case FieldType.FIELD_FORM_DROP_DOWN:
appendLine("\tDrop down items count: " + formField.getDropDownItems().getCount() + ", default selected item index: " + formField.getDropDownSelectedIndex());
appendLine("\tDrop down items: " + String.join(", ", formField.getDropDownItems()));
break;
case FieldType.FIELD_FORM_CHECK_BOX:
appendLine("\tCheckbox size: " + formField.getCheckBoxSize());
appendLine("\t" + "Checkbox is currently: " + (formField.getChecked() ? "checked, " : "unchecked, ") + "by default: " + (formField.getDefault() ? "checked" : "unchecked"));
break;
case FieldType.FIELD_FORM_TEXT_INPUT:
appendLine("\tInput format: " + formField.getTextInputFormat());
appendLine("\tCurrent contents: " + formField.getResult());
break;
}
// Let the visitor continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Adds newline char-terminated text to the current output.
/// </summary>
private void appendLine(final String text) {
mBuilder.append(text + '\n');
}
/// <summary>
/// Gets the plain text of the document that was accumulated by the visitor.
/// </summary>
public String getText() {
return mBuilder.toString();
}
private StringBuilder mBuilder;
}