Wyczyść dokument

Czasami może być konieczne usunięcie nieużywanych lub zduplikowanych informacji, aby zmniejszyć rozmiar dokumentu wyjściowego i czas przetwarzania.

Chociaż możesz ręcznie znajdować i usuwać nieużywane dane, takie jak style lub listy, lub powielać informacje, znacznie wygodniej będzie to zrobić za pomocą funkcji i możliwości dostarczonych przez Aspose.Words.

Klasa CleanupOptions umożliwia określenie opcji czyszczenia dokumentów. Aby usunąć zduplikowane style lub po prostu nieużywane style lub listy z dokumentu, możesz użyć metody Cleanup.

Usuń nieużywane informacje z dokumentu

Możesz użyć właściwości UnusedStyles i UnusedBuiltinStyles do wykrywania i usuwania stylów oznaczonych jako “nieużywane”.

Możesz użyć właściwości UnusedLists do wykrywania i usuwania list i definicji list oznaczonych jako “nieużywane”.

Poniższy przykład kodu pokazuje, jak usunąć tylko nieużywane style z dokumentu:

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");

Usuwanie zduplikowanych informacji z dokumentu

Możesz także użyć właściwości DuplicateStyle, aby zastąpić wszystkie zduplikowane style oryginalnym i usunąć duplikaty z dokumentu.

Poniższy przykład kodu pokazuje, jak usunąć zduplikowane style z dokumentu:

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");