清理文档

有时您可能需要删除未使用或重复的信息,以减少输出文档的大小和处理时间。

虽然您可以手动查找和删除未使用的数据(例如样式或列表)或重复信息,但使用 Aspose.Words 提供的特性和功能来执行此操作会更加方便。

CleanupOptions 类允许您指定文档清理选项。要从文档中删除重复的样式或仅删除未使用的样式或列表,您可以使用 Cleanup 方法。

从文档中删除未使用的信息

您可以使用 UnusedStylesUnusedBuiltinStyles 属性来检测和删除标记为"未使用"的样式。

您可以使用 UnusedLists 属性来检测和删除标记为"未使用"的列表和列表定义。

以下代码示例演示如何从文档中仅删除未使用的样式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Unused styles.docx");
// Combined with the built-in styles, the document now has eight styles.
// A custom style is marked as "used" while there is any text within the document
// formatted in that style. This means that the 4 styles we added are currently unused.
Console.WriteLine($"Count of styles before Cleanup: {doc.Styles.Count}\n" +
$"Count of lists before Cleanup: {doc.Lists.Count}");
// Cleans unused styles and lists from the document depending on given CleanupOptions.
CleanupOptions cleanupOptions = new CleanupOptions { UnusedLists = false, UnusedStyles = true };
doc.Cleanup(cleanupOptions);
Console.WriteLine($"Count of styles after Cleanup was decreased: {doc.Styles.Count}\n" +
$"Count of lists after Cleanup is the same: {doc.Lists.Count}");
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.CleanupUnusedStylesAndLists.docx");

从文档中删除重复信息

您还可以使用 DuplicateStyle 属性将所有重复样式替换为原始样式,并从文档中删除重复项。

以下代码示例演示如何从文档中删除重复的样式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Document.docx");
// Count of styles before Cleanup.
Console.WriteLine(doc.Styles.Count);
// Cleans duplicate styles from the document.
CleanupOptions options = new CleanupOptions { DuplicateStyle = true };
doc.Cleanup(options);
// Count of styles after Cleanup was decreased.
Console.WriteLine(doc.Styles.Count);
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.CleanupDuplicateStyle.docx");