フィールドを静的テキストに置き換える

ドキュメントを静的コピーとして保存する場合は、フィールドの置換が必要になることがよくあります。 たとえば、電子メールで添付ファイルとして送信する場合などです。 DATETIMEなどのフィールドを静的テキストに変換すると、文書は送信されたときと同じ日付を表示できます。 また、状況によっては、文書から条件付きIFフィールドを削除し、代わりに最新のテキスト結果に置き換える必要がある場合があります。 たとえば、IFフィールドの結果を静的テキストに変換して、ドキュメント内のフィールドが更新されたときに動的に値が変更されないようにします。

次の図は、IFフィールドがドキュメントにどのように格納されているかを示しています:

  • テキストは特別なフィールドノードFieldStartFieldEndで囲まれています
  • FieldSeparatorノードは、フィールド内のテキストをフィールドコードとフィールド結果に分離します
  • フィールドコードはフィールドの一般的な動作を定義しますが、このフィールドがMicrosoft WordまたはAspose.Wordsを使用して更新された場合、フィールドの結果は最新の結果を保
  • フィールドの結果は、フィールドに格納され、表示されたときにドキュメントに表示されるものです

update-remove-a-field-aspose-words

この構造は、デモプロジェクト*“DocumentExplorer”*を使用して階層的な形式で以下に見ることもできます。

update-remove-a-field-aspose-words-2

テキストで置き換えることができないフィールド

フィールドを静的テキストに置き換えることは、ヘッダーまたはフッターの一部のフィールドでは正常に機能しません。

たとえば、ヘッダーまたはフッターのPAGEフィールドを静的テキストに変換しようとすると、すべてのページに同じ値が表示されます。 これは、ヘッダーとフッターが複数のページで繰り返され、フィールドとして残っている場合は、特に処理されて各ページに正しい結果が表示されるためです。

ただし、ヘッダーでは、PAGEフィールドは静的なテキストの実行にうまく変換されます。 このテキストの実行は、セクションの最後のページであるかのように評価され、ヘッダー内のPAGEフィールドにすべてのページの最後のページが表示されます。

次のコード例は、フィールドを最新の結果に置き換える方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
doc->UnlinkFields();

特定のドキュメントパーツ内の特定のフィールドタイプの変換

ConvertFieldsToStaticTextメソッドは、CompositeNodeプロパティとFieldType列挙型の2つのパラメータを受け入れるため、このメソッドに任意の複合ノードを渡すことができます。 これにより、フィールドをドキュメントの特定の部分でのみ静的テキストに変換できます。

たとえば、Documentオブジェクトを渡して指定された型のフィールドを文書全体から静的テキストに変換したり、セクションのBodyオブジェクトを渡して、その本文にあるフィールドのみを変換したりすることができます。

ConvertFieldsToStaticTextメソッドに渡されるFieldType列挙体は、どのタイプのフィールドを静的テキストに変換するかを指定します。 ドキュメント内で見つかった他のフィールドタイプは変更されません。

次のコード例は、特定のノードcompositeNodeの特定の型targetFieldTypeのフィールドを選択し、それらを静的テキストに変換する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void ConvertFieldsToStaticText(System::SharedPtr<CompositeNode> compositeNode, FieldType targetFieldType)
{
for (const auto& field : System::IterateOver(compositeNode->get_Range()->get_Fields()))
{
if (field->get_Type() == targetFieldType)
{
field->Unlink();
}
}
}

次のコード例は、ドキュメント内のすべてのIFフィールドを静的テキストに変換する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");

次のコード例は、ドキュメントの本文内のすべてのPAGEフィールドを静的テキストに変換する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");

次のコード例は、最後の段落のすべてのIFフィールドを静的テキストに変換する方法を示しています:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");