Java를 사용하여 프레젠테이션에서 표 셀 관리
개요
Aspose.Slides를 사용하면 PowerPoint 프레젠테이션에서 표 셀에 접근하고 수정할 수 있습니다. 이 문서에서는 병합된 표 셀을 식별하는 방법, 셀 테두리를 제거하는 방법, 셀을 병합하거나 분할한 후 셀 번호 매기기를 다루는 방법, 셀 배경색을 변경하는 방법, 그리고 표 셀 안에 이미지를 추가하는 방법을 설명합니다. 예제에서는 프레젠테이션을 만들거나 열고, 슬라이드에서 표를 가져오고, 셀 속성을 통해 셀 서식을 업데이트하고, 수정된 프레젠테이션을 PPTX 파일로 저장하는 과정을 보여줍니다.
병합된 테이블 셀 식별
- Create an instance of the 프레젠테이션 클래스.
- 첫 슬라이드에서 표를 가져옵니다.
- 표의 행과 열을 반복하여 병합된 셀을 찾습니다.
- 병합된 셀이 발견될 때 메시지를 출력합니다.
Presentation pres = new Presentation("SomePresentationWithTable.pptx");
try {
ITable table = (ITable)pres.getSlides().get_Item(0).getShapes().get_Item(0); // Slide#0.Shape#0이 표라고 가정
for (int i = 0; i < table.getRows().size(); i++)
{
for (int j = 0; j < table.getColumns().size(); j++)
{
ICell currentCell = table.getRows().get_Item(i).get_Item(j);
if (currentCell.isMergedCell())
{
System.out.println(String.format("Cell %d;%d is a part of merged cell with RowSpan=%d and ColSpan=%d starting from Cell %d;%d.",
i, j, currentCell.getRowSpan(), currentCell.getColSpan(), currentCell.getFirstRowIndex(), currentCell.getFirstColumnIndex()));
}
}
}
} finally {
if (pres != null) pres.dispose();
}
테이블 셀 테두리 제거
- Create an instance of the 프레젠테이션 클래스.
- 인덱스를 통해 슬라이드 참조를 가져옵니다.
- 너비가 지정된 열 배열을 정의합니다.
- 높이가 지정된 행 배열을 정의합니다.
- Add a table to the slide through the addTable 메서드.
- 모든 셀을 순회하면서 위, 아래, 오른쪽, 왼쪽 테두리를 지웁니다.
- 수정된 프레젠테이션을 PPTX 파일로 저장합니다.
// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
// 첫 번째 슬라이드에 접근합니다
Slide sld = (Slide)pres.getSlides().get_Item(0);
// 열을 너비로, 행을 높이로 정의합니다
double[] dblCols = { 50, 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };
// 슬라이드에 표 모양을 추가합니다
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// 각 셀에 대한 테두리 형식을 설정합니다
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.NoFill);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.NoFill);
}
}
// PPTX를 디스크에 저장합니다
pres.save("table_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
병합된 셀의 번호 매기기
두 쌍의 셀 (1,1) x (2,1) 및 (1,2) x (2,2)를 병합하면 결과 표에 번호가 매겨집니다. 이 Java 코드는 그 과정을 보여줍니다:
// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
// 첫 번째 슬라이드에 접근합니다
ISlide sld = pres.getSlides().get_Item(0);
// 열을 너비로, 행을 높이로 정의합니다
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// 슬라이드에 표 모양을 추가합니다
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// 각 셀에 대한 테두리 형식을 설정합니다
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// 셀 (1, 1) x (2, 1)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// 셀 (1, 2) x (2, 2)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
pres.save("MergeCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
그 후 (1,1)과 (1,2)를 병합하여 셀을 추가로 병합합니다. 결과는 중앙에 큰 병합 셀이 포함된 표가 됩니다:
// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
// 첫 번째 슬라이드에 접근합니다
ISlide sld = pres.getSlides().get_Item(0);
// 열을 너비로, 행을 높이로 정의합니다
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// 슬라이드에 표 모양을 추가합니다
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// 각 셀에 대한 테두리 형식을 설정합니다
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// 셀 (1, 1) x (2, 1)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// 셀 (1, 2) x (2, 2)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
// 셀 (1, 1) x (1, 2)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(1, 2), true);
// PPTX 파일을 디스크에 저장합니다
pres.save("MergeCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
분할된 셀의 번호 매기기
이전 예제에서는 표 셀을 병합할 때 다른 셀의 번호 매기기 체계가 변하지 않았습니다.
이번에는 병합된 셀이 없는 일반 표를 사용하고 셀 (1,1)을 분할하여 특수한 표를 만들려고 합니다. 이 표의 번호 매기기가 이상하게 보일 수 있으니 주의하십시오. 그러나 이것이 Microsoft PowerPoint가 표 셀에 번호를 매기는 방식이며 Aspose.Slides도 동일하게 동작합니다.
다음 Java 코드는 설명한 과정을 보여줍니다:
// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
// 첫 번째 슬라이드에 접근합니다
ISlide sld = pres.getSlides().get_Item(0);
// 열을 너비로, 행을 높이로 정의합니다
double[] dblCols = { 70, 70, 70, 70 };
double[] dblRows = { 70, 70, 70, 70 };
// 슬라이드에 표 모양을 추가합니다
ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);
// 각 셀에 대한 테두리 형식을 설정합니다
for (IRow row : tbl.getRows())
{
for (ICell cell : row)
{
cell.getCellFormat().getBorderTop().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderTop().setWidth(5);
cell.getCellFormat().getBorderBottom().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderBottom().setWidth(5);
cell.getCellFormat().getBorderLeft().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderLeft().setWidth(5);
cell.getCellFormat().getBorderRight().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
cell.getCellFormat().getBorderRight().setWidth(5);
}
}
// 셀 (1, 1) x (2, 1)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 1), tbl.get_Item(2, 1), false);
// 셀 (1, 2) x (2, 2)을 병합합니다
tbl.mergeCells(tbl.get_Item(1, 2), tbl.get_Item(2, 2), false);
// 셀 (1, 1)을 분할합니다
tbl.get_Item(1, 1).splitByWidth(tbl.get_Item(2, 1).getWidth() / 2);
//PPTX 파일을 디스크에 저장합니다
pres.save("SplitCells_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
표 셀 배경색 변경
다음 Java 코드는 표 셀의 배경색을 변경하는 방법을 보여줍니다:
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
double[] dblCols = { 150, 150, 150, 150 };
double[] dblRows = { 50, 50, 50, 50, 50 };
// 새 테이블을 생성합니다
ITable table = slide.getShapes().addTable(50, 50, dblCols, dblRows);
// 셀의 배경색을 설정합니다
ICell cell = table.get_Item(2, 3);
cell.getCellFormat().getFillFormat().setFillType(FillType.Solid);
cell.getCellFormat().getFillFormat().getSolidFillColor().setColor(Color.RED);
presentation.save("cell_background_color.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null) presentation.dispose();
}
표 셀 안에 이미지 추가
- Create an instance of the 프레젠테이션 클래스.
- 인덱스를 통해 슬라이드 참조를 가져옵니다.
- 너비가 지정된 열 배열을 정의합니다.
- 높이가 지정된 행 배열을 정의합니다.
- Add a table to the slide through the AddTable 메서드.
Images객체를 생성하여 이미지 파일을 보관합니다.IImage이미지를IPPImage객체에 추가합니다.- 표 셀의
FillFormat을Picture로 설정합니다. - 이미지를 표의 첫 번째 셀에 추가합니다.
- 수정된 프레젠테이션을 PPTX 파일로 저장합니다.
// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
// 첫 번째 슬라이드에 접근합니다
ISlide islide = pres.getSlides().get_Item(0);
// 열을 너비로, 행을 높이로 정의합니다
double[] dblCols = {150, 150, 150, 150};
double[] dblRows = {100, 100, 100, 100, 90};
// 슬라이드에 표 모양을 추가합니다
ITable tbl = islide.getShapes().addTable(50, 50, dblCols, dblRows);
// 이미지 파일을 사용하여 IPPImage 객체를 생성합니다
IPPImage picture;
IImage image = Images.fromFile("image.jpg");
try {
picture = pres.getImages().addImage(image);
} finally {
if (image != null) image.dispose();
}
// 이미지를 첫 번째 표 셀에 추가합니다
ICellFormat cellFormat = tbl.get_Item(0, 0).getCellFormat();
cellFormat.getFillFormat().setFillType(FillType.Picture);
cellFormat.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);
cellFormat.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);
// PPTX 파일을 디스크에 저장합니다
pres.save("Image_In_TableCell_out.pptx", SaveFormat.Pptx);
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
FAQ
단일 셀의 각 면에 대해 서로 다른 선 굵기와 스타일을 설정할 수 있나요?
예. 위, 아래, 왼쪽, 오른쪽 테두리는 각각 별도의 속성을 가지므로 각 면의 굵기와 스타일을 다르게 지정할 수 있습니다. 이는 문서에서 보여준 셀별 테두리 제어와 논리적으로 일치합니다.
셀 배경에 그림을 설정한 후 열/행 크기를 변경하면 이미지가 어떻게 됩니까?
동작은 fill mode (stretch/tile)에 따라 달라집니다. Stretch 모드에서는 이미지가 새로운 셀 크기에 맞게 조정되고, Tile 모드에서는 타일이 다시 계산됩니다. 본 문서에서는 셀 내 이미지 표시 모드에 대해 언급하고 있습니다.
셀의 모든 콘텐츠에 하이퍼링크를 지정할 수 있나요?
Hyperlinks는 셀의 텍스트 프레임 내 텍스트(구간) 수준이나 전체 표/도형 수준에서 설정됩니다. 실제로는 구간에 링크를 지정하거나 셀 전체 텍스트에 링크를 지정합니다.
단일 셀 내에서 서로 다른 글꼴을 설정할 수 있나요?
예. 셀의 텍스트 프레임은 portions (런) 별로 독립적인 서식(글꼴 패밀리, 스타일, 크기, 색상)을 지원합니다.