テーブルを配置する

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

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

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

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

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

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

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

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

テーブルのテキストの折り返しが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;
}
}

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

取得と同じように、同じ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;