Aspose::Words::CleanupOptions class

CleanupOptions class

Allows to specify options for document cleaning. To learn more, visit the Clean Up a Document documentation article.

class CleanupOptions : public System::Object

Methods

MethodDescription
CleanupOptions()
get_DuplicateStyle() constGets/sets a flag indicating whether duplicate styles should be removed from document. Default value is false.
get_UnusedBuiltinStyles() constSpecifies that unused BuiltIn styles should be removed from document.
get_UnusedLists() constSpecifies whether unused list and list definitions should be removed from document. Default value is true.
get_UnusedStyles() constSpecifies whether unused styles should be removed from document. Default value is true.
GetType() const override
Is(const System::TypeInfo&) const override
set_DuplicateStyle(bool)Setter for Aspose::Words::CleanupOptions::get_DuplicateStyle.
set_UnusedBuiltinStyles(bool)Setter for Aspose::Words::CleanupOptions::get_UnusedBuiltinStyles.
set_UnusedLists(bool)Setter for Aspose::Words::CleanupOptions::get_UnusedLists.
set_UnusedStyles(bool)Setter for Aspose::Words::CleanupOptions::get_UnusedStyles.
static Type()

Examples

Shows how to remove all unused custom styles from a document.

auto doc = MakeObject<Document>();

doc->get_Styles()->Add(StyleType::List, u"MyListStyle1");
doc->get_Styles()->Add(StyleType::List, u"MyListStyle2");
doc->get_Styles()->Add(StyleType::Character, u"MyParagraphStyle1");
doc->get_Styles()->Add(StyleType::Character, u"MyParagraphStyle2");

// 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.
ASSERT_EQ(8, doc->get_Styles()->get_Count());

// Apply a custom character style, and then a custom list style. Doing so will mark them as "used".
auto builder = MakeObject<DocumentBuilder>(doc);
builder->get_Font()->set_Style(doc->get_Styles()->idx_get(u"MyParagraphStyle1"));
builder->Writeln(u"Hello world!");

SharedPtr<Aspose::Words::Lists::List> list = doc->get_Lists()->Add(doc->get_Styles()->idx_get(u"MyListStyle1"));
builder->get_ListFormat()->set_List(list);
builder->Writeln(u"Item 1");
builder->Writeln(u"Item 2");

// Now, there is one unused character style and one unused list style.
// The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them.
auto cleanupOptions = MakeObject<CleanupOptions>();
cleanupOptions->set_UnusedLists(true);
cleanupOptions->set_UnusedStyles(true);
cleanupOptions->set_UnusedBuiltinStyles(true);

doc->Cleanup(cleanupOptions);

ASSERT_EQ(4, doc->get_Styles()->get_Count());

// Removing every node that a custom style is applied to marks it as "unused" again.
// Rerun the Cleanup method to remove them.
doc->get_FirstSection()->get_Body()->RemoveAllChildren();
doc->Cleanup(cleanupOptions);

ASSERT_EQ(2, doc->get_Styles()->get_Count());

See Also