Dọn Dẹp Tài liệu
Đôi khi bạn có thể cần xóa thông tin không sử dụng hoặc trùng lặp để giảm kích thước của tài liệu đầu ra và thời gian xử lý.
Mặc dù bạn có thể tìm và xóa dữ liệu không sử dụng, chẳng hạn như kiểu hoặc danh sách hoặc thông tin trùng lặp theo cách thủ công, nhưng sẽ thuận tiện hơn nhiều khi thực hiện việc này bằng cách sử dụng các tính năng và khả năng do Aspose.Words cung cấp.
Lớp CleanupOptions cho phép bạn chỉ định các tùy chọn để làm sạch tài liệu. Để xóa các kiểu trùng lặp hoặc chỉ các kiểu hoặc danh sách không sử dụng khỏi tài liệu, bạn có thể sử dụng phương thức Cleanup.
Xóa Thông tin Không Sử dụng Khỏi Tài liệu
Bạn có thể sử dụng thuộc tính UnusedStyles và UnusedBuiltinStyles để phát hiện và xóa các kiểu được đánh dấu là “không sử dụng”.
Bạn có thể sử dụng thuộc tính UnusedLists để phát hiện và xóa danh sách và định nghĩa danh sách được đánh dấu là “không sử dụng”.
Ví dụ mã sau đây cho thấy cách chỉ xóa các kiểu không sử dụng khỏi tài liệu:
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"); |
Xóa Thông tin Trùng lặp khỏi Tài liệu
Bạn cũng có thể sử dụng thuộc tính DuplicateStyle để thay thế tất cả các kiểu trùng lặp bằng kiểu ban đầu và xóa các bản sao khỏi tài liệu.
Ví dụ mã sau đây cho thấy cách xóa các kiểu trùng lặp khỏi tài liệu:
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"); |