フィールドプロパティのカスタマイズ

Aspose.Wordsは、さまざまなフィールドプロパティとプログラムで対話する機能を提供します。 この記事では、フィールドプロパティの操作の基本原則を理解できるように、いくつかの例を見ていきます。 Fields namespaceの対応するクラスの各フィールドタイプのプロパティの完全なリストを見ることができます。

フィールドプロパティの更新

ユーザーがフィールドプロパティの値を変更する必要がある場合があります。 たとえば、AUTHORフィールドのAuthorNameプロパティを更新したり、MERGEFIELDフィールドのFieldNameプロパティを変更したりします。

Word文書の差し込み項目の名前を変更する方法を次のコード例に示します:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->InsertField(u"MERGEFIELD MyMergeField1 \\* MERGEFORMAT");
builder->InsertField(u"MERGEFIELD MyMergeField2 \\* MERGEFORMAT");
// Select all field start nodes so we can find the merge fields.
SharedPtr<NodeCollection> fieldStarts = doc->GetChildNodes(NodeType::FieldStart, true);
for (const auto& fieldStart : System::IterateOver<FieldStart>(fieldStarts))
{
if (fieldStart->get_FieldType() == FieldType::FieldMergeField)
{
auto mergeField = MakeObject<WorkingWithFields::MergeField>(fieldStart);
mergeField->set_Name(mergeField->get_Name() + u"_Renamed");
}
}
doc->Save(ArtifactsDir + u"WorkingWithFields.RenameMergeFields.docx");

フィールド表示結果

Aspose.Wordsは、フィールドセパレータノードを持たないフィールドのフィールドの結果を取得するプロパティを提供します。 Microsoft Wordはフィールドの値をオンザフライで計算することによってドキュメントに表示しますが、ドキュメントモデルにはそのような値はありません。

次のコード例は、DisplayResultプロパティの使用法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Various fields.docx");
doc->UpdateFields();
for (const auto& field : System::IterateOver(document->get_Range()->get_Fields()))
{
std::cout << field->get_DisplayResult() << std::endl;
}