ตำแหน่งตาราง

มีตารางลอยและตารางแบบอินไลน์:

  • 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 คุณสามารถจัดตำแหน่งแนวนอนและแนวตั้งของตารางได้โดยใช้คุณสมบัติ 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-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());
}

รับตำแหน่งตารางลอย

ตำแหน่งของตารางลอยจะถูกกำหนดโดยใช้คุณสมบัติต่อไปนี้:

  • HorizontalAnchor–วัตถุสำหรับการคำนวณตำแหน่งแนวนอนของตารางลอย
  • VerticalAnchor–วัตถุสำหรับการคำนวณตำแหน่งในแนวตั้งของตารางลอย
  • AbsoluteHorizontalDistance–ตำแหน่งตารางลอยแนวนอนสัมบูรณ์
  • AbsoluteVerticalDistance–ตำแหน่งตารางลอยแนวตั้งสัมบูรณ์
  • AllowOverlap-ตัวเลือกเพื่อเปิด/ปิดการทับซ้อนกับวัตถุอื่นๆลอย
  • RelativeHorizontalAlignment–ตารางลอยสัมพันธ์แนวนอน.
  • RelativeVerticalAlignment–ตารางลอยแนวตั้งญาติ.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรับตำแหน่งของตารางลอย:

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

ตั้งตำแหน่งตารางลอยตัว

เช่นเดียวกับการได้รับ,คุณสามารถตั้งค่าตำแหน่งของตารางลอยโดยใช้เดียวกัน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นอกจากนี้ยังมีโอกาสที่จะหาระยะห่างระหว่างตารางและข้อความโดยรอบ:

  • DistanceTop–ค่าของระยะทางจากด้านบน
  • DistanceBottom-ค่าของระยะทางของการรับรู้
  • DistanceRight–ค่าระยะทางทางด้านขวา
  • DistanceLeft–ค่าระยะทางทางซ้าย

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการรับระยะห่างระหว่างตารางและข้อความโดยรอบ:

// 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());