Terapkan Gaya Tabel
Gaya tabel mendefinisikan sekumpulan pemformatan yang dapat dengan mudah diterapkan ke tabel. Pemformatan seperti batas, bayangan, perataan, dan font dapat diatur dalam gaya tabel dan diterapkan ke banyak tabel untuk tampilan yang konsisten.
Aspose.Words mendukung penerapan gaya tabel ke tabel dan juga membaca properti gaya tabel apa pun. Gaya tabel dipertahankan selama pemuatan dan penyimpanan dengan cara berikut:
- Gaya tabel dalam format DOCX dan WordML dipertahankan saat memuat dan menyimpan ke format ini
- Gaya tabel dipertahankan saat memuat dan menyimpan dalam format DOC (tetapi tidak dalam format lain)
- Saat mengekspor ke format lain, rendering atau pencetakan, gaya tabel diperluas ke pemformatan langsung dalam tabel, sehingga semua pemformatan dipertahankan
Buat Gaya Tabel
Pengguna dapat membuat gaya baru dan menambahkannya ke koleksi gaya. Metode Add digunakan untuk membuat style tabel baru.
Contoh kode berikut menunjukkan cara membuat gaya tabel baru yang ditentukan pengguna:
// 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"); |
Salin Gaya Tabel yang Ada
Jika perlu, Anda dapat menyalin gaya tabel yang sudah ada pada dokumen tertentu ke dalam koleksi gaya Anda menggunakan metode AddCopy
.
Penting untuk diketahui bahwa dengan penyalinan ini, gaya tertaut juga disalin.
Contoh kode berikut menunjukkan cara mengimpor gaya dari satu dokumen ke dokumen lain:
// 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()); |
Terapkan Gaya Tabel yang Ada
Aspose.Words menyediakan TableStyle yang diwarisi dari kelas Style. TableStyle memfasilitasi pengguna untuk menerapkan opsi gaya yang berbeda seperti shading, padding, indentation, CellSpacing dan Font, dll.
Selain itu, Aspose.Words menyediakan kelas StyleCollection dan beberapa properti kelas Table
untuk menentukan gaya tabel mana yang akan kita gunakan: Style, StyleIdentifier, StyleName, dan StyleOptions.
Aspose.Words juga menyediakan kelas ConditionalStyle yang mewakili pemformatan khusus yang diterapkan pada beberapa area tabel dengan gaya tabel yang ditetapkan, dan ConditionalStyleCollection yang mewakili kumpulan objek ConditionalStyle. Koleksi ini berisi sekumpulan item permanen yang mewakili satu item untuk setiap nilai tipe enumerasi ConditionalStyleType. Pencacahan ConditionalStyleType mendefinisikan semua kemungkinan area tabel yang pemformatan bersyaratnya dapat ditentukan dalam gaya tabel.
Dalam hal ini, pemformatan bersyarat dapat ditentukan untuk semua kemungkinan area tabel yang ditentukan berdasarkan tipe enumerasi ConditionalStyleType.
Contoh kode berikut menunjukkan cara mendefinisikan pemformatan bersyarat untuk baris header tabel:
// 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"); |
Anda juga dapat memilih bagian tabel mana yang akan menerapkan gaya, seperti kolom pertama, kolom terakhir, baris berpita. Mereka tercantum dalam enumerasi TableStyleOptions dan diterapkan melalui properti StyleOptions. Pencacahan TableStyleOptions memungkinkan kombinasi bitwise dari fitur-fitur ini.
Contoh kode berikut menunjukkan cara membuat tabel baru dengan menerapkan gaya tabel:
// 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"); |
Gambar di bawah menunjukkan representasi Table Styles di Microsoft Word dan properti terkaitnya dalam Aspose.Words.
Ambil Pemformatan dari Gaya Tabel dan Terapkan sebagai Pemformatan Langsung
Aspose.Words juga menyediakan metode ExpandTableStylesToDirectFormatting untuk mengambil pemformatan yang ditemukan pada gaya tabel dan memperluasnya ke baris dan sel tabel sebagai pemformatan langsung. Coba gabungkan pemformatan dengan gaya tabel dan gaya sel.
Contoh kode berikut menunjukkan cara memperluas pemformatan dari gaya ke baris tabel dan sel sebagai pemformatan langsung:
// 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); |