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

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

以下の図は、IF フィールドがドキュメントにどのように保存されるかを示しています。

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

update-remove-a-field-aspose-words

この構造は、デモプロジェクト*「DocumentExplorer」*. を使用して階層形式で以下に表示することもできます。

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

テキストで置換できないフィールド

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

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

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

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

特定のドキュメント部分の特定のフィールド タイプを変換する

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-.NET
public class FieldsHelper
{
/// <summary>
/// Converts any fields of the specified type found in the descendants of the node into static text.
/// </summary>
/// <param name="compositeNode">The node in which all descendants of the specified FieldType will be converted to static text.</param>
/// <param name="targetFieldType">The FieldType of the field to convert to static text.</param>
public static void ConvertFieldsToStaticText(CompositeNode compositeNode, FieldType targetFieldType)
{
compositeNode.Range.Fields.Cast<Field>().Where(f => f.Type == targetFieldType).ToList().ForEach(f => f.Unlink());
}
}

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Linked fields.docx")
# Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
for f in doc.range.fields :
if f.type == aw.fields.FieldType.FIELD_IF :
f.unlink()
# Save the document with fields transformed to disk
doc.save(docs_base.artifacts_dir + "WorkingWithFields.convert_fields_in_document.docx")

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Linked fields.docx")
# Pass the appropriate parameters to convert PAGE fields encountered to text only in the body of the first section.
for f in doc.first_section.body.range.fields :
if f.type == aw.fields.FieldType.FIELD_PAGE :
f.unlink()
doc.save(docs_base.artifacts_dir + "WorkingWithFields.convert_fields_in_body.docx")

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Linked fields.docx")
# Pass the appropriate parameters to convert all IF fields to text that are encountered only in the last
# paragraph of the document.
for f in doc.first_section.body.last_paragraph.range.fields :
if f.type == aw.fields.FieldType.FIELD_IF :
f.unlink()
doc.save(docs_base.artifacts_dir + "WorkingWithFields.test_file.docx")