Oczyść 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 znaleźć i usunąć nieużywane dane, takie jak style lub listy, lub ręcznie zduplikowane informacje, znacznie wygodniej będzie to zrobić, korzystając z funkcji i możliwości zapewnianych przez Aspose.Words.

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

Usuń nieużywane informacje z dokumentu

Możesz użyć właściwości UnusedStyles i UnusedBuiltinStyles, aby wykryć i usunąć style oznaczone jako “nieużywane”.

Za pomocą właściwości UnusedLists można wykrywać i usuwać listy i definicje list oznaczone jako “nieużywane”.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "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.
Console.WriteLine($"Count of styles before Cleanup: {doc.Styles.Count}\n" +
$"Count of lists before Cleanup: {doc.Lists.Count}");
// Cleans unused styles and lists from the document depending on given CleanupOptions.
CleanupOptions cleanupOptions = new CleanupOptions { UnusedLists = false, UnusedStyles = true };
doc.Cleanup(cleanupOptions);
Console.WriteLine($"Count of styles after Cleanup was decreased: {doc.Styles.Count}\n" +
$"Count of lists after Cleanup is the same: {doc.Lists.Count}");
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.CleanupUnusedStylesAndLists.docx");

Usuń zduplikowane informacje 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-.NET
Document doc = new Document(MyDir + "Document.docx");
// Count of styles before Cleanup.
Console.WriteLine(doc.Styles.Count);
// Cleans duplicate styles from the document.
CleanupOptions options = new CleanupOptions { DuplicateStyle = true };
doc.Cleanup(options);
// Count of styles after Cleanup was decreased.
Console.WriteLine(doc.Styles.Count);
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.CleanupDuplicateStyle.docx");