書式設定を適用する

テーブルの各要素は、異なる書式設定で適用できます。 たとえば、テーブルの書式設定はテーブル全体に適用され、行の書式設定は特定の行のみに適用され、セルの書式設定は特定のセルのみに適用されます。

Aspose.Wordsは、テーブルの書式設定を取得して適用するための豊富なAPIを提供します。 TableRowFormat、およびCellFormatノードを使用して書式設定を設定できます。

この記事では、さまざまなテーブルノードに書式設定を適用する方法と、どのテーブル書式設定設定Aspose.Wordsがサポートするかについて説明します。

異なるノードに書式設定を適用する

このセクションでは、さまざまなテーブルノードに書式設定を適用する方法を見ていきます。

テーブルレベルの書式設定

テーブルに書式設定を適用するには、TablePreferredWidth、およびTableCollectionクラスを使用して、対応するTableノードで使用可能なプロパティを使用できます。

以下の図は、Microsoft WordのTable書式設定機能とAspose.Wordsの対応するプロパティの表現を示しています。

formattin-features-table-level-aspose-words-cpp

formatting-table-options-aspose-words-cpp

次のコード例は、テーブルにアウトラインの境界線を適用する方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders from the table.
table->ClearBorders();
// Set a green border around the table but not inside.
table->SetBorder(BorderType::Left, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true);
table->SetBorder(BorderType::Right, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true);
table->SetBorder(BorderType::Top, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true);
table->SetBorder(BorderType::Bottom, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true);
// Fill the cells with a light green solid color.
table->SetShading(TextureIndex::TextureSolid, System::Drawing::Color::get_LightGreen(), System::Drawing::Color::Empty);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.ApplyOutlineBorder.docx");

次のコード例は、すべての境界線を有効にしてテーブルを構築する方法を示しています(grid):

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Clear any existing borders from the table.
table->ClearBorders();
// Set a green border around and inside the table.
table->SetBorders(LineStyle::Single, 1.5, System::Drawing::Color::get_Green());
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithBorders.docx");

行レベルの書式設定

行レベル

以下の図は、Microsoft WordのRow書式設定機能とAspose.Wordsの対応するプロパティの表現を示しています。

formatting-row-level-aspose-words-cpp

次のコード例は、テーブルの行の書式を変更する方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Retrieve the first row in the table.
SharedPtr<Row> firstRow = table->get_FirstRow();
firstRow->get_RowFormat()->get_Borders()->set_LineStyle(LineStyle::None);
firstRow->get_RowFormat()->set_HeightRule(HeightRule::Auto);
firstRow->get_RowFormat()->set_AllowBreakAcrossPages(true);

セルレベルの書式設定

セルレベルの書式設定は、CellCellFormat、およびCellCollectionクラスによって制御されます。

以下の図は、Microsoft WordのCell書式設定機能とAspose.Wordsの対応するプロパティの表現を示しています。

formatting-cell-level-aspose-words-cpp

auto-formatting-cell-level-aspose-words-cpp

次のコード例は、テーブルセルの書式を変更する方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell();
firstCell->get_CellFormat()->set_Width(30);
firstCell->get_CellFormat()->set_Orientation(TextOrientation::Downward);
firstCell->get_CellFormat()->get_Shading()->set_ForegroundPatternColor(System::Drawing::Color::get_LightGreen());

次のコード例は、セルの内容の左/上/右/下に追加するスペースの量(ポイント単位)を設定する方法を示しています:

// 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);
builder->StartTable();
builder->InsertCell();
// Sets the amount of space (in points) to add to the left/top/right/bottom of the cell's contents.
builder->get_CellFormat()->SetPaddings(30, 50, 30, 50);
builder->Writeln(u"I'm a wonderful formatted cell.");
builder->EndRow();
builder->EndTable();
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CellPadding.docx");
view raw cell-padding.h hosted with ❤ by GitHub

行の高さの指定

