テーブルを配置する

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

インラインテーブルはテキストと同じレイヤーに配置され、表の上下のみを囲むテキストの流れの中に配置されます。インライン テーブルは、配置した段落の間に常に表示されます。

  • フローティングテーブル はテキストの上に重ねられ、段落に対する表の位置は表のアンカーによって決まります。このため、ドキュメント内のフローティング テーブルの位置は、垂直および水平の位置設定の影響を受けます。

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

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

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Align the table to the center of the page.
table.alignment = aw.tables.TableAlignment.CENTER

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

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

他のタイプのテキストの折り返し では、Alignment プロパティを使用してインライン テーブルの配置を取得できます。

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
if table.text_wrapping == aw.tables.TextWrapping.AROUND:
print(table.relative_horizontal_alignment)
print(table.relative_vertical_alignment)
else:
print(table.alignment)

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Table wrapped by text.docx")
for table in doc.first_section.body.tables:
table = table.as_table()
# If the table is floating type, then print its positioning properties.
if table.text_wrapping == aw.tables.TextWrapping.AROUND:
print(table.horizontal_anchor)
print(table.vertical_anchor)
print(table.absolute_horizontal_distance)
print(table.absolute_vertical_distance)
print(table.allow_overlap)
print(table.absolute_horizontal_distance)
print(table.relative_vertical_alignment)
print("..............................")

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

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

配置と水平距離と垂直距離は組み合わせられたプロパティであり、一方が他方をリセットできることを知っておくことが重要です。たとえば、RelativeHorizontalAlignment を設定すると AbsoluteHorizontalDistance がデフォルト値にリセットされ、その逆も同様です。縦方向の配置の true も同様です。

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Table wrapped by text.docx")
table = doc.first_section.body.tables[0]
table.absolute_horizontal_distance = 10
table.relative_vertical_alignment = aw.drawing.VerticalAlignment.CENTER
doc.save(ARTIFACTS_DIR + "WorkingWithTables.floating_table_position.docx")

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

Aspose.Words は、表と周囲のテキストの間の距離を調べる機会も提供します。

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
print("\nGet distance between table left, right, bottom, top and the surrounding text.")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
print(table.distance_top)
print(table.distance_bottom)
print(table.distance_right)
print(table.distance_left)