ใช้สไตล์ตาราง
สไตล์ตารางจะกำหนดชุดการจัดรูปแบบที่สามารถนำไปใช้กับตารางได้อย่างง่ายดาย การจัดรูปแบบ เช่น เส้นขอบ การแรเงา การจัดแนว และแบบอักษรสามารถตั้งค่าในลักษณะตารางและนำไปใช้กับตารางจำนวนมากเพื่อให้มีลักษณะที่สอดคล้องกัน
Aspose.Words รองรับการใช้สไตล์ตารางกับตารางและยังอ่านคุณสมบัติของสไตล์ตารางใดก็ได้ สไตล์ตารางจะถูกรักษาไว้ในระหว่างการโหลดและบันทึกด้วยวิธีต่อไปนี้:
- สไตล์ตารางในรูปแบบ DOCX และ WordML จะถูกรักษาไว้เมื่อโหลดและบันทึกเป็นรูปแบบเหล่านี้
- สไตล์ตารางจะยังคงอยู่เมื่อโหลดและบันทึกในรูปแบบ DOC (แต่ไม่ใช่รูปแบบอื่น)
- เมื่อส่งออกเป็นรูปแบบอื่น การเรนเดอร์หรือการพิมพ์ รูปแบบตารางจะถูกขยายไปสู่การจัดรูปแบบโดยตรงในตาราง ดังนั้นการจัดรูปแบบทั้งหมดจึงยังคงอยู่
สร้างสไตล์ตาราง
ผู้ใช้สามารถสร้างสไตล์ใหม่และเพิ่มลงในคอลเลกชันสไตล์ได้ วิธีการ Add ใช้เพื่อสร้างสไตล์ตารางใหม่
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างรูปแบบตารางที่ผู้ใช้กำหนดใหม่:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.Borders.LineStyle = LineStyle.Double; | |
tableStyle.Borders.LineWidth = 1; | |
tableStyle.LeftPadding = 18; | |
tableStyle.RightPadding = 18; | |
tableStyle.TopPadding = 12; | |
tableStyle.BottomPadding = 12; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx"); |
คัดลอกสไตล์ตารางที่มีอยู่
หากจำเป็น คุณสามารถคัดลอกลักษณะตารางที่มีอยู่แล้วในเอกสารบางฉบับลงในคอลเลกชั่นสไตล์ของคุณได้โดยใช้วิธี AddCopy
สิ่งสำคัญคือต้องรู้ว่าด้วยการคัดลอกนี้ สไตล์ที่เชื่อมโยงก็จะถูกคัดลอกด้วย
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการนำเข้าสไตล์จากเอกสารหนึ่งไปยังเอกสารอื่น:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document srcDoc = new Document(); | |
// Create a custom style for the source document. | |
Style srcStyle = srcDoc.Styles.Add(StyleType.Paragraph, "MyStyle"); | |
srcStyle.Font.Color = Color.Red; | |
// Import the source document's custom style into the destination document. | |
Document dstDoc = new Document(); | |
Style newStyle = dstDoc.Styles.AddCopy(srcStyle); | |
// The imported style has an appearance identical to its source style. | |
Assert.AreEqual("MyStyle", newStyle.Name); | |
Assert.AreEqual(Color.Red.ToArgb(), newStyle.Font.Color.ToArgb()); |
ใช้สไตล์ตารางที่มีอยู่
Aspose.Words จัดเตรียม TableStyle ที่สืบทอดมาจากคลาส Style TableStyle อำนวยความสะดวกให้ผู้ใช้ใช้ตัวเลือกสไตล์ต่างๆ เช่น การแรเงา การเติม การเยื้อง CellSpacing และ Font เป็นต้น
นอกจากนี้ Aspose.Words ยังมีคลาส StyleCollection และคุณสมบัติบางอย่างของคลาส Table
เพื่อระบุรูปแบบตารางที่เราจะใช้งาน: Style, StyleIdentifier, StyleName และ StyleOptions
Aspose.Words ยังมีคลาส ConditionalStyle ที่แสดงการจัดรูปแบบพิเศษที่ใช้กับพื้นที่บางส่วนของตารางด้วยสไตล์ตารางที่กำหนด และ ConditionalStyleCollection ที่แสดงคอลเลกชันของออบเจ็กต์ ConditionalStyle คอลเลกชันนี้ประกอบด้วยชุดรายการถาวรซึ่งแสดงถึงหนึ่งรายการสำหรับแต่ละค่าของประเภทการแจงนับ ConditionalStyleType การแจงนับ ConditionalStyleType จะกำหนดพื้นที่ตารางที่เป็นไปได้ทั้งหมดซึ่งอาจกำหนดการจัดรูปแบบตามเงื่อนไขในลักษณะตารางได้
ในกรณีนี้ การจัดรูปแบบตามเงื่อนไขสามารถกำหนดได้สำหรับพื้นที่ตารางที่เป็นไปได้ทั้งหมดที่กำหนดไว้ภายใต้ประเภทการแจงนับ ConditionalStyleType
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนดการจัดรูปแบบตามเงื่อนไขสำหรับแถวส่วนหัวของตาราง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Name"); | |
builder.InsertCell(); | |
builder.Write("Value"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.InsertCell(); | |
builder.EndTable(); | |
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1"); | |
tableStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow; | |
tableStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone; | |
table.Style = tableStyle; | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx"); |
คุณยังสามารถเลือกส่วนของตารางที่จะปรับใช้ลักษณะได้ เช่น คอลัมน์แรก คอลัมน์สุดท้าย แถวที่มีแถบสี มีการระบุไว้ในการแจงนับ TableStyleOptions และนำไปใช้ผ่านคุณสมบัติ StyleOptions การแจงนับ TableStyleOptions อนุญาตให้รวมคุณลักษณะเหล่านี้ในระดับบิต
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างตารางใหม่โดยใช้สไตล์ตาราง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
// We must insert at least one row first before setting any table formatting. | |
builder.InsertCell(); | |
// Set the table style used based on the unique style identifier. | |
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1; | |
// Apply which features should be formatted by the style. | |
table.StyleOptions = | |
TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow; | |
table.AutoFit(AutoFitBehavior.AutoFitToContents); | |
builder.Writeln("Item"); | |
builder.CellFormat.RightPadding = 40; | |
builder.InsertCell(); | |
builder.Writeln("Quantity (kg)"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Apples"); | |
builder.InsertCell(); | |
builder.Writeln("20"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Bananas"); | |
builder.InsertCell(); | |
builder.Writeln("40"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Carrots"); | |
builder.InsertCell(); | |
builder.Writeln("50"); | |
builder.EndRow(); | |
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx"); |
รูปภาพด้านล่างแสดงการนำเสนอ Table Styles ใน Microsoft Word และคุณสมบัติที่เกี่ยวข้องใน Aspose.Words
ใช้การจัดรูปแบบจากลักษณะตารางและนำไปใช้เป็นการจัดรูปแบบโดยตรง
Aspose.Words ยังมีวิธี ExpandTableStylesToDirectFormatting เพื่อใช้การจัดรูปแบบที่พบในลักษณะตาราง และขยายไปยังแถวและเซลล์ของตารางเป็นการจัดรูปแบบโดยตรง ลองรวมการจัดรูปแบบเข้ากับสไตล์ตารางและสไตล์เซลล์
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการขยายการจัดรูปแบบจากสไตล์ไปยังแถวและเซลล์ของตารางเป็นการจัดรูปแบบโดยตรง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
// Get the first cell of the first table in the document. | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Cell firstCell = table.FirstRow.FirstCell; | |
// First print the color of the cell shading. | |
// This should be empty as the current shading is stored in the table style. | |
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor; | |
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore); | |
doc.ExpandTableStylesToDirectFormatting(); | |
// Now print the cell shading after expanding table styles. | |
// A blue background pattern color should have been applied from the table style. | |
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor; | |
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter); |