DocumentCleanup Method
Cleans unused styles and lists from the document.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
public void Cleanup()
Shows how to remove unused styles and lists from a document.
// Create a new document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Brand new documents have 4 styles and 0 lists by default
Assert.AreEqual(4, doc.Styles.Count);
Assert.AreEqual(0, doc.Lists.Count);

// We will add one style and one list and mark them as "used" by applying them to the builder 
builder.ParagraphFormat.Style = doc.Styles.Add(StyleType.Paragraph, "My Used Style");
builder.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDiamonds);

// These items were added to their respective collections
Assert.AreEqual(5, doc.Styles.Count);
Assert.AreEqual(1, doc.Lists.Count);

// doc.Cleanup() removes all unused styles and lists

// It currently has no effect because the 2 items we added plus the original 4 styles are all used
Assert.AreEqual(5, doc.Styles.Count);
Assert.AreEqual(1, doc.Lists.Count);

// These two items will be added but will not associated with any part of the document
doc.Styles.Add(StyleType.Paragraph, "My Unused Style");

// They also get stored in the document and are ready to be used
Assert.AreEqual(6, doc.Styles.Count);
Assert.AreEqual(2, doc.Lists.Count);


// Since we didn't apply them anywhere, the two unused items are removed by doc.Cleanup()
Assert.AreEqual(5, doc.Styles.Count);
Assert.AreEqual(1, doc.Lists.Count);
See Also