Bekerja dengan Kolom dan Baris
Untuk kontrol lebih besar atas cara kerja tabel, pelajari cara memanipulasi kolom dan baris.
Temukan Indeks Elemen Tabel
Kolom, baris, dan sel dikelola dengan mengakses simpul dokumen yang dipilih berdasarkan indeksnya. Menemukan indeks dari setiap node melibatkan pengumpulan semua node turunan 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 dapat 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-Java.git. | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
NodeCollection allTables = doc.getChildNodes(NodeType.TABLE, true); | |
int tableIndex = allTables.indexOf(table); |
Menemukan Indeks Baris dalam sebuah 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-Java.git. | |
int rowIndex = table.indexOf(table.getLastRow()); |
Menemukan Indeks Sel dalam Satu 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-Java.git. | |
int cellIndex = row.indexOf(row.getCells().get(4)); |
Bekerja dengan Kolom
Dalam Model Objek Dokumen Aspose.Words (DOM), simpul Table terdiri dari Row simpul dan kemudian Cell simpul. Jadi, dalam Model Objek Document
dari Aspose.Words, seperti pada 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. Ini memberi tabel kemampuan untuk memiliki beberapa atribut yang menarik:
- Setiap baris tabel dapat memiliki jumlah sel yang sama sekali berbeda
- Secara vertikal, sel setiap baris dapat memiliki lebar yang berbeda
- Dimungkinkan untuk menggabungkan tabel dengan format baris dan jumlah sel yang 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 ke kolom. Artinya, Anda dapat melakukan operasi pada kolom hanya dengan mengulangi indeks sel baris tabel yang sama.
Contoh kode berikut menyederhanakan operasi tersebut dengan membuktikan kelas facade yang mengumpulkan sel-sel yang membentuk “kolom” dari sebuah tabel:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
/// <summary> | |
/// Represents a facade object for a column of a table in a Microsoft Word document. | |
/// </summary> | |
static class Column | |
{ | |
private Column(Table table, int columnIndex) { | |
if (table != null) { | |
mTable = table; | |
} else { | |
throw new IllegalArgumentException("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); | |
} | |
private ArrayList<Cell> getCells() { | |
return getColumnCells(); | |
} | |
/// <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() | |
{ | |
ArrayList<Cell> columnCells = getCells(); | |
if (columnCells.size() == 0) | |
throw new IllegalArgumentException("Column must not be empty"); | |
// Create a clone of this column. | |
for (Cell cell : columnCells) | |
cell.getParentRow().insertBefore(cell.deepClone(false), cell); | |
// This is the new column. | |
Column column = new Column(columnCells.get(0).getParentRow().getParentTable(), mColumnIndex); | |
// We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
for (Cell cell : column.getCells()) | |
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() | |
{ | |
for (Cell cell : getCells()) | |
cell.remove(); | |
} | |
/// <summary> | |
/// Returns the text of the column. | |
/// </summary> | |
public String toTxt() throws Exception | |
{ | |
StringBuilder builder = new StringBuilder(); | |
for (Cell cell : getCells()) | |
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 ArrayList<Cell> getColumnCells() | |
{ | |
ArrayList<Cell> columnCells = new ArrayList<Cell>(); | |
for (Row row : mTable.getRows()) | |
{ | |
Cell cell = row.getCells().get(mColumnIndex); | |
if (cell != null) | |
columnCells.add(cell); | |
} | |
return columnCells; | |
} | |
private int mColumnIndex; | |
private 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-Java.git. | |
Document doc = new Document(getMyDir() + "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. | |
System.out.println(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(); | |
for (Cell cell : newColumn.getColumnCells()) | |
cell.getFirstParagraph().appendChild(new Run(doc, "Column Text " + newColumn.indexOf(cell))); |
Contoh kode berikut menunjukkan cara menghapus kolom dari tabel dalam dokumen:
// 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, 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 di halaman pertama atau di setiap halaman jika tabel dibagi 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 ditempatkan 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-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startTable(); | |
builder.getRowFormat().setHeadingFormat(true); | |
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); | |
builder.getCellFormat().setWidth(100.0); | |
builder.insertCell(); | |
builder.writeln("Heading row 1"); | |
builder.endRow(); | |
builder.insertCell(); | |
builder.writeln("Heading row 2"); | |
builder.endRow(); | |
builder.getCellFormat().setWidth(50.0); | |
builder.getParagraphFormat().clearFormatting(); | |
for (int i = 0; i < 50; i++) | |
{ | |
builder.insertCell(); | |
builder.getRowFormat().setHeadingFormat(false); | |
builder.write("Column 1 Text"); | |
builder.insertCell(); | |
builder.write("Column 2 Text"); | |
builder.endRow(); | |
} | |
doc.save(getArtifactsDir() + "WorkingWithTables.RepeatRowsOnSubsequentPages.docx"); |
Jaga agar Tabel dan Baris tidak Pecah Di Seluruh Halaman
Ada kalanya konten tabel tidak boleh dibagi menjadi beberapa halaman. Misalnya, jika judul berada di atas tabel, judul dan tabel harus selalu disatukan pada halaman yang sama untuk mempertahankan tampilan yang tepat.
Ada dua teknik terpisah yang berguna untuk mencapai fungsionalitas ini:
Allow row break across pages
, yang diterapkan ke baris tabelKeep with next
, yang diterapkan pada paragraf dalam sel tabel
Secara default, properti di atas dinonaktifkan.

Jaga agar Baris tidak Melintasi Halaman
Ini melibatkan pembatasan konten di dalam sel baris agar tidak dipisah di seluruh halaman. Di Microsoft Word, ini dapat ditemukan di bawah Properti Tabel sebagai opsi “Izinkan baris untuk melintasi halaman”. Dalam Aspose.Words ini ditemukan di bawah objek RowFormat dari Row sebagai properti RowFormat.AllowBreakAcrossPages.

Contoh kode berikut menunjukkan cara menonaktifkan pemutusan baris 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-Java.git. | |
Document doc = new Document(getMyDir() + "Table spanning two pages.docx"); | |
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); | |
// Disable breaking across pages for all rows in the table. | |
for (Row row : (Iterable<Row>) table.getRows()) | |
row.getRowFormat().setAllowBreakAcrossPages(false); | |
doc.save(getArtifactsDir() + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx"); |
Jaga agar Tabel tidak Pecah Di Seluruh Halaman
Untuk menghentikan tabel agar tidak terbelah di seluruh halaman, kita perlu menentukan bahwa kita ingin konten yang terkandung di dalam tabel tetap bersama.
Untuk melakukannya, Aspose.Words menggunakan metode, yang memungkinkan pengguna memilih tabel dan mengaktifkan parameter KeepWithNext menjadi true untuk setiap paragraf di dalam sel tabel. Pengecualian 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-Java.git. | |
Document doc = new Document(getMyDir() + "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. | |
for (Cell cell : (Iterable<Cell>) table.getChildNodes(NodeType.CELL, true)) | |
{ | |
cell.ensureMinimum(); | |
for (Paragraph para : (Iterable<Paragraph>) cell.getParagraphs()) | |
if (!(cell.getParentRow().isLastRow() && para.isEndOfCell())) | |
para.getParagraphFormat().setKeepWithNext(true); | |
} | |
doc.save(getArtifactsDir() + "WorkingWithTables.KeepTableTogether.docx"); |