行の高さを設定する最も簡単な方法は、DocumentBuilderを使用することです。 適切なRowFormatプロパティを使用して、デフォルトの高さ設定を設定するか、テーブル内の各行に異なる高さを適用できます。

Aspose.Wordsでは、テーブルの行の高さは次のように制御されます:

  • 行の高さプロパティ-Height
  • 指定された行の高さルールプロパティ–HeightRule

同時に、行ごとに異なる高さを設定することができます–これにより、テーブル設定を広く制御できます。

次のコード例は、単一のセルを含むテーブルを作成し、行の書式設定を適用する方法を示しています:

// 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();
SharedPtr<RowFormat> rowFormat = builder->get_RowFormat();
rowFormat->set_Height(100);
rowFormat->set_HeightRule(HeightRule::Exactly);
// These formatting properties are set on the table and are applied to all rows in the table.
table->set_LeftPadding(30);
table->set_RightPadding(30);
table->set_TopPadding(30);
table->set_BottomPadding(30);
builder->Writeln(u"I'm a wonderful formatted row.");
builder->EndRow();
builder->EndTable();
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.ApplyRowFormatting.docx");

テーブル幅とセル幅の指定

Microsoft Word文書内のテーブルには、テーブルと個々のセルのサイズを変更するいくつかの異なる方法があります。 これらのプロパティを使用すると、テーブルの外観と動作を大幅に制御できるため、Aspose.WordsはMicrosoft Wordのようにテーブルの動作をサポートします。

テーブル要素には、テーブル全体の幅と個々のセルの計算方法に影響を与える可能性のあるいくつかの異なるプロパティが存在することを知っておく:

  • テーブルの好ましい幅
  • 個々のセルの優先幅
  • テーブル上でのオートフィットを許可する

この記事では、さまざまなテーブル幅計算プロパティがどのように機能するか、およびテーブル幅計算を完全に制御する方法について詳しく説明します。 これは テーブルレイアウトが期待どおりに表示されないような場合に知っておくと特に便利です。

好みの幅を使用する方法

テーブルまたは個々のセルの希望の幅は、要素が収まるように努力するサイズであるpreferred widthプロパティを介して定義されます。 すなわち、好ましい幅は、テーブル全体または個々のセルに対して指定することができる。 状況によっては、この幅を正確に合わせることができない場合がありますが、実際の幅はほとんどの場合、この値に近くなります。

適切な優先幅の型と値は、PreferredWidthクラスのメソッドを使用して設定されます:

  • autoまたは"優先幅なし"を指定するAutoメソッド
  • パーセンテージ幅を指定するFromPercentメソッド
  • ポイント単位で幅を指定するFromPointsメソッド

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

formatting-table-properties-aspose-words-cpp

これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例は、下の図で見ることができます。

todo:image_alt_text

優先するテーブルまたはセルの幅を指定します

Aspose.Wordsでは、テーブルとセルの幅はTable.PreferredWidthプロパティとCellFormat.PreferredWidthプロパティを使用して設定され、PreferredWidthType列挙で使用できるオプションがあります:

  • Autoは、優先幅セットがないことに相当します
  • Percentは、ウィンドウまたはコンテナのサイズ内の使用可能なスペースに相対的に要素を適合させ、使用可能な幅が変更されたときに値を再計算します
  • Pointsは、ポイント単位で指定された幅の要素に対応します

Table.PreferredWidthプロパティを使用すると、コンテナに対して優先される幅が調整されます。page、text column、またはネストされたテーブルの場合は外部テーブルcellです。

次のコード例は、テーブルをページ幅の50%に自動適合させるように設定する方法を示しています:

// 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);
// Insert a table with a width that takes up half the page width.
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
table->set_PreferredWidth(PreferredWidth::FromPercent(50));
builder->Writeln(u"Cell #1");
builder->InsertCell();
builder->Writeln(u"Cell #2");
builder->InsertCell();
builder->Writeln(u"Cell #3");
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitToPageWidth.docx");

指定されたセルでCellFormat.PreferredWidthプロパティを使用すると、その優先幅が調整されます。

