テーブルスタイルの適用

テーブルスタイルは、テーブルに簡単に適用できる一連の書式設定を定義します。 罫線、陰影、配置、フォントなどの書式設定は、テーブルスタイルで設定し、一貫した外観のために多くのテーブルに適用できます。

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は、シェーディング、パディング、インデント、CellSpacingFontなどのような異なるスタイルオプションを適用するためにユーザーを容易にします。

さらに、Aspose.WordsはStyleCollectionクラスとTableクラスのいくつかのプロパティを提供し、どのテーブルスタイルを使用するかを指定します: Style, StyleIdentifier, StyleName, とStyleOptions

Aspose.Wordsは、割り当てられたテーブルスタイルを持つテーブルの一部の領域に適用される特別な書式設定を表すConditionalStyleクラスと、ConditionalStyleオブジェクトのコレクションを表すConditionalStyleCollectionも提供します。 このコレクションには、ConditionalStyleType列挙型の値ごとに1つの項目を表す項目の永続的なセットが含まれています。 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の対応するプロパティの表現を示しています。

table-style-aspose-words-java

テーブルスタイルから書式設定を取得し、直接書式設定として適用します

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);