ทำความสะอาดเอกสาร

บางครั้งคุณอาจต้องลบข้อมูลที่ไม่ได้ใช้หรือซ้ำกันเพื่อลดขนาดของเอกสารที่ส่งออกและเว.

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