Word格式处理控件Aspose.Words for .NET教程——更新和删除字段

Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

>>Aspose.Words for .NET已经更新至最新版,添加了新节点以处理多节结构化文档标签,改进了SmartArt冷渲染的性能,RevisionOptions类扩展了新的属性,点击下载体验

了解更多Aspose文档管理产品  Aspose技术交流群(761297826)


如何更新字段

加载文档后,Aspose.Words模仿Microsoft Word的行为,并且自动更新字段选项已关闭。该行为可以总结如下:

  • 当您打开/保存文档时,这些字段保持不变。
  • 根据需要显式更新文档中的所有字段(例如,重建TOC)。
  • 当打印/渲染为PDF或XPS时,页眉/页脚中与页码相关的字段将更新。
  • 执行邮件合并时,所有字段都会自动更新。

以编程方式更新字段

要显式更新整个文档中的字段,只需调用Document.UpdateFields。要更新文档一部分中包含的字段,请获取Range对象并调用Range.UpdateFields方法。在Aspose.Words中,可以使用Node.Range属性为文档树中的任何节点(例如Section,HeaderFooter,Paragraph等)获取Range。可以通过调用Field.Update来更新单个字段的结果。

渲染期间页面相关字段的自动更新

当执行将文档转换为固定页面格式(例如PDF或XPS)时,Aspose.Words会自动更新与页面布局相关的字段PAGE,PAGEREF,这些字段位于文档的页眉/页脚中。此行为模仿了打印文档时Microsoft Word的行为。如果要更新文档中的所有其他字段,则需要在呈现文档之前调用Document.UpdateFields。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();

Document doc = new Document(dataDir + "Rendering.doc");

// This updates all fields in the document.
doc.UpdateFields();
dataDir = dataDir + "Rendering.UpdateFields_out.pdf";
doc.Save(dataDir);

邮件合并期间的自动字段更新

当执行邮件合并时,文档中的所有字段都会自动更新。这是因为邮件合并是字段更新的情况。该程序遇到一个邮件合并字段,需要更新其结果,这涉及从数据源获取值并将其插入该字段中。逻辑当然更复杂,例如,当到达文档/邮件合并区域的末尾但仍然有其他数据要合并时,则需要复制该区域并更新新的字段集。

更新具有dirty属性的字段

w:dirty是一个字段级属性,将仅在您打开文档时刷新您指定的字段。它告诉MS Word仅在下次打开文档时刷新此字段。使用LoadOptions.UpdateDirtyFields属性来指定是否使用dirty属性更新字段。当LoadOptions.UpdateDirtyFields的值设置为true时,将在文档加载时更新Field.IsDirty或FieldChar.IsDirty属性具有真值的所有字段。以下示例显示如何更新具有脏属性的字段。

LoadOptions lo = new LoadOptions();

//Update the fields with the dirty attribute
lo.UpdateDirtyFields = true;

//Load the Word document
Document doc = new Document(dataDir + @"input.docx", lo);
 
//Save the document into DOCX
doc.Save(dataDir + "output.docx", SaveFormat.Docx);

保存前更新LastSavedTime属性

使用SaveOptions.UpdateLastSavedTimeProperty属性是否在保存文档时更新相应的内置文档属性(BuiltInDocumentProperties.LastSavedTime)。 下面的示例显示如何更新此属性。

Document doc = new Document(dataDir + "Document.doc");

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.UpdateLastSavedTimeProperty = true;

dataDir = dataDir + "UpdateLastSavedTimeProperty_out.docx";

// Save the document to disk.
doc.Save(dataDir, options);

调用UpdateFields会更新所有字段类型

在以前的版本中,调用Document.UpdateFields或Range.UpdateFields只会更新常规字段(例如IF或DOCPROPERTY),而不会更新与页面布局相关的字段(例如PAGE或NUMPAGES)。现在,较新的版本将更新常规和页面布局相关的字段。当Document.UpdateFields或Range.UpdateFields被称为所有字段在整个文件/范围更新。如果在更新过程中遇到与页面布局相关的字段(例如PAGE字段),则可能涉及构建文档布局。 下例显示了如何更新文档中的所有字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();

Document doc = new Document(dataDir + "Rendering.doc");

// This updates all fields in the document.
doc.UpdateFields();
dataDir = dataDir + "Rendering.UpdateFields_out.pdf";
doc.Save(dataDir);

删除字段

如前所述,所有字段现在都使用Document.UpdateFields更新。现在,这意味着更新文档中字段的更简洁明了的方法。这也意味着在诸如更新TOC字段之类的情况下,不再需要对Document.UpdatePageLayout的任何调用。所有工作都在Document.UpdateFields调用中处理。 下面的示例显示如何通过调用字段更新来完全重建文档中的TOC字段。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();

Document doc = new Document(dataDir + "Field.RemoveField.doc");

Field field = doc.Range.Fields[0];
// Calling this method completely removes the field from the document.
field.Remove();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值