ใช้การจัดรูปแบบ
องค์ประกอบของตารางแต่ละคนสามารถนำไปใช้กับการจัดรูปแบบที่แตกต่างกัน ตัวอย่างเช่นการจัดรูปแบบตารางจะถูกนำไปใช้กับตารางทั้งหมดการจัดรูปแบบแถวกับแถ.
Aspose.Wordsให้รวยAPIเพื่อดึงและนำไปใช้การจัดรูปแบบกับตาราง คุณสามารถใช้โหนดTable,RowFormatและCellFormatเพื่อตั้งค่าการจัดรูปแบบ.
ในบทความนี้เราจะพูดถึงวิธีใช้การจัดรูปแบบกับโหนดตารางที่แตกต่างกันและการตั้งค่าการจัดรูปแบบตารางAspose.Wordsสนับสนุน.
ใช้การจัดรูปแบบกับโหนดที่แตกต่างกัน
ในส่วนนี้เราจะดูที่การใช้การจัดรูปแบบไปยังโหนดตารางต่างๆ.
การจัดรูปแบบระดับตาราง
หากต้องการจัดรูปแบบตาราง คุณสามารถใช้คุณสมบัติที่มีอยู่บนโหนด Table ที่สอดคล้องกันโดยใช้คลาส Table, PreferredWidth และ TableCollection.
รูปภาพด้านล่างแสดงคุณลักษณะการจัดรูปแบบTableในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันใน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"); | |
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); | |
// Clear any existing borders from the table. | |
table->ClearBorders(); | |
// Set a green border around the table but not inside. | |
table->SetBorder(BorderType::Left, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true); | |
table->SetBorder(BorderType::Right, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true); | |
table->SetBorder(BorderType::Top, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true); | |
table->SetBorder(BorderType::Bottom, LineStyle::Single, 1.5, System::Drawing::Color::get_Green(), true); | |
// Fill the cells with a light green solid color. | |
table->SetShading(TextureIndex::TextureSolid, System::Drawing::Color::get_LightGreen(), System::Drawing::Color::Empty); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.ApplyOutlineBorder.docx"); |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตารางที่มีเส้นขอบทั้งหมดที่เปิดใช้งาน(ตาราง):
// 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)); | |
// Clear any existing borders from the table. | |
table->ClearBorders(); | |
// Set a green border around and inside the table. | |
table->SetBorders(LineStyle::Single, 1.5, System::Drawing::Color::get_Green()); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithBorders.docx"); |
การจัดรูปแบบระดับแถว
ระดับแถว
รูปภาพด้านล่างแสดงคุณลักษณะการจัดรูปแบบRowในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันใน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"); | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
// Retrieve the first row in the table. | |
SharedPtr<Row> firstRow = table->get_FirstRow(); | |
firstRow->get_RowFormat()->get_Borders()->set_LineStyle(LineStyle::None); | |
firstRow->get_RowFormat()->set_HeightRule(HeightRule::Auto); | |
firstRow->get_RowFormat()->set_AllowBreakAcrossPages(true); |
การจัดรูปแบบระดับเซลล์
การจัดรูปแบบระดับเซลล์จะถูกควบคุมโดยCell,CellFormatและCellCollectionคลาส.
โปรดทราบว่าCellสามารถเป็นโหนดลูกของRowเท่านั้น ในเวลาเดียวกันต้องมีอย่างน้อยหนึ่งParagraphในCellเพื่อให้สามารถใช้การจัดรูปแบบได้.
นอกเหนือจากParagraphคุณยังสามารถแทรกTableลงในCell.
รูปภาพด้านล่างแสดงคุณลักษณะการจัดรูปแบบCellในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันใน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"); | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell(); | |
firstCell->get_CellFormat()->set_Width(30); | |
firstCell->get_CellFormat()->set_Orientation(TextOrientation::Downward); | |
firstCell->get_CellFormat()->get_Shading()->set_ForegroundPatternColor(System::Drawing::Color::get_LightGreen()); |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าจำนวนของพื้นที่(ในจุด)เพื่อเพิ่มไปทางซ้าย/บน/ขวา/ล่างข:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->StartTable(); | |
builder->InsertCell(); | |
// Sets the amount of space (in points) to add to the left/top/right/bottom of the cell's contents. | |
builder->get_CellFormat()->SetPaddings(30, 50, 30, 50); | |
builder->Writeln(u"I'm a wonderful formatted cell."); | |
builder->EndRow(); | |
builder->EndTable(); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CellPadding.docx"); |
ระบุความสูงแถว
วิธีที่ง่ายที่สุดในการตั้งค่าความสูงของแถวคือใช้DocumentBuilder การใช้คุณสมบัติที่เหมาะสมRowFormatคุณสามารถตั้งค่าความสูงเริ่มต้นหรือใช้ความสูงที่แตกต่างกัน.
ในAspose.Wordsความสูงของแถวตารางจะถูกควบคุมโดย:
- คุณสมบัติความสูงของแถว-Height
- คุณสมบัติกฎความสูงของแถวที่กำหนด-HeightRule
ในเวลาเดียวกันความสูงที่แตกต่างกันสามารถตั้งค่าสำหรับแต่ละแถว-นี้ช่วยให้คุณสามารถ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตารางที่ประกอบด้วยเซลล์เดียวและใช้การจัดรูปแ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
SharedPtr<RowFormat> rowFormat = builder->get_RowFormat(); | |
rowFormat->set_Height(100); | |
rowFormat->set_HeightRule(HeightRule::Exactly); | |
// These formatting properties are set on the table and are applied to all rows in the table. | |
table->set_LeftPadding(30); | |
table->set_RightPadding(30); | |
table->set_TopPadding(30); | |
table->set_BottomPadding(30); | |
builder->Writeln(u"I'm a wonderful formatted row."); | |
builder->EndRow(); | |
builder->EndTable(); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.ApplyRowFormatting.docx"); |
การระบุตารางและความกว้างของเซลล์
ตารางในเอกสารMicrosoft Wordมีหลายวิธีในการปรับขนาดตารางและแต่ละเซลล์ คุณสมบัติเหล่านี้อนุญาตให้ควบคุมลักษณะและพฤติกรรมของตารางดังนั้นAspose.Wordsสนับสนุนพฤติกรรมของตารางเช่นเดียวกับในMicrosoft Word.
มันเป็นสิ่งสำคัญที่จะรู้ว่าองค์ประกอบของตารางมีคุณสมบัติที่แตกต่างกันหลายอย่างที่มีผลต่อวิธีการคำนวณความกว้างของตารางโดยรวมเช่นเดียวกับแต่ละเซลล์มีการคำนวณ:
- ความกว้างที่ต้องการบนโต๊ะ
- ความกว้างที่ต้องการในแต่ละเซลล์
- การอนุญาตให้พอดีอัตโนมัติบนโต๊ะ
บทความนี้อธิบายวิธีการทำงานของคุณสมบัติการคำนวณความกว้างของตารางต่างๆและวิธี นี่มัน ประโยชน์อย่างยิ่งที่จะรู้ว่าในกรณีดังกล่าวที่รูปแบบตารางไม่ปรากฏตามที่คาดไว้.
ในกรณีส่วนใหญ่แนะนำให้ใช้เซลล์ที่ต้องการมากกว่าความกว้างของตาราง ความกว้างของเซลล์ที่ต้องการจะสอดคล้องกับข้อกำหนดรูปแบบDOCXรวมทั้งรุ่นAspose.Wordsมากขึ้น.
ความกว้างของเซลล์เป็นค่าที่คำนวณได้สำหรับรูปแบบDOCX ความกว้างของเซลล์จริงอาจขึ้นอยู่กับหลายสิ่ง ตัวอย่างเช่นการเปลี่ยนระยะขอบหน้าหรือความกว้างตารางที่ต้องการจะมีผลต่อความกว้.
ความกว้างของเซลล์ที่ต้องการคือคุณสมบัติของเซลล์ที่เก็บไว้ในเอกสาร มันไม่ได้ขึ้นอยู่กับอะไรและไม่เปลี่ยนแปลงเมื่อคุณเปลี่ยนตารางหรือคุณสมบัติอื่นๆของเซลล์.
วิธีการใช้ความกว้างที่ต้องการ
ความกว้างที่ต้องการของตารางหรือแต่ละเซลล์ถูกกำหนดผ่านคุณสมบัติความกว้างที่ต้อง ความกว้างที่ต้องการสามารถระบุได้สำหรับตารางทั้งหมดหรือสำหรับแต่ละเซลล์ ในบางสถานการณ์มันอาจจะไม่สามารถที่จะพอดีกับความกว้างนี้ตรงแต่ความกว้างที่แท้จริ.
ชนิดความกว้างและค่าที่ต้องการที่เหมาะสมจะถูกตั้งค่าโดยใช้วิธีการของคลาสของPreferredWidth:
- วิธีการAutoเพื่อระบุอัตโนมัติหรือ"ไม่มีความกว้างที่ต้องการ"
- วิธีการFromPercentเพื่อระบุเปอร์เซ็นต์ความกว้าง
- วิธีการFromPointsเพื่อระบุความกว้างของจุด
รูปภาพด้านล่างแสดงตัวแทนของpreferred width setting featuresในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันในAspose.Words.
ตัวอย่างของวิธีใช้ตัวเลือกเหล่านี้กับตารางจริงในเอกสารสามารถเห็นได้ในภาพด้านล่า.
ระบุตารางที่ต้องการหรือความกว้างของเซลล์
ในAspose.Wordsจะมีการตั้งค่าความกว้างของตารางและเซลล์โดยใช้คุณสมบัติTable.PreferredWidthและคุณสมบัติCellFormat.PreferredWidthพร้อมด้วยตัวเลือกในการแจงนับPreferredWidthType:
- Autoซึ่งเทียบเท่ากับชุดความกว้างที่ต้องการ
- Percentซึ่งเหมาะกับองค์ประกอบเมื่อเทียบกับพื้นที่ว่างในหน้าต่างหรือขนาดคอนเทนเนอร์และคำนวณค่าใหม่เมื่อความกว้างที่มีอยู่มีการเปลี่ยนแปลง
- Pointsซึ่งสอดคล้องกับองค์ประกอบของความกว้างที่ระบุในจุด
การใช้คุณสมบัติTable.PreferredWidthจะปรับความกว้างที่ต้องการเมื่อเทียบกับคอนเทนเนอร์ได้แก่เพจคอลัมน์ข้.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าตารางให้พอดีกับอัตโนมัติ 50%ของความกว้างของหน้า:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Insert a table with a width that takes up half the page width. | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
table->set_PreferredWidth(PreferredWidth::FromPercent(50)); | |
builder->Writeln(u"Cell #1"); | |
builder->InsertCell(); | |
builder->Writeln(u"Cell #2"); | |
builder->InsertCell(); | |
builder->Writeln(u"Cell #3"); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitToPageWidth.docx"); |
การใช้คุณสมบัติCellFormat.PreferredWidthบนเซลล์ที่กำหนดจะปรับความกว้างที่ต้องการ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าการตั้งค่าความกว้างที่ต้องการที่แตกต่างกัน:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Insert a table row made up of three cells which have different preferred widths. | |
builder->StartTable(); | |
// Insert an absolute sized cell. | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPoints(40)); | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightYellow()); | |
builder->Writeln(u"Cell at 40 points width"); | |
// Insert a relative (percent) sized cell. | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPercent(20)); | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightBlue()); | |
builder->Writeln(u"Cell at 20% width"); | |
// Insert a auto sized cell. | |
builder->InsertCell(); | |
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::Auto()); | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightGreen()); | |
builder->Writeln(u"Cell automatically sized. The size of this cell is calculated from the table preferred width."); | |
builder->Writeln(u"In this case the cell will fill up the rest of the available space."); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.PreferredWidthSettings.docx"); |
ค้นหาประเภทความกว้างและค่าที่ต้องการ
คุณสามารถใช้คุณสมบัติTypeและValueเพื่อค้นหารายละเอียดความกว้างที่ต้องการของตารางหรือเซลล์ที่ต้องการ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงชนิดความกว้างที่ต้องการของเซลล์ตาราง:
// 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)); | |
table->set_AllowAutoFit(true); | |
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell(); | |
PreferredWidthType type = firstCell->get_CellFormat()->get_PreferredWidth()->get_Type(); | |
double value = firstCell->get_CellFormat()->get_PreferredWidth()->get_Value(); |
วิธีการตั้งค่าพอดีอัตโนมัติ
คุณสมบัติAllowAutoFitช่วยให้เซลล์ในตารางเติบโตและหดตัวตามเกณฑ์ที่เลือก ตัวอย่างเช่นคุณสามารถใช้ตัวเลือกAutoFit to Windowเพื่อให้พอดีกับตารางความกว้างของหน้าและตัวเลือกAutoFit to Contentเพื่ออนุญาตให้แต่ละเซลล์เติบโตหรือหดตัวตามเนื้อหา.
โดยค่าเริ่มต้นAspose.Wordsแทรกตารางใหม่โดยใช้AutoFit to Window ตารางจะมีขนาดตามความกว้างของหน้าเว็บที่มีอยู่ เมื่อต้องการปรับขนาดตารางคุณสามารถเรียกวิธีการAutoFit วิธีนี้ยอมรับการแจงนับAutoFitBehaviorที่ระบุประเภทของการปรับพอดีอัตโนมัติถูกนำไปใช้กับตาราง.
มันเป็นสิ่งสำคัญที่จะรู้ว่าวิธีการพอดีอัตโนมัติเป็นจริงทางลัดที่ใช้คุณสมบัติที่แตกต่างกับตาร เหล่านี้เป็นคุณสมบัติที่จริงให้ตารางลักษณะการทำงานที่สังเกต เราจะหารือเกี่ยวกับคุณสมบัติเหล่านี้สำหรับแต่ละตัวเลือกพอดีอัตโนมัติ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าตารางให้หดตัวหรือเติบโตแต่ละเซลล์ตามเนื้อหา:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
table->set_AllowAutoFit(true); |
AutoFitตารางไปยังหน้าต่าง
เมื่อปรับอัตโนมัติไปยังหน้าต่างถูกนำไปใช้กับตาราง,การดำเนินการต่อไปนี้จะดำเนินการจริงอยู่เบื้องหลัง:
- คุณสมบัติTable.AllowAutoFitถูกเปิดใช้งานเพื่อปรับขนาดคอลัมน์โดยอัตโนมัติเพื่อให้พอดีกับเนื้อหาที่มีอยู่โดยใช้ค่าTable.PreferredWidth100%
- CellFormat.PreferredWidthจะถูกลบออกจากเซลล์ทั้งหมดของตาราง
โปรดทราบว่าสิ่งนี้แตกต่างจากพฤติกรรมMicrosoft Wordเล็กน้อยซึ่งความกว้างที่ต้องการของแต่ละเซลล์ถูกตั้งค่าเป็นค่าที่เหมาะสมตามขนาดและเนื้อหาปัจจุบัน 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"); | |
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true)); | |
// Autofit the first table to the page width. | |
table->AutoFit(AutoFitBehavior::AutoFitToWindow); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToWindow.docx"); |
AutoFitตารางเนื้อหา
เมื่อตารางถูกติดตั้งโดยอัตโนมัติเนื้อหา,ขั้นตอนต่อไปนี้จะดำเนินการจริงเบื้องหลัง:
-
คุณสมบัติTable.AllowAutoFitถูกเปิดใช้งานเพื่อปรับขนาดแต่ละเซลล์ตามเนื้อหาโดยอัตโนมัติ
-
ความกว้างตารางที่ต้องการจะถูกลบออกจากTable.PreferredWidth,CellFormat.PreferredWidthจะถูกลบออกสำหรับแต่ละเซลล์ตาราง
โปรดทราบว่าตัวเลือกการติดตั้งอัตโนมัตินี้จะลบความกว้างที่ต้องการออกจากเซลล์เช่นเดียวกับในMicrosoft Word หากคุณต้องการเก็บขนาดคอลัมน์ไว้และเพิ่มหรือลดขนาดคอลัมน์ให้พอดีกับเนื้อหาคุณควรตั้งคุณสมบัติTable.AllowAutoFitเป็นTrueด้วยตัวเองแทนที่จะใช้ทางลัดที่พอดีอัตโนมัติ -
ความกว้างของคอลัมน์จะถูกคำนวณใหม่สำหรับเนื้อหาของตารางปัจจุบัน–ผลลัพธ์สุดท้ายคือต
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการปรับตารางโดยอัตโนมัติกับเนื้อหา:
// 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)); | |
table->AutoFit(AutoFitBehavior::AutoFitToContents); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToContents.docx"); |
ปิดการใช้งานAutoFitในตารางและใช้ความกว้างของคอลัมน์คงที่
ถ้าตารางมีการปิดใช้งานการติดตั้งอัตโนมัติและความกว้างคอลัมน์คงที่ถูกใช้แทนขั้นตอนต่:
- Table.AllowAutoFitคุณสมบัติถูกปิดใช้งานดังนั้นคอลัมน์จะไม่เติบโตหรือหดตัวลงในเนื้อหา
- ความกว้างที่ต้องการของตารางทั้งหมดจะถูกลบออกจากTable.PreferredWidth,CellFormat.PreferredWidthจะถูกลบออกจากเซลล์ตารางทั้งหมด
- ผลลัพธ์สุดท้ายคือตารางที่มีความกว้างของคอลัมน์จะถูกกำหนดโดยคุณสมบัติCellFormat.Widthและคอลัมน์ที่ไ
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการปิดใช้งานการติดตั้งอัตโนมัติและเปิดใช้งานความกว้างคงที่สำหรับตารางที่ระบุ:
// 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)); | |
// Disable autofitting on this table. | |
table->AutoFit(AutoFitBehavior::FixedColumnWidths); | |
doc->Save(ArtifactsDir + u"WorkingWithTables.AutoFitTableToFixedColumnWidths.docx"); |
ลำดับความสำคัญเมื่อคำนวณความกว้างของเซลล์
Aspose.Wordsอนุญาตให้ผู้ใช้กำหนดความกว้างของตารางหรือเซลล์ผ่านหลายออบเจกต์รวมถึงCellFormat–คุณสมบัติWidthส่วนใหญ่จะเหลือจากเวอร์ชันก่อนหน้าแต่ก็ยังมีประโยชน์สำหรับการลดความกว้างของเซลล์.
สิ่งสำคัญคือต้องรู้ว่าคุณสมบัติCellFormat.Widthทำงานแตกต่างกันขึ้นอยู่กับคุณสมบัติความกว้างอื่นที่มีอยู่ในต.
Aspose.Wordsใช้ลำดับต่อไปนี้สำหรับการคำนวณความกว้างของเซลล์:
สั่งซื้อ | ทรัพย์สิน | การบรรจุ |
---|---|---|
AllowAutoFitถูกกำหนด | ถ้าAutoFitถูกเปิดใช้งาน: -ตารางอาจเติบโตที่ผ่านมาความกว้างที่ต้องการเพื่อรองรับเนื้อหา-มันมักจะไม่หดตัวต่ำกว่ -การเปลี่ยนแปลงค่าCellFormat.Widthจะถูกละเว้นและเซลล์จะพอดีกับเนื้อหาแทน |
|
PreferredWidthTypeมีค่าPointsหรือPercent | CellFormat.Widthถูกละเว้น | |
PreferredWidthTypeมีค่าAuto | ค่าจากCellFormat.Widthถูกคัดลอกและกลายเป็นความกว้างที่ต้องการของเซลล์(ในจุด) |
อนุญาตให้ระยะห่างระหว่างเซลล์
คุณสามารถรับหรือตั้งค่าช่องว่างเพิ่มเติมระหว่างเซลล์ตารางที่คล้ายกับตัวเลือก"ระยะห่างของเซลล์"ในMicrosoft Word นี้สามารถทำได้โดยใช้คุณสมบัติAllowCellSpacing.
ตัวอย่างของวิธีใช้ตัวเลือกเหล่านี้กับตารางจริงในเอกสารสามารถเห็นได้ในภาพด้านล่า.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งระยะห่างระหว่างเซลล์:
// 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)); | |
table->set_AllowCellSpacing(true); | |
table->set_CellSpacing(2); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.AllowCellSpacing.docx"); |
ใช้เส้นขอบและการแรเงา
เส้นขอบและการแรเงาสามารถนำไปใช้กับตารางทั้งหมดโดยใช้Table.SetBorder,Table.SetBordersและTable.SetShadingหรือเฉพาะกับเซลล์ที่เฉพาะเจาะจงโดยใช้CellFormat.BordersและCellFormat.Shading นอกจากนี้เส้นขอบแถวสามารถตั้งค่าได้โดยใช้RowFormat.Bordersแต่ไม่สามารถใช้การแรเงาด้วยวิธีนี้ได้.
รูปภาพด้านล่างแสดงการตั้งค่าเส้นขอบและเงาในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันใน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>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
SharedPtr<Table> table = builder->StartTable(); | |
builder->InsertCell(); | |
// Set the borders for the entire table. | |
table->SetBorders(LineStyle::Single, 2.0, System::Drawing::Color::get_Black()); | |
// Set the cell shading for this cell. | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_Red()); | |
builder->Writeln(u"Cell #1"); | |
builder->InsertCell(); | |
// Specify a different cell shading for the second cell. | |
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_Green()); | |
builder->Writeln(u"Cell #2"); | |
builder->EndRow(); | |
// Clear the cell formatting from previous operations. | |
builder->get_CellFormat()->ClearFormatting(); | |
builder->InsertCell(); | |
// Create larger borders for the first cell of this row. This will be different | |
// compared to the borders set for the table. | |
builder->get_CellFormat()->get_Borders()->get_Left()->set_LineWidth(4.0); | |
builder->get_CellFormat()->get_Borders()->get_Right()->set_LineWidth(4.0); | |
builder->get_CellFormat()->get_Borders()->get_Top()->set_LineWidth(4.0); | |
builder->get_CellFormat()->get_Borders()->get_Bottom()->set_LineWidth(4.0); | |
builder->Writeln(u"Cell #3"); | |
builder->InsertCell(); | |
builder->get_CellFormat()->ClearFormatting(); | |
builder->Writeln(u"Cell #4"); | |
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.FormatTableAndCellWithDifferentBorders.docx"); |