フォームフィールドの操作
入力空白(フィールド)を含むドキュメントは、フォームと呼ばれます。 たとえば、ユーザーがエントリを選択できるドロップダウンリストを使用する登録フォームをMicrosoft Wordに作成できます。 Form
フィールドは、名前や住所などの特定の種類のデータが格納される場所です。 Microsoft Wordのフォームフィールドには、テキスト入力、combobox、checkboxが含まれます。
プロジェクトのフォームフィールドを使用して、ユーザーと"通信"することができます。 たとえば、コンテンツが保護されているが、フォームフィールドのみが編集可能なドキュメントを作成するとします。 ユーザーはフォームフィールドにデータを入力してドキュメントを送信できます。 Aspose.Wordsを使用するアプリケーションは、フォームフィールドからデータを取得して処理できます。
コードを介してフォームフィールドを文書に配置するのは簡単です。 DocumentBuilderには、フォームフィールドの種類ごとに1つずつ挿入するための特別なメソッドがあります。 各メソッドは、フォームフィールドの名前を表す文字列パラメーターを受け入れます。 名前は空の文字列にすることができます。 ただし、フォームフィールドに名前を指定すると、同じ名前のブックマークが自動的に作成されます。
フォームフィールドの挿入
フォームフィールドは、ユーザーとの"対話"を可能にする単語フィールドの特定のケースです。 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"); |