テーブルスタイルの適用

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

Aspose.Wordsは、テーブルスタイルをテーブルに適用し、任意のテーブルスタイルのプロパティを読み取ることもサポートします。 テーブルスタイルは、ロード中および保存中に次の方法で保持されます:

  • DOCXおよびWordML形式のテーブルスタイルは、これらの形式への読み込みおよび保存時に保持されます
  • テーブルスタイルは、DOC形式でロードおよび保存するときに保持されます(ただし、他の形式には保持されません)。
  • 他の形式、レンダリング、印刷にエクスポートする場合、テーブルスタイルはテーブル内の直接書式設定に展開されるため、すべての書式設定が保持されま

テーブルスタイルを作成する

ユーザーは新しいスタイルを作成し、それをスタイルコレクションに追加できます。 Addメソッドは、新しいテーブルスタイルを作成するために使用されます。

次のコード例は、新しいユーザー定義テーブルスタイルを作成する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_Borders()->set_LineStyle(LineStyle::Double);
tableStyle->get_Borders()->set_LineWidth(1);
tableStyle->set_LeftPadding(18);
tableStyle->set_RightPadding(18);
tableStyle->set_TopPadding(12);
tableStyle->set_BottomPadding(12);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

既存のテーブルスタイルをコピーする

必要に応じて、AddCopyメソッドを使用して、特定のドキュメントに既に存在するテーブルスタイルをスタイルコレクションにコピーできます。

このコピーでは、リンクされたスタイルもコピーされることを知っておくことが重要です。

次のコード例は、ある文書から別の文書にスタイルをインポートする方法を示しています:

例を示します。

既存のテーブルスタイルを適用する

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-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_GreenYellow());
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_Texture(TextureIndex::TextureNone);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

また、最初の列、最後の列、縞模様の行など、スタイルを適用するテーブルパーツを選択することもできます。 これらはTableStyleOptions列挙体にリストされ、StyleOptionsプロパティを介して適用されます。 TableStyleOptions列挙では、これらの機能をビット単位で組み合わせることができます。

次のコード例は、テーブルスタイルを適用して新しいテーブルを作成する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<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->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Apply which features should be formatted by the style.
table->set_StyleOptions(TableStyleOptions::FirstColumn | TableStyleOptions::RowBands | TableStyleOptions::FirstRow);
table->AutoFit(AutoFitBehavior::AutoFitToContents);
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

下の写真は、Microsoft WordのTable StylesとAspose.Wordsの対応するプロパティの表現を示しています。

formatting-table-style-aspose-words-cpp

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

Aspose.Wordsはまた、テーブルスタイルで見つかった書式設定を取得し、直接書式設定としてテーブルの行とセルに展開するExpandTableStylesToDirectFormattingメソッドを提供します。 書式設定をテーブルスタイルとセルスタイルと組み合わせてみてください。

次のコード例は、スタイルからテーブルの行とセルに直接書式設定として書式を展開する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Get the first cell of the first table in the document.
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell();
// First print the color of the cell shading.
// This should be empty as the current shading is stored in the table style.
System::Drawing::Color cellShadingBefore = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading before style expansion: ") + cellShadingBefore) << std::endl;
doc->ExpandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles.
// A blue background pattern color should have been applied from the table style.
System::Drawing::Color cellShadingAfter = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading after style expansion: ") + cellShadingAfter) << std::endl;