フォームフィールドの操作
穴埋め(フィールド)を含む文書はフォームと呼ばれます。たとえば、ユーザーがエントリを選択できるドロップダウン リストを使用する登録フォームを Microsoft Word で作成できます。 Form
フィールドは、名前やアドレスなどの特定の種類のデータが保存される場所です。 Microsoft Word のフォーム フィールドには、テキスト入力、コンボボックス、チェックボックスが含まれます。
プロジェクト内のフォーム フィールドを使用して、ユーザーと「通信」できます。たとえば、コンテンツは保護されているが、編集できるのはフォーム フィールドのみであるドキュメントを作成するとします。ユーザーはフォームフィールドにデータを入力し、ドキュメントを送信できます。 Aspose.Words を使用するアプリケーションは、フォーム フィールドからデータを取得して処理できます。
コードを使用してフォーム フィールドをドキュメントに配置するのは簡単です。 DocumentBuilder には、フォーム フィールドの種類ごとに 1 つずつ、それらを挿入するための特別なメソッドがあります。各メソッドは、フォーム フィールドの名前を表す文字列パラメータを受け入れます。名前は空の文字列にすることができます。ただし、フォーム フィールドの名前を指定すると、ブックマークが同じ名前で自動的に作成されます。
フォームフィールドの挿入
フォーム フィールドは、ユーザーとの「対話」を可能にする Word フィールドの特殊なケースです。 Microsoft Word のフォーム フィールドには、テキスト ボックス、コンボ ボックス、チェックボックスが含まれます。
DocumentBuilder は、InsertTextInput、InsertCheckBox、InsertComboBox などの各タイプのフォーム フィールドをドキュメントに挿入するための特別なメソッドを提供します。フォームフィールドの名前を指定すると、ブックマークが同じ名前で自動的に作成されることに注意してください。
次のコード例は、コンボボックス フォーム フィールドをドキュメントに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
string[] items = { "One", "Two", "Three" }; | |
builder.InsertComboBox("DropDown", items, 0); |
テキスト入力を挿入する
InsertTextInput メソッドを使用して、ドキュメントにテキストボックスを挿入します。
次のコード例は、テキスト入力フォーム フィールドをドキュメントに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0); | |
dataDir = dataDir + "DocumentBuilderInsertTextInputFormField_out.doc"; | |
doc.Save(dataDir); |
チェックボックスを挿入する
InsertCheckBox を呼び出して、ドキュメントにチェックボックスを挿入します。
次のコード例は、チェックボックス フォーム フィールドをドキュメントに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertCheckBox("CheckBox", true, true, 0); | |
dataDir = dataDir + "DocumentBuilderInsertCheckBoxFormField_out.doc"; | |
doc.Save(dataDir); |
コンボボックスを挿入する
InsertComboBox を呼び出して、ドキュメントにコンボボックスを挿入します。
次のコード例は、コンボボックス フォーム フィールドをドキュメントに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
string[] items = { "One", "Two", "Three" }; | |
builder.InsertComboBox("DropDown", items, 0); | |
dataDir = dataDir + "DocumentBuilderInsertComboBoxFormField_out.doc"; | |
doc.Save(dataDir); |
フォームフィールドの取得
フォーム フィールドのコレクションは、FormFields プロパティを使用して取得できる FormFieldCollection クラスによって表されます。これは、ドキュメント自体を含む任意のドキュメント ノードに含まれるフォーム フィールドを取得できることを意味します。
次のコード例は、フォーム フィールドのコレクションを取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormFieldCollection formFields = doc.Range.FormFields; | |
インデックスまたは名前によって特定のフォーム フィールドを取得できます。
次のコード例は、フォーム フィールドにアクセスする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormFieldCollection documentFormFields = doc.Range.FormFields; | |
FormField formField1 = documentFormFields[3]; | |
FormField formField2 = documentFormFields["Text2"]; |
FormField プロパティを使用すると、フォーム フィールド名、タイプ、結果を操作できます。
次のコード例は、フォーム フィールドの名前、型、および結果を操作する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormField formField = doc.Range.FormFields[3]; | |
if (formField.Type.Equals(FieldType.FieldFormTextInput)) | |
formField.Result = "My name is " + formField.Name; |
フォームフィールドのフォーマット
FormField の Font プロパティを使用すると、フィールド値を含む FormField 全体にフォント書式設定を適用できます。
次のコード例は、フォントの書式設定を FormField に適用する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "Document.doc"); | |
doc.Range.FormFields[0].Font.Size = 20; | |
doc.Range.FormFields[0].Font.Color = Color.Red; | |
doc.Save(dataDir + "Document_out.doc"); |