DocumentCleanup(CleanupOptions) Method

Cleans unused styles and lists from the document depending on given CleanupOptions.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0

Syntax

public void Cleanup(
	CleanupOptions options
)

Parameters

options
Type: Aspose.WordsCleanupOptions

Examples

Shows how to remove all unused styles and lists from a document.
Document doc = new Document();

// Insert some styles into a blank document
doc.Styles.Add(StyleType.List, "MyListStyle1");
doc.Styles.Add(StyleType.List, "MyListStyle2");
doc.Styles.Add(StyleType.Character, "MyParagraphStyle1");
doc.Styles.Add(StyleType.Character, "MyParagraphStyle2");

// Combined with the built in styles, the document now has 8 styles in total,
// but all 4 of the ones we added count as unused
Assert.AreEqual(8, doc.Styles.Count);

// A character style counts as used when the document contains text in that style
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Style = doc.Styles["MyParagraphStyle1"];
builder.Writeln("Hello world!");

// A list style is also "used" when there is a list that uses it
Aspose.Words.Lists.List list = doc.Lists.Add(doc.Styles["MyListStyle1"]);
builder.ListFormat.List = list;
builder.Writeln("Item 1");
builder.Writeln("Item 2");

// The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them
CleanupOptions cleanupOptions = new CleanupOptions();
cleanupOptions.UnusedLists = true;
cleanupOptions.UnusedStyles = true;

// We've added 4 styles and used 2 of them, so the other two will be removed when this method is called
doc.Cleanup(cleanupOptions);
Assert.AreEqual(6, doc.Styles.Count);

See Also