次のコード例は、さまざまな優先幅設定を設定する方法を示しています:

// 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);
// Insert a table row made up of three cells which have different preferred widths.
builder->StartTable();
// Insert an absolute sized cell.
builder->InsertCell();
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPoints(40));
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightYellow());
builder->Writeln(u"Cell at 40 points width");
// Insert a relative (percent) sized cell.
builder->InsertCell();
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(20));
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue());
builder->Writeln(u"Cell at 20% width");
// Insert a auto sized cell.
builder->InsertCell();
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::Auto());
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightGreen());
builder->Writeln(u"Cell automatically sized. The size of this cell is calculated from the table preferred width.");
builder->Writeln(u"In this case the cell will fill up the rest of the available space.");
doc->Save(ArtifactsDir + u"WorkingWithTables.PreferredWidthSettings.docx");

優先する幅のタイプと値を検索する

TypeプロパティとValueプロパティを使用して、目的のテーブルまたはセルの優先幅の詳細を見つけることができます。

次のコード例は、テーブルセルの優先幅の種類を取得する方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
table->set_AllowAutoFit(true);
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell();
PreferredWidthType type = firstCell->get_CellFormat()->get_PreferredWidth()->get_Type();
double value = firstCell->get_CellFormat()->get_PreferredWidth()->get_Value();

オートフィットを設定する方法

AllowAutoFitプロパティを使用すると、選択した基準に従ってテーブル内のセルを拡大および縮小できます。 たとえば、AutoFit to Windowオプションを使用してテーブルをページの幅に合わせ、AutoFit to Contentオプションを使用して各セルをその内容に応じて拡大または縮小できます。

既定では、Aspose.WordsはAutoFit to Windowを使用して新しいテーブルを挿入します。 テーブルのサイズは、使用可能なページ幅に応じて変更されます。 テーブルのサイズを変更するには、AutoFitメソッドを呼び出すことができます。 このメソッドは、テーブルに適用される自動調整のタイプを指定するAutoFitBehavior列挙体を受け入れます。

Autofitメソッドは、実際にはテーブルに異なるプロパティを同時に適用するショートカットであることを知っておくことが重要です。 これらは、実際にテーブルに観察された動作を与えるプロパティです。 各オートフィットオプションについて、これらのプロパティについて説明します。

次のコード例は、内容に応じて各セルを縮小または拡大するようにテーブルを設定する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
table->set_AllowAutoFit(true);

AutoFitテーブルからウィンドウへ

ウィンドウへの自動フィットがテーブルに適用されると、実際には次の操作が舞台裏で実行されます:

  1. Table.AllowAutoFitプロパティは、Table.PreferredWidthの値100を使用して、利用可能なコンテンツに合わせて列のサイズを自動的に変更することができます%
  2. CellFormat.PreferredWidthはすべてのテーブルセルから削除されます
  3. 列の幅は現在のテーブルの内容に対して再計算されます-最終的な結果は、使用可能な幅全体を占めるテーブルになります
  4. ユーザーがテキストを編集すると、テーブル内の列の幅が自動的に変更されます

次のコード例は、テーブルをページ幅に自動フィットさせる方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Autofit the first table to the page width.
table->AutoFit(AutoFitBehavior::AutoFitToWindow);
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToWindow.docx");

AutoFitテーブルからコンテンツへ

テーブルがコンテンツを自動入力されると、次の手順が実際に舞台裏で実行されます:

  1. Table.AllowAutoFitプロパティは、内容に応じて各セルのサイズを自動的に変更できるようになっています

  2. 優先テーブル幅はTable.PreferredWidthから削除され、CellFormat.PreferredWidthはテーブルセルごとに削除されます

  3. 最終的な結果は、ユーザーがテキストを編集するときにコンテンツに最適なサイズになるように、列幅とテーブル全体の幅が自動的にサイズ変更されるテー

次のコード例は、テーブルをそのコンテンツに自動フィットさせる方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
table->AutoFit(AutoFitBehavior::AutoFitToContents);
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToContents.docx");

