ドキュメントをクリーンアップする
出力ドキュメントのサイズと処理時間を削減するために、未使用または重複した情報を削除する必要がある場合があります。
スタイルやリストなどの未使用のデータや重複した情報を手動で検索して削除することもできますが、Aspose.Words が提供する機能を使用してこれを行う方がはるかに便利です。
CleanupOptions クラスを使用すると、ドキュメント クリーニングのオプションを指定できます。重複したスタイル、または未使用のスタイルやリストのみをドキュメントから削除するには、Cleanup メソッドを使用します。
ドキュメントから未使用の情報を削除する
UnusedStyles プロパティと UnusedBuiltinStyles プロパティを使用すると、「未使用」としてマークされたスタイルを検出して削除できます。
UnusedLists プロパティを使用すると、「未使用」としてマークされているリストおよびリスト定義を検出して削除できます。
次のコード例は、ドキュメントから未使用のスタイルのみを削除する方法を示しています。
// 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"); |
文書から重複情報を削除する
DuplicateStyle プロパティを使用して、重複するすべてのスタイルを元のスタイルに置き換えたり、ドキュメントから重複を削除したりすることもできます。
次のコード例は、ドキュメントから重複したスタイルを削除する方法を示しています。
// 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"); |