应用表格样式
表格样式定义了一组可轻松应用于表格的格式。 可以在表格样式中设置边框、阴影、对齐和字体等格式,并将其应用于许多表格以获得一致的外观。
Aspose.Words支持将表样式应用于表,也支持读取任何表样式的属性。 通过以下方式在加载和保存期间保留表样式:
- 加载和保存为这些格式时,将保留DOCX和WordML格式的表格样式
- 以DOC格式加载和保存时会保留表格样式(但不会保留为任何其他格式)
- 导出为其他格式、呈现或打印时,表格样式会扩展为表格中的直接格式,因此会保留所有格式
创建表格样式
用户可以创建新样式并将其添加到样式集合中。 Add方法用于创建新的表格样式。
下面的代码示例演示如何创建新的用户定义表样式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
builder.write("Name"); | |
builder.insertCell(); | |
builder.write("Value"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.insertCell(); | |
builder.endTable(); | |
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); | |
tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE); | |
tableStyle.getBorders().setLineWidth(1.0); | |
tableStyle.setLeftPadding(18.0); | |
tableStyle.setRightPadding(18.0); | |
tableStyle.setTopPadding(12.0); | |
tableStyle.setBottomPadding(12.0); | |
table.setStyle(tableStyle); | |
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx"); |
复制现有表格样式
如有必要,您可以使用AddCopy
方法将某个文档中已存在的表格样式复制到样式集合中。
重要的是要知道,通过这种复制,链接的样式也会被复制。
下面的代码示例演示如何将样式从一个文档导入到另一个文档:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document srcDoc = new Document(); | |
// Create a custom style for the source document. | |
Style srcStyle = srcDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle"); | |
srcStyle.getFont().setColor(Color.RED); | |
// Import the source document's custom style into the destination document. | |
Document dstDoc = new Document(); | |
Style newStyle = dstDoc.getStyles().addCopy(srcStyle); | |
// The imported style has an appearance identical to its source style. | |
Assert.assertEquals("MyStyle", newStyle.getName()); | |
Assert.assertEquals(Color.RED.getRGB(), newStyle.getFont().getColor().getRGB()); |
应用现有表格样式
Aspose.Words提供从Style类继承的TableStyle。 TableStyle方便用户应用不同的样式选项,如阴影、填充、缩进、CellSpacing和Font等。
此外,Aspose.Words提供了StyleCollection类和Table
类的一些属性,以指定我们将使用哪种表样式: Style, StyleIdentifier, StyleName, 和StyleOptions。
Aspose.Words还提供了ConditionalStyle类,该类表示应用于具有指定表格样式的表格的某些区域的特殊格式,以及表示ConditionalStyle对象集合的ConditionalStyleCollection。 此集合包含一组永久项,表示ConditionalStyleType枚举类型的每个值的一个项。 ConditionalStyleType枚举定义了可以在表格样式中定义条件格式的所有可能的表格区域。
在这种情况下,可以为ConditionalStyleType枚举类型下定义的所有可能的表区域定义条件格式。
下面的代码示例演示如何为表的标题行定义条件格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
builder.write("Name"); | |
builder.insertCell(); | |
builder.write("Value"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.insertCell(); | |
builder.endTable(); | |
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); | |
tableStyle.getConditionalStyles().getFirstRow().getShading().setBackgroundPatternColor(Color.yellow); | |
tableStyle.getConditionalStyles().getFirstRow().getShading().setTexture(TextureIndex.TEXTURE_NONE); | |
table.setStyle(tableStyle); | |
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx"); |
您还可以选择要应用样式的表格部分,例如第一列、最后一列、带状行。 它们在TableStyleOptions枚举中列出,并通过StyleOptions属性应用。 TableStyleOptions枚举允许按位组合这些功能。
下面的代码示例演示如何创建应用了表样式的新表:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.startTable(); | |
// We must insert at least one row first before setting any table formatting. | |
builder.insertCell(); | |
// Set the table style used based on the unique style identifier. | |
table.setStyleIdentifier(StyleIdentifier.MEDIUM_SHADING_1_ACCENT_1); | |
// Apply which features should be formatted by the style. | |
table.setStyleOptions(TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS | TableStyleOptions.FIRST_ROW); | |
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS); | |
builder.writeln("Item"); | |
builder.getCellFormat().setRightPadding(40.0); | |
builder.insertCell(); | |
builder.writeln("Quantity (kg)"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.writeln("Apples"); | |
builder.insertCell(); | |
builder.writeln("20"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.writeln("Bananas"); | |
builder.insertCell(); | |
builder.writeln("40"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.writeln("Carrots"); | |
builder.insertCell(); | |
builder.writeln("50"); | |
builder.endRow(); | |
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx"); |
下面的图片显示了Microsoft Word中的Table Styles及其在Aspose.Words中的相应属性的表示。
从表格样式中获取格式并将其应用为直接格式
Aspose.Words还提供了ExpandTableStylesToDirectFormatting方法来获取表格样式上的格式,并将其作为直接格式扩展到表格的行和单元格中。 尝试将格式与表格样式和单元格样式相结合。
下面的代码示例演示如何将格式从样式扩展到表行和单元格中,作为直接格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Tables.docx"); | |
// Get the first cell of the first table in the document. | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
Cell firstCell = table.getFirstRow().getFirstCell(); | |
// First print the color of the cell shading. | |
// This should be empty as the current shading is stored in the table style. | |
Color cellShadingBefore = firstCell.getCellFormat().getShading().getBackgroundPatternColor(); | |
System.out.println("Cell shading before style expansion: " + cellShadingBefore); | |
doc.expandTableStylesToDirectFormatting(); | |
// Now print the cell shading after expanding table styles. | |
// A blue background pattern color should have been applied from the table style. | |
Color cellShadingAfter = firstCell.getCellFormat().getShading().getBackgroundPatternColor(); | |
System.out.println("Cell shading after style expansion: " + cellShadingAfter); |