com.aspose.words

Class CleanupOptions

  • java.lang.Object
    • com.aspose.words.CleanupOptions
public class CleanupOptions 
extends java.lang.Object

Allows to specify options for document cleaning.

Example:

Shows how to remove all unused custom styles from a document.
Document doc = new Document();

doc.getStyles().add(StyleType.LIST, "MyListStyle1");
doc.getStyles().add(StyleType.LIST, "MyListStyle2");
doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1");
doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2");

// 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.
Assert.assertEquals(8, doc.getStyles().getCount());

// Apply a custom character style, and then a custom list style. Doing so will mark them as "used".
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1"));
builder.writeln("Hello world!");

List list = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(list);
builder.writeln("Item 1");
builder.writeln("Item 2");

// Now, there is one unused character style and one unused list style.
// The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them.
CleanupOptions cleanupOptions = new CleanupOptions();
cleanupOptions.setUnusedLists(true);
cleanupOptions.setUnusedStyles(true);
cleanupOptions.setUnusedBuiltinStyles(true);

doc.cleanup(cleanupOptions);

Assert.assertEquals(4, doc.getStyles().getCount());

// Removing every node that a custom style is applied to marks it as "unused" again. 
// Rerun the Cleanup method to remove them.
doc.getFirstSection().getBody().removeAllChildren();
doc.cleanup(cleanupOptions);

Assert.assertEquals(2, doc.getStyles().getCount());

Constructor Summary
 
Property Getters/Setters Summary
booleangetDuplicateStyle()
void
setDuplicateStyle(booleanvalue)
           Gets/sets a flag indicating whether duplicate styles should be removed from document. Default value is false.
booleangetUnusedBuiltinStyles()
void
           Specifies that unused Style.BuiltIn styles should be removed from document.
booleangetUnusedLists()
void
setUnusedLists(booleanvalue)
           Specifies whether unused list and list definitions should be removed from document. Default value is true.
