public class CleanupOptions
extends java.lang.Object
To learn more, visit the Clean Up a Document documentation article.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
| Modifier and Type | Method and Description |
|---|---|
boolean |
getDuplicateStyle()
Gets/sets a flag indicating whether duplicate styles should be removed from document.
|
boolean |
getUnusedBuiltinStyles()
Specifies that unused
Style.getBuiltIn() styles should be removed from document. |
boolean |
getUnusedLists()
Specifies whether unused list and list definitions should be removed from document.
|
boolean |
getUnusedStyles()
Specifies whether unused styles should be removed from document.
|
void |
setDuplicateStyle(boolean value)
Gets/sets a flag indicating whether duplicate styles should be removed from document.
|
void |
setUnusedBuiltinStyles(boolean value)
Specifies that unused
Style.getBuiltIn() styles should be removed from document. |
void |
setUnusedLists(boolean value)
Specifies whether unused list and list definitions should be removed from document.
|
void |
setUnusedStyles(boolean value)
Specifies whether unused styles should be removed from document.
|
public boolean getUnusedStyles()
true.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
boolean value.public void setUnusedStyles(boolean value)
true.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
value - The corresponding boolean value.public boolean getUnusedLists()
true.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
boolean value.public void setUnusedLists(boolean value)
true.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
value - The corresponding boolean value.public boolean getDuplicateStyle()
false.
Examples:
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());
boolean value.public void setDuplicateStyle(boolean value)
false.
Examples:
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());
value - The corresponding boolean value.public boolean getUnusedBuiltinStyles()
Style.getBuiltIn() styles should be removed from document.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
boolean value.public void setUnusedBuiltinStyles(boolean value)
Style.getBuiltIn() styles should be removed from document.
Examples:
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 docList = doc.getLists().add(doc.getStyles().get("MyListStyle1"));
builder.getListFormat().setList(docList);
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());
value - The corresponding boolean value.