自訂欄位屬性

Aspose.Words提供程式化與各種欄位屬性互動的能力。 在本文中,我們將看一些例子,讓您了解在與欄位屬性工作的基本原則。 你可以在 Fields namespace 中的對應類別中找到每個欄位型的完整屬性清單。

場域屬性更新

有時用戶需要更改欄位屬性的值。 例如,更新 AuthorName 屬性的 AUTHOR 欄位或更改 FieldName 屬性的 MERGEFIELD 欄位。

以下範例示範了如何在Word文件中重命名結合欄位:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"MERGEFIELD MyMergeField1 \* MERGEFORMAT");
builder.InsertField(@"MERGEFIELD MyMergeField2 \* MERGEFORMAT");
foreach (FieldMergeField mergeField in doc.Range.Fields.OfType<FieldMergeField>().ToList())
mergeField.FieldName = mergeField.FieldName + "_Renamed";
doc.Save(ArtifactsDir + "WorkingWithFields.RenameMergeFields.docx");

顯示結果

Aspose.Words 提供一個屬性來取得沒有使用欄位分界節點的欄位結果。 我們稱此為假結果或顯示結果,MS Word 在文件中透過計算欄位值來顯示它,但該文件模型中並沒有這樣的值。

接下來的程式碼範例示範了 DisplayResult 屬性的使用方式:

// 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.UpdateFields();
foreach (Field field in doc.Range.Fields)
Console.WriteLine(field.DisplayResult);