booleangetUnusedStyles()
void
setUnusedStyles(booleanvalue)
           Specifies whether unused styles should be removed from document. Default value is true.
 

    • Constructor Detail

      • CleanupOptions

        public CleanupOptions()
    • Property Getters/Setters Detail

      • getDuplicateStyle/setDuplicateStyle

        public boolean getDuplicateStyle() / public void setDuplicateStyle(boolean value)
        
        Gets/sets a flag indicating whether duplicate styles should be removed from document. Default value is false.

        Example:

        Shows how to remove duplicated styles from the document.
        Document doc = new Document();
        
        // Add two styles to the document with identical properties,
        // but different names. The second style is considered a duplicate of the first.
        Style myStyle = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
        myStyle.getFont().setSize(14.0);
        myStyle.getFont().setName("Courier New");
        myStyle.getFont().setColor(Color.BLUE);
        
        Style duplicateStyle = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle2");
        duplicateStyle.getFont().setSize(14.0);
        duplicateStyle.getFont().setName("Courier New");
        duplicateStyle.getFont().setColor(Color.BLUE);
        
        Assert.assertEquals(6, doc.getStyles().getCount());
        
        // Apply both styles to different paragraphs within the document.
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getParagraphFormat().setStyleName(myStyle.getName());
        builder.writeln("Hello world!");
        
        builder.getParagraphFormat().setStyleName(duplicateStyle.getName());
        builder.writeln("Hello again!");
        
        ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs();
        
        Assert.assertEquals(myStyle, paragraphs.get(0).getParagraphFormat().getStyle());
        Assert.assertEquals(duplicateStyle, paragraphs.get(1).getParagraphFormat().getStyle());
        
        // Configure a CleanOptions object, then call the Cleanup method to substitute all duplicate styles
        // with the original and remove the duplicates from the document.
        CleanupOptions cleanupOptions = new CleanupOptions();
        cleanupOptions.setDuplicateStyle(true);
        
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(5, doc.getStyles().getCount());
        Assert.assertEquals(myStyle, paragraphs.get(0).getParagraphFormat().getStyle());
        Assert.assertEquals(myStyle, paragraphs.get(1).getParagraphFormat().getStyle());
      • getUnusedBuiltinStyles/setUnusedBuiltinStyles

        public boolean getUnusedBuiltinStyles() / public void setUnusedBuiltinStyles(boolean value)
        
        Specifies that unused Style.BuiltIn styles should be removed from document.

        Example:

        Shows how to remove all unused custom styles from a document.
        Document doc = new Document();
        
        doc.getStyles().add(StyleType.LIST, "MyListStyle1");
        doc.getStyles().add(StyleType.LIST, "MyListStyle2");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2");
        
        // 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.
        Assert.assertEquals(8, doc.getStyles().getCount());
        
        // Apply a custom character style, and then a custom list style. Doing so will mark them as "used".
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1"));
        builder.writeln("Hello world!");
        
        List list = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
        builder.getListFormat().setList(list);
        builder.writeln("Item 1");
        builder.writeln("Item 2");
        
        // Now, there is one unused character style and one unused list style.
        // The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them.
        CleanupOptions cleanupOptions = new CleanupOptions();
        cleanupOptions.setUnusedLists(true);
        cleanupOptions.setUnusedStyles(true);
        cleanupOptions.setUnusedBuiltinStyles(true);
        
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(4, doc.getStyles().getCount());
        
        // Removing every node that a custom style is applied to marks it as "unused" again. 
        // Rerun the Cleanup method to remove them.
        doc.getFirstSection().getBody().removeAllChildren();
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(2, doc.getStyles().getCount());
      • getUnusedLists/setUnusedLists

        public boolean getUnusedLists() / public void setUnusedLists(boolean value)
        
        Specifies whether unused list and list definitions should be removed from document. Default value is true.

        Example:

        Shows how to remove all unused custom styles from a document.
        Document doc = new Document();
        
        doc.getStyles().add(StyleType.LIST, "MyListStyle1");
        doc.getStyles().add(StyleType.LIST, "MyListStyle2");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2");
        
        // 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.
        Assert.assertEquals(8, doc.getStyles().getCount());
        
        // Apply a custom character style, and then a custom list style. Doing so will mark them as "used".
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1"));
        builder.writeln("Hello world!");
        
        List list = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
        builder.getListFormat().setList(list);
        builder.writeln("Item 1");
        builder.writeln("Item 2");
        
        // Now, there is one unused character style and one unused list style.
        // The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them.
        CleanupOptions cleanupOptions = new CleanupOptions();
        cleanupOptions.setUnusedLists(true);
        cleanupOptions.setUnusedStyles(true);
        cleanupOptions.setUnusedBuiltinStyles(true);
        
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(4, doc.getStyles().getCount());
        
        // Removing every node that a custom style is applied to marks it as "unused" again. 
        // Rerun the Cleanup method to remove them.
        doc.getFirstSection().getBody().removeAllChildren();
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(2, doc.getStyles().getCount());
      • getUnusedStyles/setUnusedStyles

        public boolean getUnusedStyles() / public void setUnusedStyles(boolean value)
        
        Specifies whether unused styles should be removed from document. Default value is true.

        Example:

        Shows how to remove all unused custom styles from a document.
        Document doc = new Document();
        
        doc.getStyles().add(StyleType.LIST, "MyListStyle1");
        doc.getStyles().add(StyleType.LIST, "MyListStyle2");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle1");
        doc.getStyles().add(StyleType.CHARACTER, "MyParagraphStyle2");
        
        // 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.
        Assert.assertEquals(8, doc.getStyles().getCount());
        
        // Apply a custom character style, and then a custom list style. Doing so will mark them as "used".
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getFont().setStyle(doc.getStyles().get("MyParagraphStyle1"));
        builder.writeln("Hello world!");
        
        List list = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
        builder.getListFormat().setList(list);
        builder.writeln("Item 1");
        builder.writeln("Item 2");
        
        // Now, there is one unused character style and one unused list style.
        // The Cleanup() method, when configured with a CleanupOptions object, can target unused styles and remove them.
        CleanupOptions cleanupOptions = new CleanupOptions();
        cleanupOptions.setUnusedLists(true);
        cleanupOptions.setUnusedStyles(true);
        cleanupOptions.setUnusedBuiltinStyles(true);
        
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(4, doc.getStyles().getCount());
        
        // Removing every node that a custom style is applied to marks it as "unused" again. 
        // Rerun the Cleanup method to remove them.
        doc.getFirstSection().getBody().removeAllChildren();
        doc.cleanup(cleanupOptions);
        
        Assert.assertEquals(2, doc.getStyles().getCount());