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

ドキュメントを静的コピーとして保存する場合、フィールドの置換が必要になることがよくあります。たとえば、電子メールに添付して送信する場合です。 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 フィールドにはすべてのページの最後のページが表示されます。

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Various fields.docx");
doc.UnlinkFields();

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

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

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

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

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

// 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-.NET
Document doc = new Document(MyDir + "Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc.Range.Fields.Where(f => f.Type == FieldType.FieldIf).ToList().ForEach(f => f.Unlink());
// Save the document with fields transformed to disk
doc.Save(ArtifactsDir + "WorkingWithFields.ConvertFieldsInDocument.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Linked fields.docx");
// Pass the appropriate parameters to convert PAGE fields encountered to text only in the body of the first section.
doc.FirstSection.Body.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList().ForEach(f => f.Unlink());
doc.Save(ArtifactsDir + "WorkingWithFields.ConvertFieldsInBody.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "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.
doc.FirstSection.Body.LastParagraph.Range.Fields.Where(f => f.Type == FieldType.FieldIf).ToList()
.ForEach(f => f.Unlink());
doc.Save(ArtifactsDir + "WorkingWithFields.TestFile.docx");