Czyszczenie dokumentu

Czasami może być konieczne usunięcie niewykorzystanych lub duplikatów informacji w celu zmniejszenia rozmiaru dokumentu wyjściowego i czasu przetwarzania.

Podczas gdy można znaleźć i usunąć niewykorzystane dane, takie jak style lub listy, lub powielać informacje ręcznie, będzie o wiele wygodniej to zrobić za pomocą funkcji i możliwości dostarczonych przez Aspose.Words.

W CleanupOptions klasa pozwala określić opcje czyszczenia dokumentów. Aby usunąć duplikaty stylów lub tylko nieużywane style lub listy z dokumentu, można użyć Cleanup Metoda.

Usuń niewykorzystane informacje z dokumentu

Można użyć UnusedStyles oraz UnusedBuiltinStyles właściwości do wykrywania i usuwania stylów, które są oznaczone jako “nieużywane”.

Można użyć UnusedLists właściwość do wykrywania i usuwania list i definicji list oznaczonych jako “niewykorzystane”.

Poniższy przykład kodu pokazuje jak usunąć tylko nieużywane style z dokumentu:

// 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");

Usuń powielone informacje z dokumentu

Można również użyć DuplicateStyle właściwość do zastąpienia wszystkich duplikatów stylami oryginału i usunięcia duplikatów z dokumentu.

Poniższy przykład kodu pokazuje jak usunąć duplikaty stylów z dokumentu:

// 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");