フォームフィールドの操作

入力空白(フィールド)を含むドキュメントは、フォームと呼ばれます。 たとえば、ユーザーがエントリを選択できるドロップダウンリストを使用する登録フォームをMicrosoft Wordに作成できます。 フォームフィールドは、名前や住所などの特定の種類のデータが格納される場所です。 Microsoft Wordのフォームフィールドには、テキスト入力、Combobox、checkboxが含まれます。

プロジェクトのフォームフィールドを使用して、ユーザーと"通信"することができます。 たとえば、コンテンツが保護されているが、フォームフィールドのみが編集可能なドキュメントを作成するとします。 ユーザーはフォームフィールドにデータを入力してドキュメントを送信できます。 Aspose.Wordsを使用するアプリケーションは、フォームフィールドからデータを取得して処理できます。

コードを介してフォームフィールドを文書に配置するのは簡単です。 DocumentBuilderには、フォームフィールドの種類ごとに1つずつ挿入するための特別なメソッドがあります。 各メソッドは、フォームフィールドの名前を表す文字列パラメーターを受け入れます。 名前は空の文字列にすることができます。 ただし、フォームフィールドに名前を指定すると、同じ名前のブックマークが自動的に作成されます。

フォームフィールドの挿入

フォームフィールドは、ユーザーとの"対話"を可能にする単語フィールドの特定のケースです。 Microsoft Wordのフォームフィールドには、textbox、combo box、checkboxが含まれます。

DocumentBuilder

Comboboxフォームフィールドをドキュメントに挿入する方法を次のコード例に示します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::ArrayPtr<System::String> items = System::MakeArray<System::String>({u"One", u"Two", u"Three"});
builder->InsertComboBox(u"DropDown", items, 0);

フォームフィールドの取得

フォームフィールドのコレクションは、FormFieldsプロパティを使用して取得できるFormFieldCollectionクラスによって表されます。 これは、文書自体を含む任意の文書ノードに含まれるフォームフィールドを取得できることを意味します。

フォームフィールドのコレクションを取得する方法を次のコード例に示します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithFields();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"FormFields.doc");
System::SharedPtr<FormFieldCollection> formFields = doc->get_Range()->get_FormFields();

特定のフォームフィールドは、そのインデックスまたは名前で取得できます。

フォームフィールドにアクセスする方法を次のコード例に示します:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithFields();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"FormFields.doc");
System::SharedPtr<FormFieldCollection> documentFormFields = doc->get_Range()->get_FormFields();
System::SharedPtr<FormField> formField1 = documentFormFields->idx_get(3);
System::SharedPtr<FormField> formField2 = documentFormFields->idx_get(u"Text2");

FormFieldプロパティを使用すると、フォームフィールド名、タイプ、および結果を操作できます。

次のコード例は、フォームフィールド名、型、および結果を操作する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithFields();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"FormFields.doc");
System::SharedPtr<FormField> formField = doc->get_Range()->get_FormFields()->idx_get(3);
if (formField->get_Type() == FieldType::FieldFormTextInput)
{
formField->set_Result(System::String(u"My name is ") + formField->get_Name());
}