Bekerja dengan Kolom dan Baris
Untuk kontrol lebih besar terhadap cara kerja tabel, pelajari cara memanipulasi kolom dan baris.
Temukan Indeks Elemen Tabel
Kolom, baris, dan sel dikelola dengan mengakses node dokumen yang dipilih berdasarkan indeksnya. Menemukan indeks dari setiap node melibatkan pengumpulan semua node anak dari tipe elemen dari node induk, dan kemudian menggunakan metode IndexOf untuk menemukan indeks dari node yang diinginkan dalam koleksi.
Temukan Indeks Tabel dalam Dokumen
Terkadang Anda mungkin perlu membuat perubahan pada tabel tertentu dalam dokumen. Untuk melakukan ini, Anda bisa merujuk ke tabel berdasarkan indeksnya.
Contoh kode berikut menunjukkan cara mengambil indeks tabel dalam dokumen:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); | |
int tableIndex = allTables.IndexOf(table); |
Temukan Indeks Baris dalam Tabel
Demikian pula, Anda mungkin perlu membuat perubahan pada baris tertentu dalam tabel yang dipilih. Untuk melakukan ini, Anda juga dapat merujuk ke baris berdasarkan indeksnya.
Contoh kode berikut menunjukkan cara mengambil indeks baris dalam tabel:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
int rowIndex = table.IndexOf(table.LastRow); |
Temukan Indeks Sel di Baris
Terakhir, Anda mungkin perlu membuat perubahan pada sel tertentu, dan Anda juga dapat melakukannya dengan indeks sel.
Contoh kode berikut menunjukkan cara mengambil indeks sel dalam satu baris:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
int cellIndex = row.IndexOf(row.Cells[4]); |
Bekerja dengan Kolom
Dalam Aspose.Words Document Object Model (DOM), node Table terdiri dari node Row dan kemudian node Cell. Jadi, dalam Model Objek Document
Aspose.Words, seperti dalam dokumen Word, tidak ada konsep kolom.
Secara desain, baris tabel di Microsoft Word dan Aspose.Words sepenuhnya independen, dan properti serta operasi dasar hanya terdapat di baris dan sel tabel. Hal ini memberikan tabel kemampuan untuk memiliki beberapa atribut menarik:
- Setiap baris tabel dapat memiliki jumlah sel yang sangat berbeda
- Secara vertikal, sel setiap baris dapat memiliki lebar berbeda
- Dimungkinkan untuk menggabungkan tabel dengan format baris dan jumlah sel berbeda
Setiap operasi yang dilakukan pada kolom sebenarnya adalah “pintasan” yang melakukan operasi dengan mengubah sel baris secara kolektif sedemikian rupa sehingga terlihat seperti diterapkan pada kolom. Artinya, Anda dapat melakukan operasi pada kolom hanya dengan melakukan iterasi pada indeks sel baris tabel yang sama.
Contoh kode berikut menyederhanakan operasi tersebut dengan membuktikan kelas fasad yang mengumpulkan sel-sel yang membentuk “kolom” tabel:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
/// <summary> | |
/// Represents a facade object for a column of a table in a Microsoft Word document. | |
/// </summary> | |
internal class Column | |
{ | |
private Column(Table table, int columnIndex) | |
{ | |
mTable = table ?? throw new ArgumentException("table"); | |
mColumnIndex = columnIndex; | |
} | |
/// <summary> | |
/// Returns a new column facade from the table and supplied zero-based index. | |
/// </summary> | |
public static Column FromIndex(Table table, int columnIndex) | |
{ | |
return new Column(table, columnIndex); | |
} | |
/// <summary> | |
/// Returns the cells which make up the column. | |
/// </summary> | |
public Cell[] Cells => GetColumnCells().ToArray(); | |
/// <summary> | |
/// Returns the index of the given cell in the column. | |
/// </summary> | |
public int IndexOf(Cell cell) | |
{ | |
return GetColumnCells().IndexOf(cell); | |
} | |
/// <summary> | |
/// Inserts a brand new column before this column into the table. | |
/// </summary> | |
public Column InsertColumnBefore() | |
{ | |
Cell[] columnCells = Cells; | |
if (columnCells.Length == 0) | |
throw new ArgumentException("Column must not be empty"); | |
// Create a clone of this column. | |
foreach (Cell cell in columnCells) | |
cell.ParentRow.InsertBefore(cell.Clone(false), cell); | |
// This is the new column. | |
Column column = new Column(columnCells[0].ParentRow.ParentTable, mColumnIndex); | |
// We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
foreach (Cell cell in column.Cells) | |
cell.EnsureMinimum(); | |
// Increase the index which this column represents since there is now one extra column in front. | |
mColumnIndex++; | |
return column; | |
} | |
/// <summary> | |
/// Removes the column from the table. | |
/// </summary> | |
public void Remove() | |
{ | |
foreach (Cell cell in Cells) | |
cell.Remove(); | |
} | |
/// <summary> | |
/// Returns the text of the column. | |
/// </summary> | |
public string ToTxt() | |
{ | |
StringBuilder builder = new StringBuilder(); | |
foreach (Cell cell in Cells) | |
builder.Append(cell.ToString(SaveFormat.Text)); | |
return builder.ToString(); | |
} | |
/// <summary> | |
/// Provides an up-to-date collection of cells which make up the column represented by this facade. | |
/// </summary> | |
private List<Cell> GetColumnCells() | |
{ | |
List<Cell> columnCells = new List<Cell>(); | |
foreach (Row row in mTable.Rows) | |
{ | |
Cell cell = row.Cells[mColumnIndex]; | |
if (cell != null) | |
columnCells.Add(cell); | |
} | |
return columnCells; | |
} | |
private int mColumnIndex; | |
private readonly Table mTable; | |
} |
Contoh kode berikut menunjukkan cara menyisipkan kolom kosong ke dalam tabel:
// 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"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Column column = Column.FromIndex(table, 0); | |
// Print the plain text of the column to the screen. | |
Console.WriteLine(column.ToTxt()); | |
// Create a new column to the left of this column. | |
// This is the same as using the "Insert Column Before" command in Microsoft Word. | |
Column newColumn = column.InsertColumnBefore(); | |
foreach (Cell cell in newColumn.Cells) | |
cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell))); |
Contoh kode berikut memperlihatkan cara menghapus kolom dari tabel di dokumen:
// 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"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 1, true); | |
Column column = Column.FromIndex(table, 2); | |
column.Remove(); |
Tentukan Baris sebagai Baris Header
Anda dapat memilih untuk mengulang baris pertama dalam tabel sebagai Baris Header hanya pada halaman pertama atau pada setiap halaman jika tabel dipecah menjadi beberapa. Di Aspose.Words, Anda dapat mengulang Baris Header di setiap halaman menggunakan properti HeadingFormat.
Anda juga dapat menandai beberapa baris header jika baris tersebut terletak satu demi satu di awal tabel. Untuk melakukan ini, Anda perlu menerapkan properti HeadingFormat ke baris ini.
Contoh kode berikut menunjukkan cara membuat tabel yang menyertakan Baris Header yang berulang di halaman berikutnya:
// 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); | |
builder.StartTable(); | |
builder.RowFormat.HeadingFormat = true; | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.CellFormat.Width = 100; | |
builder.InsertCell(); | |
builder.Writeln("Heading row 1"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Heading row 2"); | |
builder.EndRow(); | |
builder.CellFormat.Width = 50; | |
builder.ParagraphFormat.ClearFormatting(); | |
for (int i = 0; i < 50; i++) | |
{ | |
builder.InsertCell(); | |
builder.RowFormat.HeadingFormat = false; | |
builder.Write("Column 1 Text"); | |
builder.InsertCell(); | |
builder.Write("Column 2 Text"); | |
builder.EndRow(); | |
} | |
doc.Save(ArtifactsDir + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx"); |
Cegah Tabel dan Baris agar Tidak Melanggar Halaman
Ada kalanya isi tabel tidak boleh dipecah menjadi beberapa halaman. Misalnya, jika judul berada di atas tabel, judul dan tabel harus selalu diletakkan bersamaan pada halaman yang sama untuk menjaga tampilan yang tepat.
Ada dua teknik terpisah yang berguna untuk mencapai fungsi ini:
Allow row break across pages
, yang diterapkan pada baris tabelKeep with next
, yang diterapkan pada paragraf di sel tabel
Secara default, properti di atas dinonaktifkan.
Cegah Satu Baris agar Tidak Melanggar Halaman
Hal ini melibatkan pembatasan konten di dalam sel-sel baris agar tidak dipecah menjadi satu halaman. Di Microsoft Word, ini dapat ditemukan di bawah Properti Tabel sebagai opsi “Izinkan baris untuk dipecah melintasi halaman”. Di Aspose.Words ini ditemukan di bawah objek RowFormat Row sebagai properti RowFormat.AllowBreakAcrossPages.
Contoh kode berikut menunjukkan cara menonaktifkan baris pemisah di seluruh halaman untuk setiap baris dalam tabel:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// Disable breaking across pages for all rows in the table. | |
foreach (Row row in table.Rows) | |
row.RowFormat.AllowBreakAcrossPages = false; | |
doc.Save(ArtifactsDir + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx"); |
Jaga agar Tabel tidak Melanggar Halaman
Untuk menghentikan tabel agar tidak terpecah menjadi beberapa halaman, kita perlu menentukan bahwa kita ingin konten yang terdapat dalam tabel tetap bersama.
Untuk melakukan hal ini, Aspose.Words menggunakan metode yang memungkinkan pengguna memilih tabel dan mengaktifkan parameter KeepWithNext ke true untuk setiap paragraf dalam sel tabel. Pengecualiannya adalah paragraf terakhir dalam tabel, yang harus disetel ke false.
Contoh kode berikut menunjukkan cara mengatur tabel agar tetap bersama di halaman yang sama:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page, | |
// except for the last paragraphs in the last row of the table. | |
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true)) | |
{ | |
cell.EnsureMinimum(); | |
foreach (Paragraph para in cell.Paragraphs) | |
if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell)) | |
para.ParagraphFormat.KeepWithNext = true; | |
} | |
doc.Save(ArtifactsDir + "WorkingWithTables.KeepTableTogether.docx"); |