Почистване на документ
Понякога може да се наложи да премахнете неизползвана или дублирана информация, за да намалите размера на изходния документ и времето за обработка.
Въпреки че можете да намирате и премахвате неизползвани данни, като например стилове или списъци, или ръчно да дублирате информация, ще бъде много по-удобно да направите това, като използвате функции и възможности, предоставени от 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"); |