フィールドを静的テキストに置き換える
ドキュメントを静的コピーとして保存する場合は、フィールドの置換が必要になることがよくあります。 たとえば、電子メールで添付ファイルとして送信する場合などです。 DATE
やTIME
などのフィールドを静的テキストに変換すると、文書は送信されたときと同じ日付を表示できます。 また、状況によっては、文書から条件付きIF
フィールドを削除し、代わりに最新のテキスト結果に置き換える必要がある場合があります。 たとえば、IF
フィールドの結果を静的テキストに変換して、ドキュメント内のフィールドが更新されたときに動的に値が変更されないようにします。
次の図は、IF
フィールドがドキュメントにどのように格納されているかを示しています:
- テキストは特別なフィールドノードFieldStartとFieldEndで囲まれています
- FieldSeparatorノードは、フィールド内のテキストをフィールドコードとフィールド結果に分離します
- フィールドコードはフィールドの一般的な動作を定義しますが、このフィールドがMicrosoft WordまたはAspose.Wordsを使用して更新された場合、フィールドの結果は最新の結果を保
- フィールドの結果は、フィールドに格納され、表示されたときにドキュメントに表示されるものです
この構造は、Aspose.Wordsインストーラに同梱されているデモプロジェクト**“DocumentExplorer”**を使用して階層的な形式で以下に見ることもできます。
テキストで置き換えることができないフィールド
フィールドを静的テキストに置き換えることは、ヘッダーまたはフッターの一部のフィールドでは正常に機能しません。
たとえば、ヘッダーまたはフッターのPAGE
フィールドを静的テキストに変換しようとすると、すべてのページに同じ値が表示されます。 これは、ヘッダーとフッターが複数のページで繰り返され、フィールドとして残っている場合は、特に処理されて各ページに正しい結果が表示されるためです。
ただし、ヘッダーでは、PAGE
フィールドは静的なテキストの実行にうまく変換されます。 このテキストの実行は、セクションの最後のページであるかのように評価され、ヘッダー内のPAGE
フィールドにすべてのページの最後のページが表示されます。
次のコード例は、フィールドを最新の結果に置き換える方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(getMyDir() + "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-Java | |
private static class FieldsHelper extends DocumentVisitor { | |
/** | |
* Converts any fields of the specified type found in the descendants of the node into static text. | |
* | |
* @param compositeNode The node in which all descendants of the specified FieldType will be converted to static text. | |
* @param targetFieldType The FieldType of the field to convert to static text. | |
*/ | |
public static void convertFieldsToStaticText(CompositeNode compositeNode, int targetFieldType) throws Exception { | |
for (Field field : compositeNode.getRange().getFields()) | |
{ | |
if (field.getType() == targetFieldType) | |
{ | |
field.unlink(); | |
} | |
} | |
} | |
} |
次のコード例は、ドキュメント内のすべてのIF
フィールドを静的テキストに変換する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(getMyDir() + "Linked fields.docx"); | |
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text. | |
for (Field field : doc.getRange().getFields()) { | |
if (field.getType() == FieldType.FIELD_IF) { | |
field.unlink(); | |
} | |
} | |
doc.save(getArtifactsDir() + "WorkingWithFields.ConvertFieldsInDocument.docx"); |
次のコード例は、ドキュメントの本文内のすべてのPAGE
フィールドを静的テキストに変換する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(getMyDir() + "Linked fields.docx"); | |
// Pass the appropriate parameters to convert PAGE fields encountered to text only in the body of the first section. | |
for (Field field : doc.getRange().getFields()) { | |
if (field.getType() == FieldType.FIELD_PAGE) { | |
field.unlink(); | |
} | |
} | |
doc.save(getArtifactsDir() + "WorkingWithFields.ConvertFieldsInBody.docx"); |
次のコード例は、最後の段落のすべてのIF
フィールドを静的テキストに変換する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(getMyDir() + "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 (Field field : doc.getFirstSection().getBody().getLastParagraph().getRange().getFields()) { | |
if (field.getType() == FieldType.FIELD_IF) { | |
field.unlink(); | |
} | |
} | |
doc.save(getArtifactsDir() + "WorkingWithFields.TestFile.docx"); |