清理文档
有时您可能需要删除未使用或重复的信息,以减少输出文档的大小和处理时间。
虽然您可以手动查找和删除未使用的数据(如样式或列表)或重复信息,但使用Aspose.Words提供的功能和功能执行此操作会方便得多。
CleanupOptions类允许您指定用于文档清理的选项。 要从文档中删除重复的样式或只是未使用的样式或列表,可以使用Cleanup方法。
从文档中删除未使用的信息
您可以使用UnusedStyles和UnusedBuiltinStyles属性来检测和删除标记为"未使用"的样式。
您可以使用UnusedLists属性检测并删除标记为"未使用"的列表和列表定义。
下面的代码示例演示如何从文档中仅删除未使用的样式:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(MyDir + u"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. | |
std::cout << (String::Format(u"Count of styles before Cleanup: {0}\n", doc->get_Styles()->get_Count()) + | |
String::Format(u"Count of lists before Cleanup: {0}", doc->get_Lists()->get_Count())) | |
<< std::endl; | |
// Cleans unused styles and lists from the document depending on given CleanupOptions. | |
auto cleanupOptions = MakeObject<CleanupOptions>(); | |
cleanupOptions->set_UnusedLists(false); | |
cleanupOptions->set_UnusedStyles(true); | |
doc->Cleanup(cleanupOptions); | |
std::cout << (String::Format(u"Count of styles after Cleanup was decreased: {0}\n", doc->get_Styles()->get_Count()) + | |
String::Format(u"Count of lists after Cleanup is the same: {0}", doc->get_Lists()->get_Count())) | |
<< std::endl; | |
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.CleanupUnusedStylesAndLists.docx"); |
从文档中删除重复信息
您还可以使用DuplicateStyle属性将所有重复样式替换为原始样式,并从文档中删除重复项。
下面的代码示例演示如何从文档中删除重复的样式:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(MyDir + u"Document.docx"); | |
// Count of styles before Cleanup. | |
std::cout << doc->get_Styles()->get_Count() << std::endl; | |
// Cleans duplicate styles from the document. | |
auto options = MakeObject<CleanupOptions>(); | |
options->set_DuplicateStyle(true); | |
doc->Cleanup(options); | |
// Count of styles after Cleanup was decreased. | |
std::cout << doc->get_Styles()->get_Count() << std::endl; | |
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.CleanupDuplicateStyle.docx"); |