放置一张桌子

有浮动表和内联表:

  • 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-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Align the table to the center of the page.
table->set_Alignment(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-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
if (table->get_TextWrapping() == TextWrapping::Around)
{
std::cout << System::EnumGetName(table->get_RelativeHorizontalAlignment()) << std::endl;
std::cout << System::EnumGetName(table->get_RelativeVerticalAlignment()) << std::endl;
}
else
{
std::cout << System::EnumGetName(table->get_Alignment()) << std::endl;
}

获取浮动表位置

浮动表的位置使用以下属性确定:

下面的代码示例演示如何获取浮动表的位置:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
for (const auto& table : System::IterateOver<Table>(doc->get_FirstSection()->get_Body()->get_Tables()))
{
// If the table is floating type, then print its positioning properties.
if (table->get_TextWrapping() == TextWrapping::Around)
{
std::cout << System::EnumGetName(table->get_HorizontalAnchor()) << std::endl;
std::cout << System::EnumGetName(table->get_VerticalAnchor()) << std::endl;
std::cout << table->get_AbsoluteHorizontalDistance() << std::endl;
std::cout << table->get_AbsoluteVerticalDistance() << std::endl;
std::cout << System::Convert::ToString(table->get_AllowOverlap()) << std::endl;
std::cout << table->get_AbsoluteHorizontalDistance() << std::endl;
std::cout << System::EnumGetName(table->get_RelativeVerticalAlignment()) << std::endl;
std::cout << ".............................." << std::endl;
}
}

设置浮动工作台位置

就像getting一样,您可以使用相同的Aspose.WordsAPI设置浮动表的位置。

重要的是要知道对齐和水平和垂直距离是组合属性,一个可以重置另一个。 例如,设置RelativeHorizontalAlignment会将AbsoluteHorizontalDistance重置为其默认值,反之亦然。 垂直布置也是如此。

下面的代码示例演示如何设置浮动表的位置:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
table->set_AbsoluteHorizontalDistance(10);
table->set_RelativeVerticalAlignment(VerticalAlignment::Center);
doc->Save(ArtifactsDir + u"WorkingWithTables.FloatingTablePosition.docx");

获取表格与周围文本之间的距离

Aspose.Words还提供了一个机会,找出表和周围文本之间的距离:

下面的代码示例演示如何获取表与其周围文本之间的距离:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
std::cout << "\nGet distance between table left, right, bottom, top and the surrounding text." << std::endl;
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
std::cout << table->get_DistanceTop() << std::endl;
std::cout << table->get_DistanceBottom() << std::endl;
std::cout << table->get_DistanceRight() << std::endl;
std::cout << table->get_DistanceLeft() << std::endl;