Clean Up a Document

Sometimes you may need to remove unused or duplicate information to reduce the size of the output document and processing time.

While you can find and remove unused data, such as styles or lists, or duplicate information manually, it will be much more convenient to do this using features and capabilities provided by Aspose.Words.

The CleanupOptions class allows you to specify options for document cleaning. To remove duplicate styles or just unused styles or lists from the document, you can use the Cleanup method.

Remove Unused Information from a Document

You can use the UnusedStyles and UnusedBuiltinStyles properties to detect and remove styles that are marked as “unused”.

You can use the UnusedLists property to detect and remove lists and list definitions that are marked as “unused”.

The following code example shows how to remove only unused styles from a document:

Remove Duplicate Information from a Document

You can also use the DuplicateStyle property to substitute all duplicate styles with the original one and remove duplicates from a document.

The following code example shows how to remove duplicate styles from a document:


FAQ

  1. Q: What does the Document::Cleanup method do?
    A: Document::Cleanup analyses the document according to the options set in a CleanupOptions object and removes or consolidates unused or duplicate elements such as styles, built‑in styles, lists, and duplicate style definitions. It does not alter the visible content of the document.

  2. Q: How can I remove only unused custom styles from a document?
    A: Create a CleanupOptions instance, set UnusedStyles to true, and pass it to Document::Cleanup. Example:

    Aspose::Words::CleanupOptions options;
    options.set_UnusedStyles(true);
    document.Cleanup(options);
    
  3. Q: How do I eliminate duplicate styles while keeping the original style intact?
    A: Enable the DuplicateStyle option in CleanupOptions. The method will replace all occurrences of duplicate styles with the first (original) style and then delete the duplicates.

    Aspose::Words::CleanupOptions options;
    options.set_DuplicateStyle(true);
    document.Cleanup(options);
    
  4. Q: Can the cleanup process also remove unused built‑in styles and lists?
    A: Yes. Set UnusedBuiltinStyles and/or UnusedLists to true in the CleanupOptions object before calling Cleanup. This will purge any built‑in styles or list definitions that are not referenced in the document.

  5. Q: Will using Cleanup affect the formatting or layout of the document?
    A: No. Cleanup only removes or merges internal definitions that are not used. The visible text, formatting, and layout remain unchanged, but the file size may be reduced and processing can become faster.