テーブルの書式設定の適用

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

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

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

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

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

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

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

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

apply-formatting-to-table-level-aspose-words-java

apply-formatting-to-table-level-aspose-words-java

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

// 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");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Align the table to the center of the page.
table.setAlignment(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, Color.GREEN, true);
table.setBorder(BorderType.RIGHT, LineStyle.SINGLE, 1.5, Color.GREEN, true);
table.setBorder(BorderType.TOP, LineStyle.SINGLE, 1.5, Color.GREEN, true);
table.setBorder(BorderType.BOTTOM, LineStyle.SINGLE, 1.5, Color.GREEN, true);
// Fill the cells with a light green solid color.
table.setShading(TextureIndex.TEXTURE_SOLID, Color.lightGray, new Color(0, true));
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.ApplyOutlineBorder.docx");

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

// 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");
Table table = (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, Color.GREEN);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.BuildTableWithBorders.docx");

行レベルの書式設定

行レベルの書式設定は、RowRowFormat、およびRowCollectionクラスを使用して制御できます。

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

apply-formatting-to-row-level-aspose-words-java

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

// 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");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Retrieve the first row in the table.
Row firstRow = table.getFirstRow();
firstRow.getRowFormat().getBorders().setLineStyle(LineStyle.NONE);
firstRow.getRowFormat().setHeightRule(HeightRule.AUTO);
firstRow.getRowFormat().setAllowBreakAcrossPages(true);

セルレベルの書式設定

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

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

apply-formatting-to-cell-level-aspose-words-java

apply-auto-formatting-to-row-level-aspose-words-java

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

// 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");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
Cell firstCell = table.getFirstRow().getFirstCell();
firstCell.getCellFormat().setWidth(30.0);
firstCell.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
firstCell.getCellFormat().getShading().setForegroundPatternColor(Color.GREEN);

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

// 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);
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.getCellFormat().setPaddings(30.0, 50.0, 30.0, 50.0);
builder.writeln("I'm a wonderful formatted cell.");
builder.endRow();
builder.endTable();
doc.save(getArtifactsDir() + "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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
RowFormat rowFormat = builder.getRowFormat();
rowFormat.setHeight(100.0);
rowFormat.setHeightRule(HeightRule.EXACTLY);
// These formatting properties are set on the table and are applied to all rows in the table.
table.setLeftPadding(30.0);
table.setRightPadding(30.0);
table.setTopPadding(30.0);
table.setBottomPadding(30.0);
builder.writeln("I'm a wonderful formatted row.");
builder.endRow();
builder.endTable();
doc.save(getArtifactsDir() + "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-java

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

table-applied-options-java

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

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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table with a width that takes up half the page width.
Table table = builder.startTable();
builder.insertCell();
table.setPreferredWidth(PreferredWidth.fromPercent(50.0));
builder.writeln("Cell #1");
builder.insertCell();
builder.writeln("Cell #2");
builder.insertCell();
builder.writeln("Cell #3");
doc.save(getArtifactsDir() + "WorkingWithTables.AutoFitToPageWidth.docx");

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

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

// 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);
// Insert a table row made up of three cells which have different preferred widths.
builder.startTable();
// Insert an absolute sized cell.
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.writeln("Cell at 40 points width");
// Insert a relative (percent) sized cell.
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
builder.writeln("Cell at 20% width");
// Insert a auto sized cell.
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln(
"Cell automatically sized. The size of this cell is calculated from the table preferred width.");
builder.writeln("In this case the cell will fill up the rest of the available space.");
doc.save(getArtifactsDir() + "WorkingWithTables.PreferredWidthSettings.docx");

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

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

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

// 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");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
table.setAllowAutoFit(true);
Cell firstCell = table.getFirstRow().getFirstCell();
/*PreferredWidthType*/int type = firstCell.getCellFormat().getPreferredWidth().getType();
double value = firstCell.getCellFormat().getPreferredWidth().getValue();

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

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-Java.git.
table.setAllowAutoFit(true);

次の表を使用して、さまざまな自動フィット設定をデモとして適用します。

apply-different-autofit-settings-to-a-table-aspose-words-java

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-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Autofit the first table to the page width.
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
doc.save(getArtifactsDir() + "WorkingWithTables.AutoFitTableToWindow.docx");

これらのオプションが上の表にどのように適用されるかの例は、下の図で見ることができます。

autofit-table-aspose-words-java

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-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
doc.save(getArtifactsDir() + "WorkingWithTables.AutoFitTableToContents.docx");

これらのオプションが上の表にどのように適用されるかの例は、下の図で見ることができます。

resize-column-autofit-settings-aspose-words-java

テーブルで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-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Disable autofitting on this table.
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
doc.save(getArtifactsDir() + "WorkingWithTables.AutoFitTableToFixedColumnWidths.docx");

これらのオプションが上の表にどのように適用されるかの例は、下の図で見ることができます。

disable-autofit-settings-to-a-table-aspose-words-java

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

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

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

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

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

セル間の間隔を許可する

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

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

formatting-spacing-between-cells-aspose-words-java

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

// 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");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
table.setAllowCellSpacing(true);
table.setCellSpacing(2.0);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.AllowCellSpacing.docx");

枠線と陰影を適用する

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

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

apply-borders-shading-aspose-words-java-1

apply-borders-shading-aspose-words-java-2

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

// 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();
// Set the borders for the entire table.
table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK);
// Set the cell shading for this cell.
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED);
builder.writeln("Cell #1");
builder.insertCell();
// Specify a different cell shading for the second cell.
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.writeln("Cell #2");
builder.endRow();
// Clear the cell formatting from previous operations.
builder.getCellFormat().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.getCellFormat().getBorders().getLeft().setLineWidth(4.0);
builder.getCellFormat().getBorders().getRight().setLineWidth(4.0);
builder.getCellFormat().getBorders().getTop().setLineWidth(4.0);
builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0);
builder.writeln("Cell #3");
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.writeln("Cell #4");
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.FormatTableAndCellWithDifferentBorders.docx");