Zastosuj styl tabeli
Styl tabeli definiuje zestaw formatowania, który można łatwo zastosować do tabeli. Formatowanie takie jak granice, cieniowanie, wyrównanie i czcionka mogą być ustawione w stylu tabeli i stosowane do wielu tabel dla spójnego wyglądu.
Aspose.Words obsługuje stosowanie stylu tabeli do tabeli, a także właściwości czytania każdego stylu tabeli. Style tabeli są zachowywane podczas załadunku i oszczędzania w następujący sposób:
- Style tabeli w formatach DOCX i WordML są zachowane przy załadunku i zapisywaniu do tych formatów
- Style tabeli są zachowane podczas wczytywania i zapisywania w formacie DOC (ale nie do innego formatu)
- Podczas eksportu do innych formatów, renderowania lub drukowania, style tabeli są rozszerzone do bezpośredniego formatowania w tabeli, więc wszystkie formatowanie jest zachowane
Utwórz styl tabeli
Użytkownik może stworzyć nowy styl i dodać go do kolekcji stylu. W Add metoda jest używana do tworzenia nowego stylu tabeli.
Poniższy przykład kodu pokazuje jak stworzyć nowy styl tabeli zdefiniowany przez użytkownika:
// 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"); |
Kopiuj istniejący styl tabeli
W razie potrzeby można skopiować styl tabeli, który już istnieje w określonym dokumencie do kolekcji stylu za pomocą AddCopy
Metoda.
Ważne jest, aby wiedzieć, że z tego kopiowania, powiązane style są również kopiowane.
Poniższy przykład kodu pokazuje jak zaimportować styl z jednego dokumentu do innego dokumentu:
// 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()); |
Zastosuj istniejący styl tabeli
Aspose.Words zapewnia TableStyle po Style Klasa. TableStyle ułatwia użytkownikowi stosowanie różnych opcji stylu, takich jak cieniowanie, ścieranie, wgłębienie, CellSpacing oraz Font, itd.
Ponadto, Aspose.Words zapewnia StyleCollection klasy i kilka właściwości Table
klasa w celu określenia stylu tabeli będziemy pracować z: Style, StyleIdentifier, StyleName, oraz StyleOptions.
Aspose.Words również zapewnia ConditionalStyle klasy, która reprezentuje specjalne formatowanie stosowane na niektórych obszarach tabeli z przypisanym stylu tabeli, i ConditionalStyleCollection który reprezentuje kolekcję ConditionalStyle obiektów. Ta kolekcja zawiera stały zestaw elementów reprezentujących jedną pozycję dla każdej wartości ConditionalStyleType rodzaj wyliczenia. W ConditionalStyleType wyliczenie określa wszystkie możliwe obszary tabeli, do których można zdefiniować formatowanie warunkowe w stylu tabeli.
W takim przypadku można określić formatowanie warunkowe dla wszystkich możliwych powierzchni tabeli zdefiniowanych w ramach rodzaju wyliczenia ConditionalStyleType.
Poniższy przykład kodu pokazuje jak zdefiniować warunkowe formatowanie dla nagłówka tabeli:
// 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"); |
Można również wybrać, które części tabeli do zastosowania stylów, takich jak pierwsza kolumna, ostatnia kolumna, rząd banded. Są one wymienione w TableStyleOptions wyliczenie i są stosowane przez StyleOptions nieruchomości. W TableStyleOptions wyliczenie pozwala na kombinację tych funkcji.
Poniższy przykład kodu pokazuje, jak stworzyć nową tabelę z zastosowaniem stylu tabeli:
// 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"); |
Poniższe zdjęcia przedstawiają reprezentację Table Styles w Microsoft Word i ich właściwości w Aspose.Words.
Zastosuj formatowanie ze stylu tabeli jako bezpośrednie formatowanie
Aspose.Words zapewnia również ExpandTableStylesToDirectFormatting metoda podejmowania formatowania znalezionego na stole stylu i rozszerza go na wiersze i komórki tabeli jako bezpośrednie formatowanie. Spróbuj połączyć formatowanie z stylu tabeli i stylu komórki.
Poniższy przykład kodu pokazuje jak rozszerzyć formatowanie ze stylów na wiersze i komórki tabeli jako bezpośrednie formatowanie:
// 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); |