分割テーブル

Contents
[ ]

Aspose.Wordsドキュメントオブジェクトモデルで表されるテーブルは、独立した行とセルで構成されているため、テーブルを簡単に分割できます。

テーブルを操作して2つのテーブルに分割するには、元のテーブルから新しいテーブルに行の一部を移動するだけです。 これを行うには、テーブルを分割する行を選択する必要があります。

次の簡単な手順に従って、元のテーブルから2つのテーブルを作成できます:

  1. 移動された行を保持し、元のテーブルの後に挿入するために、子を複製せずにテーブルのクローンを作成します
  2. 指定された行から開始して、後続のすべての行をこの2番目のテーブルに移動します

次のコード例は、テーブルを特定の行の2つのテーブルに分割する方法を示しています:

// 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 firstTable = (Table) doc.getChild(NodeType.TABLE, 0, true);
// We will split the table at the third row (inclusive).
Row row = firstTable.getRows().get(2);
// Create a new container for the split table.
Table table = (Table) firstTable.deepClone(false);
// Insert the container after the original.
firstTable.getParentNode().insertAfter(table, firstTable);
// Add a buffer paragraph to ensure the tables stay apart.
firstTable.getParentNode().insertAfter(new Paragraph(doc), firstTable);
Row currentRow;
do
{
currentRow = firstTable.getLastRow();
table.prependChild(currentRow);
} while (currentRow != row);
doc.save(getArtifactsDir() + "WorkingWithTables.SplitTable.docx");