テーブル内のテキストを操作する

以前の記事で述べたように、テーブルには通常プレーンテキストが含まれますが、画像や他のテーブルなどの他のコンテンツはテーブルセルに配置できま

テーブルへのテキストまたはその他のコンテンツの追加は、DocumentBuilderクラスの適切なメソッドを使用して実行され、**“Create a Table”**の記事で説明されています。 この記事では、既存のテーブルのテキストを操作する方法について説明します。

テーブル内のテキストを置換する

テーブルはAspose.Words内の他のノードと同様に、Rangeオブジェクトにアクセスできます。 Table rangeオブジェクトを使用すると、テーブル内のテキストを置き換えることができます。

置換時に特殊文字を使用する機能は現在サポートされているため、既存のテキストを複数段落のテキストに置き換えることができます。 これを行うには、対応するReplaceメソッドで説明されている特別なメタ文字を使用する必要があります。

次のコード例は、テーブル全体のセル内のテキスト文字列のすべてのインスタンスを置き換える方法を示しています:

// 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);
table.getRange().replace("Carrots", "Eggs", new FindReplaceOptions(FindReplaceDirection.FORWARD));
table.getLastRow().getLastCell().getRange().replace("50", "20", new FindReplaceOptions(FindReplaceDirection.FORWARD));
doc.save(getArtifactsDir() + "FindAndReplace.ReplaceTextInTable.docx");

表からプレーンテキストを抽出する

Rangeオブジェクトを使用すると、テーブル範囲全体のメソッドを呼び出して、テーブルをプレーンテキストとして抽出することもできます。 これを行うには、Textプロパティを使用します。

次のコード例は、テーブルのテキスト範囲を印刷する方法を示しています:

// 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);
// The range text will include control characters such as "\a" for a cell.
// You can call ToString and pass SaveFormat.Text on the desired node to find the plain text content.
System.out.println("Contents of the table: ");
System.out.println(table.getRange().getText());

次のコード例は、行要素とテーブル要素のテキスト範囲を印刷する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
System.out.println("\nContents of the row: ");
System.out.println(table.getRows().get(1).getRange().getText());
System.out.println("\nContents of the cell: ");
System.out.println(table.getLastRow().getLastCell().getRange().getText());

代替表テキストの操作

Microsoft Wordテーブルには、テーブルに含まれる情報の代替テキスト表現を提供するtable titletable descriptionがあります。

Aspose.Wordsでは、TitleプロパティとDescriptionプロパティを使用してテーブルのタイトルと説明を追加することもできます。 これらのプロパティはISO/IEC29500に準拠するDOCX文書にとって意味があります。 ISO/IEC29500より前の形式で保存する場合、これらのプロパティは無視されます。

テーブルのtitleプロパティとdescriptionプロパティを設定する方法を次のコード例に示します:

// 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);
table.setTitle("Test title");
table.setDescription("Test description");
OoxmlSaveOptions options = new OoxmlSaveOptions(); { options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.getCompatibilityOptions().optimizeFor(com.aspose.words.MsWordVersion.WORD_2016);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.SetTableTitleAndDescription.docx", options);