テーブルでAutoFitを無効にし、固定列幅を使用する

テーブルの自動調整が無効になっていて、代わりに固定列幅が使用されている場合は、次の手順が実行されます:

  1. Table.AllowAutoFitプロパティが無効になっているため、列はコンテンツに拡大または縮小されません
  2. テーブル全体の優先幅はTable.PreferredWidthから削除され、CellFormat.PreferredWidthはすべてのテーブルセルから削除されます
  3. 最終的な結果は、列幅がCellFormat.Widthプロパティによって決定され、ユーザーがテキストを入力したとき、またはページのサイズが変更されたときに列が自動的にリ

次のコード例は、autofitを無効にし、指定したテーブルの固定幅を有効にする方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Disable autofitting on this table.
table->AutoFit(AutoFitBehavior::FixedColumnWidths);
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToFixedColumnWidths.docx");

セル幅を計算する際の優先順位

Aspose.Wordsは、ユーザーがCellFormatを含む複数のオブジェクトを介してテーブルまたはセルの幅を定義することができます–そのWidthプロパティは、主に以前のバージョンから残

CellFormat.Widthプロパティは、テーブル内に既に存在する他のwidthプロパティに応じて異なる動作をすることを知っておくことが重要です。

Aspose.Wordsは、セル幅の計算に次の順序を使用します:

ご注文 プロパティ 説明
AllowAutoFitが決定されます AutoFitが有効な場合:
-テーブルは、コンテンツに対応するために、優先幅を超えて成長することがあります–それは通常、優先幅以下に縮小しません
-CellFormat.Width値への変更は無視され、代わりにセルはその内容に適合します
PreferredWidthType の値は Points または Percent です CellFormat.Widthは無視されます
PreferredWidthTypeの値がAutoの場合 CellFormat.Widthの値がコピーされ、セルの優先幅(ポイント単位)になります。

セル間の間隔を許可する

Microsoft Wordの"セル間隔"オプションと同様に、テーブルセル間に追加のスペースを取得または設定できます。 これはAllowCellSpacingプロパティを使用して行うことができます。

これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例は、下の図で見ることができます。

formatting-spacing-between-cells-aspose-words-cpp

次のコード例は、セル間の間隔を設定する方法を示しています:

// 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");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
table->set_AllowCellSpacing(true);
table->set_CellSpacing(2);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.AllowCellSpacing.docx");

枠線と陰影を適用する

境界線と陰影は、Table.SetBorderTable.SetBordersTable.SetShadingを使用してテーブル全体に適用するか、CellFormat.BordersCellFormat.Shadingを使用して特定のセルにのみ適用できます。 さらに、行の境界線はRowFormat.Bordersを使用して設定できますが、この方法ではシェーディングを適用できません。

下の図は、Microsoft Wordの境界線と影の設定、およびAspose.Wordsの対応するプロパティを示しています。

formatting-border-line-aspose-words-cpp

formatting-cell-color-aspose-words-cpp

次のコード例は、異なる境界線と陰影を使用してテーブルとセルを書式設定する方法を示しています:

// 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();
// Set the borders for the entire table.
table->SetBorders(LineStyle::Single, 2.0, System::Drawing::Color::get_Black());
// Set the cell shading for this cell.
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_Red());
builder->Writeln(u"Cell #1");
builder->InsertCell();
// Specify a different cell shading for the second cell.
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_Green());
builder->Writeln(u"Cell #2");
builder->EndRow();
// Clear the cell formatting from previous operations.
builder->get_CellFormat()->ClearFormatting();
builder->InsertCell();
// Create larger borders for the first cell of this row. This will be different
// compared to the borders set for the table.
builder->get_CellFormat()->get_Borders()->get_Left()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Right()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Top()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Bottom()->set_LineWidth(4.0);
builder->Writeln(u"Cell #3");
builder->InsertCell();
builder->get_CellFormat()->ClearFormatting();
builder->Writeln(u"Cell #4");
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.FormatTableAndCellWithDifferentBorders.docx");