フィールドプロパティのカスタマイズ
Aspose.Wordsは、さまざまなフィールドプロパティとプログラムで対話する機能を提供します。 この記事では、フィールドプロパティの操作の基本原則を理解できるように、いくつかの例を見ていきます。 対応するクラスの各フィールドタイプのプロパティの完全なリストを表示できます。
フィールドプロパティの更新
ユーザーがフィールドプロパティの値を変更する必要がある場合があります。 たとえば、AUTHOR
フィールドのAuthorNameプロパティを更新したり、MERGEFIELD
フィールドのFieldNameプロパティを変更したりします。
Word文書の差し込み項目の名前を変更する方法を次のコード例に示します:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertField("MERGEFIELD MyMergeField1 \\* MERGEFORMAT"); | |
builder.insertField("MERGEFIELD MyMergeField2 \\* MERGEFORMAT"); | |
for (Field f : doc.getRange().getFields()) | |
{ | |
if (f.getType() == FieldType.FIELD_MERGE_FIELD) | |
{ | |
FieldMergeField mergeField = (FieldMergeField)f; | |
mergeField.setFieldName(mergeField.getFieldName() + "_Renamed"); | |
mergeField.update(); | |
} | |
} | |
doc.save(getArtifactsDir() + "WorkingWithFields.RenameMergeFields.docx"); |
フィールド表示結果
Aspose.Wordsは、フィールドセパレータノードを持たないフィールドのフィールドの結果を取得するプロパティを提供します。 MSWordは、フィールドの値をオンザフライで計算することによってドキュメントに表示しますが、ドキュメントモデルにはそのような値はありません。
次のコード例は、DisplayResultプロパティの使用法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document document = new Document(getMyDir() + "Various fields.docx"); | |
document.updateFields(); | |
//ExEnd:UpdateDocFields | |
for (Field field : document.getRange().getFields()) | |
System.out.println(field.getDisplayResult()); |