使用表单字段
包含填空(字段)的文档称为表单。 例如,您可以在Microsoft Word中创建一个注册表单,该注册表单使用用户可以从中选择条目的下拉列表。 Form
字段是存储特定类型数据(如名称或地址)的位置。 Microsoft Word中的表单字段包括文本输入,combobox和checkbox。
您可以在项目中使用表单字段与用户"沟通"。 例如,您创建一个文档,其内容受保护,但只有表单字段可编辑。 用户可以在表单字段中输入数据并提交文档。 使用Aspose.Words的应用程序可以从表单字段中检索数据并进行处理。
通过代码将表单字段放入文档很容易。 DocumentBuilder有特殊的方法来插入它们,每个表单字段类型一个。 每个方法都接受一个表示表单字段名称的字符串参数。 名称可以是空字符串。 但是,如果您为表单字段指定了名称,则会自动创建具有相同名称的书签。
插入表单字段
表单字段是Word字段的一个特殊情况,它允许与用户进行"交互"。 Microsoft Word中的表单字段包括文本框,组合框和checkbox。
DocumentBuilder
下面的代码示例演示如何将combobox表单字段插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(InsertFormFields.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
String[] items = {"One", "Two", "Three"}; | |
builder.insertComboBox("DropDown", items, 0); | |
doc.save(dataDir + "output.docx"); |
插入文本输入
使用insertTextInput方法将文本框插入到文档中。
下面的代码示例演示如何将文本输入表单字段插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertTextInputFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Hello", 0); | |
doc.save(dataDir + "output.doc"); |
插入复选框
调用insertCheckBox将checkbox插入到文档中。
下面的代码示例演示如何将checkbox表单字段插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertCheckBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertCheckBox("CheckBox", true, true, 0); | |
doc.save(dataDir + "output.doc"); |
插入组合框
调用insertComboBox将combobox插入到文档中。
下面的代码示例演示如何将Combobox表单字段插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertComboBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
String[] items = {"One", "Two", "Three"}; | |
builder.insertComboBox("DropDown", items, 0); | |
doc.save(dataDir + "output.doc"); |
获取表单字段
表单字段的集合由可以使用Range.getFormFields属性检索的FormFieldCollection类表示。 这意味着您可以获取包含在包括文档本身的任何文档节点中的表单字段。
下面的代码示例演示如何获取表单字段的集合:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FormFieldsGetFormFieldsCollection.class); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormFieldCollection formFields = doc.getRange().getFormFields(); | |
doc.save(dataDir + "output.docx"); |
您可以通过其索引或名称获取特定表单字段。
下面的代码示例演示如何访问表单字段:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FormFieldsGetByName.class); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// FormFieldCollection formFields = doc.getRange().getFormFields(); | |
FormFieldCollection documentFormFields = doc.getRange().getFormFields(); | |
FormField formField1 = documentFormFields.get(3); | |
FormField formField2 = documentFormFields.get("Text2"); | |
System.out.println("Name: " + formField2.getName()); | |
doc.save(dataDir + "output.docx"); |
FormField属性允许您使用表单字段名称、类型和结果。
下面的代码示例演示如何使用表单字段名称、类型和结果:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FormFieldsWorkWithProperties.class); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
FormFieldCollection documentFormFields = doc.getRange().getFormFields(); | |
FormField formField = doc.getRange().getFormFields().get(3); | |
if (formField.getType() == FieldType.FIELD_FORM_TEXT_INPUT) | |
formField.setResult("Field Name :" + formField.getName()); | |
doc.save(dataDir + "output.docx"); |