Java에서 프레젠테이션 표 관리

소개

PowerPoint의 표는 정보를 표시하고 전달하는 효율적인 방법입니다. 행과 열로 배열된 셀 그리드에 있는 정보는 직관적이고 이해하기 쉽습니다.

Aspose.Slides는 Table 클래스, ITable 인터페이스, Cell 클래스, ICell 인터페이스 및 기타 유형을 제공하여 프레젠테이션에서 표를 만들고, 업데이트하고, 관리할 수 있도록 합니다.

처음부터 표 만들기

  1. Presentation 클래스의 인스턴스를 생성합니다.
  2. 인덱스를 통해 슬라이드의 참조를 가져옵니다.
  3. columnWidth 배열을 정의합니다.
  4. rowHeight 배열을 정의합니다.
  5. addTable 메서드를 사용하여 슬라이드에 ITable 객체를 추가합니다.
  6. ICell을 반복하면서 위, 아래, 오른쪽, 왼쪽 테두리 서식을 적용합니다.
  7. 표의 첫 번째 행에서 첫 번째 두 셀을 병합합니다.
  8. ICellTextFrame에 접근합니다.
  9. TextFrame에 텍스트를 추가합니다.
  10. 수정된 프레젠테이션을 저장합니다.

다음 Java 코드는 프레젠테이션에 표를 만드는 방법을 보여줍니다:

import com.aspose.slides.*;
import java.awt.Color;

// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation();
try {
    // 첫 번째 슬라이드에 접근합니다
    ISlide sld = pres.getSlides().get_Item(0);

    // 열 너비와 행 높이를 정의합니다
    double[] dblCols = {50, 50, 50};
    double[] dblRows = {50, 30, 30, 30, 30};

    // 슬라이드에 표 모양을 추가합니다
    ITable tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows);

    // 각 셀에 대한 테두리 형식을 설정합니다
    for (int row = 0; row < tbl.getRows().size(); row++)
    {
        for (int cell = 0; cell < tbl.getRows().get_Item(row).size(); cell++)
        {
            ICellFormat cellFormat = tbl.getRows().get_Item(row).get_Item(cell).getCellFormat();
            
            cellFormat.getBorderTop().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderTop().setWidth(5);

            cellFormat.getBorderBottom().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderBottom().setWidth(5);

            cellFormat.getBorderLeft().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderLeft().setWidth(5);

            cellFormat.getBorderRight().getFillFormat().setFillType(FillType.Solid);
            cellFormat.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color.RED);
            cellFormat.getBorderRight().setWidth(5);
        }
    }
    // 행 1의 셀 1과 2를 병합합니다
    tbl.mergeCells(tbl.getRows().get_Item(0).get_Item(0), tbl.getRows().get_Item(0).get_Item(1), false);

    // 병합된 셀에 텍스트를 추가합니다
    tbl.getRows().get_Item(0).get_Item(0).getTextFrame().setText("Merged Cells");

    // 프레젠테이션을 디스크에 저장합니다
    pres.save("table.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

표의 기본 번호 매기기

표에서 셀 번호 매기기는 직관적이며 0부터 시작합니다. 표의 첫 번째 셀은 0,0 (열 0, 행 0)으로 인덱싱됩니다.

예를 들어, 4열 4행 표의 셀은 다음과 같이 번호가 매겨집니다:

(0, 0) (1, 0) (2, 0) (3, 0)
(0, 1) (1, 1) (2, 1) (3, 1)
(0, 2) (1, 2) (2, 2) (3, 2)
(0, 3) (1, 3) (2, 3) (3, 3)

다음 Java 코드는 표의 셀 번호를 지정하는 방법을 보여줍니다:

import com.aspose.slides.*;
import java.awt.Color;

// 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);
        }
    }

    // 프레젠테이션을 디스크에 저장합니다
    pres.save("StandardTables_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

기존 표 접근

  1. Presentation 클래스의 인스턴스를 생성합니다.

  2. 인덱스를 통해 표가 포함된 슬라이드에 대한 참조를 가져옵니다.

  3. ITable 객체를 만들고 null로 초기화합니다.

  4. 모든 IShape 객체를 반복하면서 표를 찾을 때까지 탐색합니다.

    슬라이드에 단일 표만 포함된 것으로 의심되는 경우 해당 슬라이드에 포함된 모든 쉐이프를 확인하면 됩니다. 쉐이프가 표로 식별되면 이를 Table 객체로 형변환할 수 있습니다. 그러나 슬라이드에 여러 표가 있는 경우 setAlternativeText(String value)를 사용해 필요한 표를 검색하는 것이 좋습니다.

  5. ITable 객체를 사용해 표를 조작합니다. 아래 예제에서는 표에 새 행을 추가했습니다.

  6. 수정된 프레젠테이션을 저장합니다.

다음 Java 코드는 기존 표에 접근하고 작업하는 방법을 보여줍니다:

import com.aspose.slides.*;

// PPTX 파일을 나타내는 Presentation 클래스를 인스턴스화합니다
Presentation pres = new Presentation("UpdateExistingTable.pptx");
try {

    // 첫 번째 슬라이드에 접근합니다
    ISlide sld = pres.getSlides().get_Item(0);

    // null TableEx를 초기화합니다
    ITable tbl = null;

    // 모양들을 반복하며 찾은 표에 대한 참조를 설정합니다
    for (IShape shp : sld.getShapes()) 
    {
        if (shp instanceof ITable) 
        {
            tbl = (ITable) shp;
            // 두 번째 행의 첫 번째 열에 텍스트를 설정합니다
            tbl.get_Item(0, 1).getTextFrame().setText("New");
        }
    }
    
    // 수정된 프레젠테이션을 디스크에 저장합니다
    pres.save("table1_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

텍스트 프레임을 보유한 셀 찾기

표에서 ITextFrame을 받는 일반 텍스트 처리 코드에서는 ITextFrame.getParentCell 메서드를 사용해 해당 셀(ICell)을 가져옵니다. 표 셀의 텍스트 프레임에 대해 ITextFrame.getParentCell은 소유자를 반환하고, ITextFrame.getParentShapenull을 반환합니다. 이는 표 자체가 쉐이프이지만 텍스트 프레임이 셀에 직접 연결되어 있기 때문입니다.

읽기 전용 ICell.getFirstColumnIndexICell.getFirstRowIndex 메서드를 통해 셀 좌표에 접근할 수 있습니다. ITextFrame.getParentCell 또한 읽기 전용 탐색을 제공하며, 반환된 소유자를 변경하지 않습니다. 사용하기 전에 항상 반환된 셀이 null인지 확인하십시오.

표 셀 및 쉐이프 소유자를 식별하는 전체 예제(스마트아트 노드와 연결된 쉐이프 포함)는 Search and Replace Text를 참조하십시오.

표 안의 텍스트 정렬

  1. Presentation 클래스의 인스턴스를 생성합니다.
  2. 인덱스를 통해 슬라이드의 참조를 가져옵니다.
  3. 슬라이드에 ITable 객체를 추가합니다.
  4. 표에서 ITextFrame 객체에 접근합니다.
  5. ITextFrameIParagraph에 접근합니다.
  6. 텍스트를 수직으로 정렬합니다.
  7. 수정된 프레젠테이션을 저장합니다.

다음 Java 코드는 표 안의 텍스트를 정렬하는 방법을 보여줍니다:

import com.aspose.slides.*;
import java.awt.Color;

// Presentation 클래스의 인스턴스를 생성합니다
Presentation pres = new Presentation();
try {
    // 첫 번째 슬라이드를 가져옵니다 
    ISlide slide = pres.getSlides().get_Item(0);
    
    // 열 너비와 행 높이를 정의합니다
    double[] dblCols = { 120, 120, 120, 120 };
    double[] dblRows = { 100, 100, 100, 100 };
    
    // 슬라이드에 표 모양을 추가합니다
    ITable tbl = slide.getShapes().addTable(100, 50, dblCols, dblRows);
    tbl.get_Item(1, 0).getTextFrame().setText("10");
    tbl.get_Item(2, 0).getTextFrame().setText("20");
    tbl.get_Item(3, 0).getTextFrame().setText("30");
    
    // 텍스트 프레임에 접근합니다
    ITextFrame txtFrame = tbl.get_Item(0, 0).getTextFrame();
    
    // 텍스트 프레임용 Paragraph 객체를 생성합니다
    IParagraph paragraph = txtFrame.getParagraphs().get_Item(0);
    
    // Paragraph용 Portion 객체를 생성합니다
    IPortion portion = paragraph.getPortions().get_Item(0);
    portion.setText("Text here");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    
    // 텍스트를 수직으로 정렬합니다
    ICell cell = tbl.get_Item(0, 0);
    cell.setTextAnchorType(TextAnchorType.Center);
    cell.setTextVerticalType(TextVerticalType.Vertical270);
    
    // 프레젠테이션을 디스크에 저장합니다
    pres.save("Vertical_Align_Text_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

표 수준에서 텍스트 서식 지정

  1. Presentation 클래스의 인스턴스를 생성합니다.
  2. 인덱스를 통해 슬라이드의 참조를 가져옵니다.
  3. 슬라이드에서 ITable 객체에 접근합니다.
  4. 텍스트에 대해 setFontHeight(float value)를 설정합니다.
  5. setAlignment(int value)setMarginRight(float value)를 설정합니다.
  6. setTextVerticalType(byte value)를 설정합니다.
  7. 수정된 프레젠테이션을 저장합니다.

다음 Java 코드는 표 안 텍스트에 원하는 서식 옵션을 적용하는 방법을 보여줍니다:

import com.aspose.slides.*;

// Presentation 클래스의 인스턴스를 생성합니다
Presentation pres = new Presentation("simpletable.pptx");
try {
    // 첫 번째 슬라이드의 첫 번째 도형이 표라고 가정합니다
    ITable someTable = (ITable) pres.getSlides().get_Item(0).getShapes().get_Item(0);
    
    // 표 셀의 글꼴 높이를 설정합니다
    PortionFormat portionFormat = new PortionFormat();
    portionFormat.setFontHeight(25);
    someTable.setTextFormat(portionFormat);
    
    // 표 셀의 텍스트 정렬과 오른쪽 여백을 한 번에 설정합니다
    ParagraphFormat paragraphFormat = new ParagraphFormat();
    paragraphFormat.setAlignment(TextAlignment.Right);
    paragraphFormat.setMarginRight(20);
    someTable.setTextFormat(paragraphFormat);
    
    // 표 셀의 텍스트 수직 유형을 설정합니다
    TextFrameFormat textFrameFormat = new TextFrameFormat();
    textFrameFormat.setTextVerticalType(TextVerticalType.Vertical);
    someTable.setTextFormat(textFrameFormat);
    
    pres.save("result.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

표 스타일 속성 가져오기

Aspose.Slides를 사용하면 표의 스타일 속성을 검색하여 다른 표나 다른 위치에 재사용할 수 있습니다. 다음 Java 코드는 표 프리셋 스타일에서 스타일 속성을 가져오는 방법을 보여줍니다:

import com.aspose.slides.*;

Presentation pres = new Presentation();
try {
    ITable table = pres.getSlides().get_Item(0).getShapes().addTable(10, 10, new double[] { 100, 150 }, new double[] { 5, 5, 5 });
    table.setStylePreset(TableStylePreset.DarkStyle1); // 기본 스타일 프리셋 테마를 변경합니다 

    // 테이블의 스타일 프리셋을 가져옵니다
    int stylePreset = table.getStylePreset();
    System.out.println("Table style preset: " + stylePreset);

    // 가져온 스타일 프리셋을 다른 표에 적용합니다
    ITable anotherTable = pres.getSlides().get_Item(0).getShapes().addTable(10, 100, new double[] { 100, 150 }, new double[] { 5, 5, 5 });
    anotherTable.setStylePreset(stylePreset);

    pres.save("table.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

표의 가로세로 비율 잠금

기하학적 도형의 가로세로 비율은 서로 다른 차원에서의 크기 비율을 의미합니다. Aspose.Slides는 표 및 기타 도형에 대한 가로세로 비율 잠금 설정을 제공하는 setAspectRatioLocked 속성을 제공합니다.

다음 Java 코드는 표의 가로세로 비율을 잠그는 방법을 보여줍니다:

import com.aspose.slides.*;

Presentation pres = new Presentation("pres.pptx");
try {
    ITable table = (ITable)pres.getSlides().get_Item(0).getShapes().get_Item(0);
    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    table.getGraphicalObjectLock().setAspectRatioLocked(!table.getGraphicalObjectLock().getAspectRatioLocked()); // 반전

    System.out.println("Lock aspect ratio set: " + table.getGraphicalObjectLock().getAspectRatioLocked());

    pres.save("pres-out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

FAQ

전체 표와 셀 텍스트에 대해 오른쪽에서 왼쪽(RTL) 읽기 방향을 활성화할 수 있나요?

네. 표는 setRightToLeft 메서드를 제공하며, 단락은 ParagraphFormat.setRightToLeft를 가집니다. 두 메서드를 모두 사용하면 셀 내부의 RTL 순서와 렌더링이 올바르게 적용됩니다.

최종 파일에서 사용자가 표를 이동하거나 크기 조정하지 못하도록 방지하려면 어떻게 해야 하나요?

shape locks를 사용하여 이동, 크기 조정, 선택 등을 비활성화합니다. 이러한 잠금은 표에도 적용됩니다.

셀 안에 이미지를 배경으로 삽입하는 것이 지원되나요?

네. 셀에 대해 picture fill을 설정하면 이미지가 선택한 모드(늘리기 또는 타일)대로 셀 영역을 덮습니다.