Vyčistit dokument
Někdy můžete potřebovat odstranit nepoužité nebo duplikované informace ke snížení velikosti výstupního dokumentu a času zpracování.
Zatímco můžete najít a odstranit nevyužitá data, jako jsou styly nebo seznamy, nebo duplikovat informace ručně, bude mnohem pohodlnější, aby to pomocí funkcí a schopností poskytnutých Aspose.Words.
• CleanupOptions třída umožňuje určit možnosti pro čištění dokumentů. Chcete-li odstranit duplikát stylů nebo jen nepoužité styly nebo seznamy z dokumentu, můžete použít Cleanup metoda.
Odstranit nepoužité informace z dokumentu
Můžete použít UnusedStyles a UnusedBuiltinStyles vlastnosti pro detekci a odstranění stylů, které jsou označeny jako “nepoužité.”
Můžete použít UnusedLists vlastnost odhalit a odstranit seznamy a definice seznamu, které jsou označeny jako “nepoužité.”
Následující příklad kódu ukazuje, jak odstranit pouze nepoužité styly 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 + "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"); |
Odstranit dvojí informace z dokumentu
Můžete také použít DuplicateStyle vlastnost nahradit všechny duplikátní styly originálem a odstranit duplikáty z dokumentu.
Následující příklad kódu ukazuje, jak odstranit duplikát stylů 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"); |