ทำความสะอาดเอกสาร
บางครั้งคุณอาจต้องลบข้อมูลที่ไม่ได้ใช้หรือซ้ำกันออกเพื่อลดขนาดของเอกสารเอาต์พุตและเวลาในการประมวลผล
แม้ว่าคุณจะสามารถค้นหาและลบข้อมูลที่ไม่ได้ใช้ เช่น สไตล์หรือรายการ หรือข้อมูลที่ซ้ำกันด้วยตนเองได้ แต่จะสะดวกกว่ามากหากใช้ฟีเจอร์และความสามารถที่ 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"); |