文書のクリーンアップ
出力文書のサイズと処理時間を短縮するために、未使用または重複した情報を削除する必要がある場合があります。
スタイルやリストなどの未使用のデータを手動で検索して削除したり、情報を重複させたりすることはできますが、Aspose.Wordsが提供する機能を使用してこれを行う方がはるかに便利です。
CleanupOptionsクラスを使用すると、文書のクリーニングのオプションを指定できます。 文書から重複したスタイルまたは未使用のスタイルまたはリストのみを削除するには、Cleanupメソッドを使用できます。
文書から未使用の情報を削除する
UnusedStylesおよびUnusedBuiltinStylesプロパティを使用して、“未使用"としてマークされているスタイルを検出および削除できます。
UnusedListsプロパティを使用すると、“未使用"としてマークされているリストおよびリスト定義を検出および削除できます。
次のコード例は、未使用のスタイルのみをドキュメントから削除する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
// Count of styles before Cleanup. | |
System.out.println(doc.getStyles().getCount()); | |
// Count of lists before Cleanup. | |
System.out.println(doc.getLists().getCount()); | |
CleanupOptions cleanupoptions = new CleanupOptions(); | |
cleanupoptions.setUnusedLists(false); | |
cleanupoptions.setUnusedStyles(true); | |
// Cleans unused styles and lists from the document depending on given | |
// CleanupOptions. | |
doc.cleanup(cleanupoptions); | |
// Count of styles after Cleanup was decreased. | |
System.out.println(doc.getStyles().getCount()); | |
// Count of lists after Cleanup is the same. | |
System.out.println(doc.getLists().getCount()); | |
doc.save(dataDir + "Document.Cleanup_out.docx"); |
文書から重複した情報を削除する
また、DuplicateStyleプロパティを使用して、すべての重複スタイルを元のスタイルに置き換え、文書から重複を削除することもできます。
次のコード例は、ドキュメントから重複したスタイルを削除する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
// Count of styles before Cleanup. | |
System.out.println(doc.getStyles().getCount()); | |
CleanupOptions options = new CleanupOptions(); | |
options.setDuplicateStyle(true); | |
// Cleans duplicate styles from the document. | |
doc.cleanup(options); | |
// Count of styles after Cleanup was decreased. | |
System.out.println(doc.getStyles().getCount()); | |
doc.save(dataDir + "Document.CleanupDuplicateStyle_out.docx"); |