文書のクリーンアップ

出力文書のサイズと処理時間を短縮するために、未使用または重複した情報を削除する必要がある場合があります。

スタイルやリストなどの未使用のデータを手動で検索して削除したり、情報を重複させたりすることはできますが、Aspose.Wordsが提供する機能を使用してこれを行う方がはるかに便利です。

CleanupOptionsクラスを使用すると、文書のクリーニングのオプションを指定できます。 文書から重複したスタイルまたは未使用のスタイルまたはリストのみを削除するには、Cleanupメソッドを使用できます。

文書から未使用の情報を削除する

UnusedStylesおよびUnusedBuiltinStylesプロパティを使用して、“未使用"としてマークされているスタイルを検出および削除できます。

UnusedListsプロパティを使用すると、“未使用"としてマークされているリストおよびリスト定義を検出および削除できます。

次のコード例は、未使用のスタイルのみをドキュメントから削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"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.
std::cout << (String::Format(u"Count of styles before Cleanup: {0}\n", doc->get_Styles()->get_Count()) +
String::Format(u"Count of lists before Cleanup: {0}", doc->get_Lists()->get_Count()))
<< std::endl;
// Cleans unused styles and lists from the document depending on given CleanupOptions.
auto cleanupOptions = MakeObject<CleanupOptions>();
cleanupOptions->set_UnusedLists(false);
cleanupOptions->set_UnusedStyles(true);
doc->Cleanup(cleanupOptions);
std::cout << (String::Format(u"Count of styles after Cleanup was decreased: {0}\n", doc->get_Styles()->get_Count()) +
String::Format(u"Count of lists after Cleanup is the same: {0}", doc->get_Lists()->get_Count()))
<< std::endl;
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.CleanupUnusedStylesAndLists.docx");

文書から重複した情報を削除する

また、DuplicateStyleプロパティを使用して、すべての重複スタイルを元のスタイルに置き換え、文書から重複を削除することもできます。

次のコード例は、ドキュメントから重複したスタイルを削除する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>(MyDir + u"Document.docx");
// Count of styles before Cleanup.
std::cout << doc->get_Styles()->get_Count() << std::endl;
// Cleans duplicate styles from the document.
auto options = MakeObject<CleanupOptions>();
options->set_DuplicateStyle(true);
doc->Cleanup(options);
// Count of styles after Cleanup was decreased.
std::cout << doc->get_Styles()->get_Count() << std::endl;
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.CleanupDuplicateStyle.docx");