.NET でのプレゼンテーションにおけるテーブルセルの管理
結合されたテーブルセルの識別
- Presentation クラスのインスタンスを作成します。
- 最初のスライドからテーブルを取得します。
- テーブルの行と列を反復処理して結合セルを探します。
- 結合セルが見つかったときにメッセージを出力します。
この C# コードは、プレゼンテーション内で結合されたテーブルセルを識別する方法を示しています:
using (Presentation pres = new Presentation("SomePresentationWithTable.pptx"))
{
ITable table = pres.Slides[0].Shapes[0] as ITable; // Slide#0.Shape#0 がテーブルであると想定しています
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
ICell currentCell = table.Rows[i][j];
if (currentCell.IsMergedCell)
{
Console.WriteLine(string.Format("Cell {0};{1} is a part of merged cell with RowSpan={2} and ColSpan={3} starting from Cell {4};{5}.",
i, j, currentCell.RowSpan, currentCell.ColSpan, currentCell.FirstRowIndex, currentCell.FirstColumnIndex));
}
}
}
}
テーブルセルの枠線を削除する
Presentationクラスのインスタンスを作成します。- インデックスを使用してスライドの参照を取得します。
- 幅を指定した列の配列を定義します。
- 高さを指定した行の配列を定義します。
AddTableメソッドを使用してスライドにテーブルを追加します。- すべてのセルを反復処理し、上、下、右、左の枠線をクリアします。
- 変更されたプレゼンテーションを PPTX ファイルとして保存します。
この C# コードは、テーブルセルの枠線を削除する方法を示しています:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成します
using (Presentation pres = new Presentation())
{
// 最初のスライドにアクセスします
Slide sld = (Slide)pres.Slides[0];
// 列の幅と行の高さを定義します
double[] dblCols = { 50, 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };
// スライドにテーブル形状を追加します
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);
// 各セルの枠線フォーマットを設定します
foreach (IRow row in tbl.Rows)
foreach (ICell cell in row)
{
cell.CellFormat.BorderTop.FillFormat.FillType = FillType.NoFill;
cell.CellFormat.BorderBottom.FillFormat.FillType = FillType.NoFill;
cell.CellFormat.BorderLeft.FillFormat.FillType = FillType.NoFill;
cell.CellFormat.BorderRight.FillFormat.FillType = FillType.NoFill;
}
// PPTX ファイルを書き込みます
pres.Save("table_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
結合セルにおける番号付け
2 つのセルペア (1, 1) x (2, 1) と (1, 2) x (2, 2) を結合すると、結果のテーブルに番号が付けられます。この C# コードはそのプロセスを示しています:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成します
using (Presentation presentation = new Presentation())
{
// 最初のスライドにアクセスします
ISlide sld = presentation.Slides[0];
// 列の幅と行の高さを定義します
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// スライドにテーブル形状を追加します
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);
// 各セルの枠線フォーマットを設定します
foreach (IRow row in tbl.Rows)
{
foreach (ICell cell in row)
{
cell.CellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderTop.Width = 5;
cell.CellFormat.BorderBottom.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderBottom.Width = 5;
cell.CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderLeft.Width = 5;
cell.CellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderRight.Width = 5;
}
}
// セル (1, 1) と (2, 1) を結合します
tbl.MergeCells(tbl[1, 1], tbl[2, 1], false);
// セル (1, 2) と (2, 2) を結合します
tbl.MergeCells(tbl[1, 2], tbl[2, 2], false);
presentation.Save("MergeCells_out.pptx", SaveFormat.Pptx);
}
次に、(1, 1) と (1, 2) を結合してさらにセルを結合します。その結果、中央に大きな結合セルを含むテーブルが得られます:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成します
using (Presentation presentation = new Presentation())
{
// 最初のスライドにアクセスします
ISlide slide = presentation.Slides[0];
// 列の幅と行の高さを定義します
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// スライドにテーブル形状を追加します
ITable table = slide.Shapes.AddTable(100, 50, dblCols, dblRows);
// 各セルの枠線フォーマットを設定します
foreach (IRow row in table.Rows)
{
foreach (ICell cell in row)
{
cell.CellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderTop.Width = 5;
cell.CellFormat.BorderBottom.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderBottom.Width = 5;
cell.CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderLeft.Width = 5;
cell.CellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderRight.Width = 5;
}
}
// セル (1, 1) と (2, 1) を結合します
table.MergeCells(table[1, 1], table[2, 1], false);
// セル (1, 2) と (2, 2) を結合します
table.MergeCells(table[1, 2], table[2, 2], false);
// セル (1, 1) と (1, 2) を結合します
table.MergeCells(table[1, 1], table[1, 2], true);
// PPTX ファイルを書き込みます
presentation.Save("MergeCells1_out.pptx", SaveFormat.Pptx);
}
分割セルにおける番号付け
前の例では、テーブルセルが結合されたとき、他のセルの番号付けや番号体系は変わりませんでした。
今回は、結合セルのない通常のテーブルを使用し、セル (1,1) を分割して特別なテーブルを作成します。このテーブルの番号付けは奇妙に見えるかもしれませんが、これは Microsoft PowerPoint がテーブルセルに付与する番号付けの方式であり、Aspose.Slides も同様です。
この C# コードは、上記の手順を示しています:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成します
using (Presentation presentation = new Presentation())
{
// 最初のスライドにアクセスします
ISlide slide = presentation.Slides[0];
// 列の幅と行の高さを定義します
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// スライドにテーブル形状を追加します
ITable table = slide.Shapes.AddTable(100, 50, dblCols, dblRows);
// 各セルの枠線フォーマットを設定します
foreach (IRow row in table.Rows)
{
foreach (ICell cell in row)
{
cell.CellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderTop.Width = 5;
cell.CellFormat.BorderBottom.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderBottom.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderBottom.Width = 5;
cell.CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderLeft.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderLeft.Width = 5;
cell.CellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
cell.CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
cell.CellFormat.BorderRight.Width = 5;
}
}
// セル (1, 1) と (2, 1) を結合します
table.MergeCells(table[1, 1], table[2, 1], false);
// セル (1, 2) と (2, 2) を結合します
table.MergeCells(table[1, 2], table[2, 2], false);
// セル (1, 1) を分割します。
table[1, 1].SplitByWidth(table[2, 1].Width / 2);
// PPTX ファイルを書き込みます
presentation.Save("CellSplit_out.pptx", SaveFormat.Pptx);
}
テーブルセルの背景色を変更する
この C# コードは、テーブルセルの背景色を変更する方法を示しています:
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
double[] dblCols = { 150, 150, 150, 150 };
double[] dblRows = { 50, 50, 50, 50, 50 };
// 新しいテーブルを作成します
ITable table = slide.Shapes.AddTable(50, 50, dblCols, dblRows);
// セルの背景色を設定します
ICell cell = table[2, 3];
cell.CellFormat.FillFormat.FillType = FillType.Solid;
cell.CellFormat.FillFormat.SolidFillColor.Color = Color.Red;
presentation.Save("cell_background_color.pptx", SaveFormat.Pptx);
}
テーブルセル内に画像を追加する
Presentationクラスのインスタンスを作成します。- インデックスを使用してスライドの参照を取得します。
- 幅を指定した列の配列を定義します。
- 高さを指定した行の配列を定義します。
AddTableメソッドを使用してスライドにテーブルを追加します。Bitmapオブジェクトを作成して画像ファイルを保持します。IPPImageオブジェクトにビットマップ画像を追加します。- テーブルセルの
FillFormatをPictureに設定します。 - 画像をテーブルの最初のセルに追加します。
- 変更されたプレゼンテーションを PPTX ファイルとして保存します。
この C# コードは、テーブル作成時にテーブルセル内に画像を配置する方法を示しています:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成します
using (Presentation presentation = new Presentation())
{
// 最初のスライドにアクセスします
ISlide slide = presentation.Slides[0];
// 列の幅と行の高さを定義します
double[] dblCols = { 150, 150, 150, 150 };
double[] dblRows = { 100, 100, 100, 100, 90 };
// スライドにテーブル形状を追加します
ITable table = slide.Shapes.AddTable(50, 50, dblCols, dblRows);
// ファイルから画像を読み込み、プレゼンテーションのリソースに追加します
IImage image = Images.FromFile("aspose-logo.jpg");
IPPImage ppImage = presentation.Images.AddImage(image);
image.Dispose();
// 画像を最初のテーブルセルに追加します
table[0, 0].CellFormat.FillFormat.FillType = FillType.Picture;
table[0, 0].CellFormat.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
table[0, 0].CellFormat.FillFormat.PictureFillFormat.Picture.Image = ppImage;
// PPTX ファイルをディスクに保存します
presentation.Save("Image_In_TableCell_out.pptx", SaveFormat.Pptx);
}
FAQ
単一セルの各辺に異なる線の太さやスタイルを設定できますか?
はい。 top/bottom/left/right の枠線は個別のプロパティを持つため、各辺の太さやスタイルを別々に設定できます。これは、セルの各辺の枠線制御がこの記事で示されている論理的な流れに従っています。
画像をセルの背景として設定した後に列/行のサイズを変更すると画像はどうなりますか?
動作は fill mode(stretch/tile)に依存します。ストレッチの場合、画像は新しいセルに合わせて調整されます。タイルの場合、タイルは再計算されます。この記事ではセル内の画像表示モードについて言及しています。
セル内のすべてのコンテンツにハイパーリンクを割り当てられますか?
Hyperlinks はセルのテキストフレーム内のテキスト(部分)レベルまたはテーブル/シェイプ全体レベルで設定されます。実際には、部分またはセル内のすべてのテキストにリンクを割り当てます。
単一セル内で異なるフォントを設定できますか?
はい。セルのテキストフレームは、フォントファミリ、スタイル、サイズ、カラーなどを個別に設定できる portions(ラン)をサポートしています。