以靜態文字取代欄位

替換欄位通常是在您想要將文件儲存在靜態副本時必須的。 例如,當以附件在電子郵件中傳送。 將例如 DATETIME 的欄位轉換為靜態文字,會讓文件顯示與发送時間相同的日期。 在有些情況下,你可能需要從你的文件中移除條件性IF欄位並用最新的文字結果替換它。 例如,將 IF 欄位結果轉換為靜態文字,所以它會不再隨著文檔中欄位的更新而動態地改變其值。

下方圖示說明了 IF 欄位儲存在文件中的方式。

文本被特殊的字段节点 – FieldStartFieldEnd 包围 “ FieldSeparator節點將欄位中的文字分開為欄位代碼和欄位結果”

  • 欄位代碼定義了該欄位的一般行為,而欄位結果會保留使用 Microsoft Word 或 Aspose.Words 更新此欄位時最新的結果。 “*欄位結果是當在文件中查看時,儲存在欄位並顯示於該文件中的值”

update-remove-a-field-aspose-words

結構也可以在下面的階層式形式中使用 demo project “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 數值,因此可以將任何複合節點傳遞給此方法。 這使得在文件的某特定部分可以將欄位轉換為靜態文字。

例如,你可以傳送一個 Document 物件並將指定類型之字段從整個文件轉換為靜態文字,或是傳送一個 Body 物件(即某個節點)僅將該節點中找到的字段轉換。

FieldType這個枚舉傳入給ConvertFieldsToStaticText的方法,會指定哪種類型的欄位該轉換成靜態文字。 文檔中發現的任何其他字段類型都會保持不變。

接下來的程式碼範例展示如何選擇特定類型的欄位- 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");