書式設定を適用する
テーブルの各要素は、異なる書式設定で適用できます。 たとえば、テーブルの書式設定はテーブル全体に適用され、行の書式設定は特定の行のみに適用され、セルの書式設定は特定のセルのみに適用されます。
Aspose.Wordsは、テーブルの書式設定を取得して適用するための豊富なAPIを提供します。 Table、RowFormat、およびCellFormatノードを使用して書式設定を設定できます。
この記事では、さまざまなテーブルノードに書式設定を適用する方法と、どのテーブル書式設定設定Aspose.Wordsがサポートするかについて説明します。
異なるノードに書式設定を適用する
このセクションでは、さまざまなテーブルノードに書式設定を適用する方法を見ていきます。
テーブルレベルの書式設定
テーブルに書式設定を適用するには、Table、PreferredWidth、およびTableCollectionクラスを使用して、対応するTableノードで使用可能なプロパティを使用できます。
以下の図は、Microsoft WordのTable書式設定機能とAspose.Wordsの対応するプロパティの表現を示しています。
次のコード例は、テーブルにアウトラインの境界線を適用する方法を示しています:
// 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の対応するプロパティの表現を示しています。
次のコード例は、テーブルの行の書式を変更する方法を示しています:
// 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); |
セルレベルの書式設定
セルレベルの書式設定は、Cell、CellFormat、およびCellCollectionクラスによって制御されます。
ACellはaRowの子ノードのみであることに注意してください。 同時に、書式設定を適用できるように、Cellに少なくとも一つのParagraphがなければなりません。
Paragraphに加えて、TableをCellに挿入することもできます。
以下の図は、Microsoft WordのCell書式設定機能とAspose.Wordsの対応するプロパティの表現を示しています。
次のコード例は、テーブルセルの書式を変更する方法を示しています:
// 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"); |
行の高さの指定
行の高さを設定する最も簡単な方法は、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のようにテーブルの動作をサポートします。
テーブル要素には、テーブル全体の幅と個々のセルの計算方法に影響を与える可能性のあるいくつかの異なるプロパティが存在することを知っておく:
- テーブルの好ましい幅
- 個々のセルの優先幅
- テーブル上でのオートフィットを許可する
この記事では、さまざまなテーブル幅計算プロパティがどのように機能するか、およびテーブル幅計算を完全に制御する方法について詳しく説明します。 これは テーブルレイアウトが期待どおりに表示されないような場合に知っておくと特に便利です。
ほとんどの場合、テーブルの幅よりも優先セルが推奨されます。 推奨されるセルの幅は、Aspose.Wordsモデルと同様にDOCX形式の仕様に沿っています。
セルの幅は、実際にはDOCX形式の計算値です。 実際のセルの幅は、多くのものに依存する可能性があります。 たとえば、ページの余白や優先テーブルの幅を変更すると、実際のセルの幅に影響を与える可能性があります。
推奨されるセル幅は、ドキュメントに格納されているセルプロパティです。 これは何にも依存せず、セルのテーブルやその他のプロパティを変更しても変更されません。
好みの幅を使用する方法
テーブルまたは個々のセルの希望の幅は、要素が収まるように努力するサイズであるpreferred widthプロパティを介して定義されます。 すなわち、好ましい幅は、テーブル全体または個々のセルに対して指定することができる。 状況によっては、この幅を正確に合わせることができない場合がありますが、実際の幅はほとんどの場合、この値に近くなります。
適切な優先幅の型と値は、PreferredWidthクラスのメソッドを使用して設定されます:
- autoまたは"優先幅なし"を指定するAutoメソッド
- パーセンテージ幅を指定するFromPercentメソッド
- ポイント単位で幅を指定するFromPointsメソッド
下の写真は、Microsoft Wordのpreferred width setting featuresとAspose.Wordsの対応するプロパティの表現を示しています。
これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例は、下の図で見ることができます。
優先するテーブルまたはセルの幅を指定します
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テーブルからウィンドウへ
ウィンドウへの自動フィットがテーブルに適用されると、実際には次の操作が舞台裏で実行されます:
- Table.AllowAutoFitプロパティは、Table.PreferredWidthの値100を使用して、利用可能なコンテンツに合わせて列のサイズを自動的に変更することができます%
- CellFormat.PreferredWidthはすべてのテーブルセルから削除されます
これは、各セルの優先幅が現在のサイズと内容に基づいて適切な値に設定されるMicrosoft Wordの動作とはわずかに異なることに注意してください。 Aspose.Wordsは優先幅を更新しないので、代わりにクリアされます。
- 列の幅は現在のテーブルの内容に対して再計算されます-最終的な結果は、使用可能な幅全体を占めるテーブルになります
- ユーザーがテキストを編集すると、テーブル内の列の幅が自動的に変更されます
次のコード例は、テーブルをページ幅に自動フィットさせる方法を示しています:
// 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テーブルからコンテンツへ
テーブルがコンテンツを自動入力されると、次の手順が実際に舞台裏で実行されます:
-
Table.AllowAutoFitプロパティは、内容に応じて各セルのサイズを自動的に変更できるようになっています
-
優先テーブル幅はTable.PreferredWidthから削除され、CellFormat.PreferredWidthはテーブルセルごとに削除されます
この自動調整オプションは、Microsoft Wordのように、セルから優先幅を削除することに注意してください。 列のサイズを維持し、内容に合わせて列を増減する場合は、autofitショートカットを使用するのではなく、Table.AllowAutoFitプロパティを独自にTrueに設定する必要があります。 -
最終的な結果は、ユーザーがテキストを編集するときにコンテンツに最適なサイズになるように、列幅とテーブル全体の幅が自動的にサイズ変更されるテー
次のコード例は、テーブルをそのコンテンツに自動フィットさせる方法を示しています:
// 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を無効にし、固定列幅を使用する
テーブルの自動調整が無効になっていて、代わりに固定列幅が使用されている場合は、次の手順が実行されます:
- Table.AllowAutoFitプロパティが無効になっているため、列はコンテンツに拡大または縮小されません
- テーブル全体の優先幅はTable.PreferredWidthから削除され、CellFormat.PreferredWidthはすべてのテーブルセルから削除されます
- 最終的な結果は、列幅が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プロパティを使用して行うことができます。
これらのオプションがドキュメント内の実際のテーブルにどのように適用されるかの例は、下の図で見ることができます。

次のコード例は、セル間の間隔を設定する方法を示しています:
// 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.SetBorder、Table.SetBorders、Table.SetShadingを使用してテーブル全体に適用するか、CellFormat.BordersとCellFormat.Shadingを使用して特定のセルにのみ適用できます。 さらに、行の境界線はRowFormat.Bordersを使用して設定できますが、この方法ではシェーディングを適用できません。
下の図は、Microsoft Wordの境界線と影の設定、およびAspose.Wordsの対応するプロパティを示しています。
次のコード例は、異なる境界線と陰影を使用してテーブルとセルを書式設定する方法を示しています:
// 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"); |