ตำแหน่งตาราง
มีตารางลอยและตารางแบบอินไลน์:
- 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 คุณสามารถจัดตำแหน่งแนวนอนและแนวตั้งของตารางได้โดยใช้คุณสมบัติ RelativeHorizontalAlignment และ RelativeVerticalAlignment.
ด้วย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; | |
} |
รับตำแหน่งตารางลอย
ตำแหน่งของตารางลอยจะถูกกำหนดโดยใช้คุณสมบัติต่อไปนี้:
- HorizontalAnchor–วัตถุสำหรับการคำนวณตำแหน่งแนวนอนของตารางลอย
- VerticalAnchor–วัตถุสำหรับการคำนวณตำแหน่งในแนวตั้งของตารางลอย
- AbsoluteHorizontalDistance–ตำแหน่งตารางลอยแนวนอนสัมบูรณ์
- AbsoluteVerticalDistance–ตำแหน่งตารางลอยแนวตั้งสัมบูรณ์
- AllowOverlap-ตัวเลือกเพื่อเปิด/ปิดการทับซ้อนกับวัตถุอื่นๆลอย
- RelativeHorizontalAlignment–ตารางลอยสัมพันธ์แนวนอน.
- RelativeVerticalAlignment–ตารางลอยแนวตั้งญาติ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรับตำแหน่งของตารางลอย:
// 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นอกจากนี้ยังมีโอกาสที่จะหาระยะห่างระหว่างตารางและข้อความโดยรอบ:
- DistanceTop–ค่าของระยะทางจากด้านบน
- DistanceBottom-ค่าของระยะทางของการรับรู้
- DistanceRight–ค่าระยะทางทางด้านขวา
- DistanceLeft–ค่าระยะทางทางซ้าย
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรับระยะห่างระหว่างตารางและข้อความโดยรอบ:
// 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; |