テーブルを配置する

フローティングテーブルとインラインテーブルがあります:

  • Inline tablesはテキストと同じレイヤー上に配置され、上下のテーブルのみを囲むテキストのフロー内に配置されます。 インラインテーブルは、それらを配置した段落の間に常に表示されます。
  • Floating tablesはテキストの上に階層化され、段落に対するテーブルの位置はテーブルアンカーによって決まります。 このため、ドキュメント内のフローティングテーブルの位置は、垂直方向および水平方向の位置設定の影響を受けます。

場合によっては、特定の方法で文書内のテーブルを配置する必要があります。 これを行うには、配置ツールを使用して、テーブルと周囲のテキストの間にインデントを設定する必要があります。

この記事では、どのオプションAspose.Wordsが位置決めに提供するかについて説明します。

インラインテーブル位置の指定

インラインテーブルの位置は、Aspose.WordsAPIとAlignmentプロパティを使用して設定できます。 したがって、ドキュメントページに対するテーブルの配置を調整できます。

次のコード例は、インラインテーブルの位置を設定する方法を示しています:

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

フローティングテーブルの配置を取得

テーブルのテキストの折り返しがAroundに設定されている場合、RelativeHorizontalAlignmentRelativeVerticalAlignmentプロパティを使用してテーブルの水平方向と垂直方向の配置を取得できます。

other types of text wrappingを使用すると、Alignmentプロパティを使用してインラインテーブルの配置を取得できます。

次のコード例は、テーブルの配置を取得する方法を示しています:

// 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);
if (table.getTextWrapping() == TextWrapping.AROUND)
{
System.out.println(table.getRelativeHorizontalAlignment());
System.out.println(table.getRelativeVerticalAlignment());
}
else
{
System.out.println(table.getAlignment());
}

フローティングテーブルの位置を取得

フローティングテーブルの位置は、次のプロパティを使用して決定されます:

次のコード例は、フローティングテーブルの位置を取得する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Table wrapped by text.docx");
for (Table table : doc.getFirstSection().getBody().getTables())
{
// If the table is floating type, then print its positioning properties.
if (table.getTextWrapping() == TextWrapping.AROUND)
{
System.out.println(table.getHorizontalAnchor());
System.out.println(table.getVerticalAnchor());
System.out.println(table.getAbsoluteHorizontalDistance());
System.out.println(table.getAbsoluteVerticalDistance());
System.out.println(table.getAllowOverlap());
System.out.println(table.getAbsoluteHorizontalDistance());
System.out.println(table.getRelativeVerticalAlignment());
System.out.println("..............................");
}
}

フローティングテーブルの位置を設定

取得と同じように、同じAspose.WordsAPIを使用してフローティングテーブルの位置を設定できます。

アライメントと水平方向と垂直方向の距離は結合されたプロパティであり、一方が他方をリセットできることを知っておくことが重要です。 たとえば、RelativeHorizontalAlignmentを設定するとAbsoluteHorizontalDistanceがデフォルト値にリセットされ、その逆も同様です。 垂直方向の配置についても同じことが言えます。

次のコード例は、フローティングテーブルの位置を設定する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Table wrapped by text.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.setAbsoluteHorizontalDistance(10.0);
table.setRelativeVerticalAlignment(VerticalAlignment.CENTER);
doc.save(getArtifactsDir() + "WorkingWithTables.FloatingTablePosition.docx");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Table wrapped by text.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
table.setHorizontalAnchor(RelativeHorizontalPosition.COLUMN);
table.setVerticalAnchor(RelativeVerticalPosition.PAGE);
doc.save(getArtifactsDir() + "WorkingWithTables.RelativeHorizontalOrVerticalPosition.docx");

テーブルと周囲のテキスト間の距離を取得する

Aspose.Wordsはまた、表と周囲のテキスト間の距離を見つける機会を提供します:

次のコード例は、テーブルとその周囲のテキストとの間の距離を取得する方法を示しています:

// 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");
System.out.println("\nGet distance between table left, right, bottom, top and the surrounding text.");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
System.out.println(table.getDistanceTop());
System.out.println(table.getDistanceBottom());
System.out.println(table.getDistanceRight());
System.out.println(table.getDistanceLeft());