放置桌子
有浮动表和内联表:
- 内联表 与文本放置在同一层,并放置在仅围绕上方和下方表格的文本流中。内联表格将始终出现在放置它们的段落之间。
- 浮动桌子 分层在文本之上,表格相对于段落的位置由表格锚确定。因此,浮动表格在文档中的位置会受到垂直和水平定位设置的影响。
有时您需要以某种方式在文档中定位表格。为此,您需要使用对齐工具并设置表格和周围文本之间的缩进。
在本文中,我们将讨论 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) |
获取浮动表位置
浮动表的位置是使用以下属性确定的:
- HorizontalAnchor – 用于计算浮动桌子水平位置的对象
- VerticalAnchor – 用于计算浮动桌子垂直位置的对象
- AbsoluteHorizontalDistance – 绝对水平浮动工作台位置
- AbsoluteVerticalDistance – 绝对垂直浮动工作台位置
- AllowOverlap – 启用/禁用与其他浮动对象重叠的选项
- RelativeHorizontalAlignment – 浮动表相对水平对齐。
- RelativeVerticalAlignment – 浮动表相对垂直对齐。
以下代码示例展示了如何获取浮动表的位置:
# 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 还提供了找出表格和周围文本之间距离的机会:
- DistanceTop – 距上方的距离值
- DistanceBottom–感知距离值
- DistanceRight – 右侧距离值
- DistanceLeft – 左侧距离值
以下代码示例演示如何获取表格与其周围文本之间的距离:
# 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) |