用静态文本替换字段

当您希望将文档另存为静态副本时,通常需要替换字段。 例如,在电子邮件中作为附件发送时。 将DATETIME等字段转换为静态文本将允许文档显示与发送时相同的日期。 此外,在某些情况下,您可能需要从文档中删除条件IF字段,并将其替换为最新的文本结果。 例如,将IF字段的结果转换为静态文本,以便在更新文档中的字段时不再动态更改其值。

下图显示了IF字段如何存储在文档中:

  • 文本由特殊字段节点–FieldStartFieldEnd包围
  • 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-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
doc->UnlinkFields();

转换特定文档部分中的某些字段类型

由于ConvertFieldsToStaticText方法接受两个参数–CompositeNode属性和FieldType枚举,因此可以将任何复合节点传递给此方法。 这允许字段仅在文档的特定部分转换为静态文本。

例如,您可以传递一个Document对象并将指定类型的字段从整个文档转换为静态文本,或者您可以传递一个节的Body对象并仅转换在该正文中找到的字段。

传递给ConvertFieldsToStaticText方法的FieldType枚举指定应将哪种类型的字段转换为静态文本。 在文档中找到的任何其他字段类型将保持不变。

下面的代码示例演示如何在特定节点compositeNode中选择特定类型targetFieldType的字段,然后将其转换为静态文本:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
void ConvertFieldsToStaticText(System::SharedPtr<CompositeNode> compositeNode, FieldType targetFieldType)
{
for (const auto& field : System::IterateOver(compositeNode->get_Range()->get_Fields()))
{
if (field->get_Type() == targetFieldType)
{
field->Unlink();
}
}
}

下面的代码示例演示如何将文档中的所有IF字段转换为静态文本:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");

下面的代码示例演示如何将文档正文中的所有PAGE字段转换为静态文本:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");

下面的代码示例演示如何将最后一段中的所有IF字段转换为静态文本:

//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc->get_Range()
->get_Fields()
->LINQ_Where([](SharedPtr<Field> f) { return f->get_Type() == FieldType::FieldIf; })
->LINQ_ToList()
->ForEach(std::function<void(SharedPtr<Field> f)>([](SharedPtr<Field> f) { f->Unlink(); }));
doc->Save(ArtifactsDir + u"WorkingWithFields.ConvertFieldsInDocument.docx");