放置一张桌子

有浮动表和内联表:

  • 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("..............................");
}
}

设置浮动工作台位置

就像getting一样,您可以使用相同的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());