C++에서 PowerPoint에 도형 추가하는 방법

C++에서 PowerPoint에 도형 추가하는 방법

Aspose.Slides FOSS for C++는 프레젠테이션 슬라이드에 AutoShapes, Tables, Connectors 및 PictureFrames를 추가하는 것을 지원합니다. 모든 도형 유형은 slide.shapes() 컬렉션을 통해 추가됩니다.

단계별 가이드

1단계: 라이브러리 빌드 및 링크

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake . && cmake --build .

2단계: 프레젠테이션 만들기

RAII 정리를 위해 스택 할당을 사용하십시오.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    // ... add shapes ...
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

3단계: AutoShape 추가

slide.shapes().add_auto_shape(shape_type, x, y, width, height)은(는) 지정된 위치와 크기(모두 포인트 단위)에 도형을 배치합니다. 도형을 선택하려면 ShapeType 상수를 사용하십시오.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    // Rectangle
    auto& rect = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 300, 100);
    rect.text_frame()->set_text("Rectangle shape");

    // Ellipse
    auto& ellipse = slide.shapes().add_auto_shape(
        asf::ShapeType::ELLIPSE, 400, 50, 200, 100);
    ellipse.text_frame()->set_text("Ellipse shape");

    prs.save("autoshapes.pptx", asf::SaveFormat::PPTX);
    return 0;
}

4단계: 표 추가

slide.shapes().add_table(x, y, col_widths, row_heights)은 지정된 위치에 표를 생성합니다. 열 너비와 행 높이는 포인트 값의 벡터입니다.

#include <Aspose/Slides/Foss/presentation.h>
#include <vector>
#include <string>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    std::vector<double> col_widths = {150.0, 150.0, 150.0};
    std::vector<double> row_heights = {40.0, 40.0, 40.0};
    auto& table = slide.shapes().add_table(50, 200, col_widths, row_heights);

    // Set header row text
    std::vector<std::string> headers = {"Product", "Units", "Revenue"};
    for (size_t col = 0; col < headers.size(); ++col) {
        table.rows()[0][col].text_frame()->set_text(headers[col]);
    }

    // Set data rows
    std::vector<std::vector<std::string>> rows = {
        {"Widget A", "120", "$2,400"},
        {"Widget B", "85",  "$1,700"},
    };
    for (size_t r = 0; r < rows.size(); ++r) {
        for (size_t c = 0; c < rows[r].size(); ++c) {
            table.rows()[r + 1][c].text_frame()->set_text(rows[r][c]);
        }
    }

    prs.save("table.pptx", asf::SaveFormat::PPTX);
    return 0;
}

5단계: 커넥터 추가

커넥터는 두 도형을 시각적으로 연결합니다. 먼저 도형을 만든 다음, 커넥터를 추가하고 시작 및 끝 연결점을 설정합니다.

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];

    auto& box1 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 100, 150, 60);
    box1.text_frame()->set_text("Start");

    auto& box2 = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 350, 100, 150, 60);
    box2.text_frame()->set_text("End");

    auto& conn = slide.shapes().add_connector(
        asf::ShapeType::BENT_CONNECTOR3, 0, 0, 10, 10);
    conn.set_start_shape_connected_to(&box1);
    conn.set_start_shape_connection_site_index(3); // right side of box1
    conn.set_end_shape_connected_to(&box2);
    conn.set_end_shape_connection_site_index(1);   // left side of box2

    prs.save("connector.pptx", asf::SaveFormat::PPTX);
    return 0;
}

연결 사이트 인덱스는 사각형에 대해 0-3으로 번호가 매겨집니다: top=0, left=1, bottom=2, right=3.


6단계: 사진 프레임 추가

이미지를 삽입하고 슬라이드에 PictureFrame 로 추가합니다. 먼저 이미지 바이트를 읽은 다음 프레젠테이션의 이미지 컬렉션에 추가하고, 그 후 프레임을 생성합니다.

#include <Aspose/Slides/Foss/presentation.h>
#include <fstream>
#include <vector>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;

    std::ifstream file("logo.png", std::ios::binary);
    std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)),
                               std::istreambuf_iterator<char>());

    auto& image = prs.images().add_image(data);

    auto& slide = prs.slides()[0];
    slide.shapes().add_picture_frame(
        asf::ShapeType::RECTANGLE, // bounding shape type
        50, 50,                    // x, y in points
        200, 150,                  // width, height in points
        image);

    prs.save("with-image.pptx", asf::SaveFormat::PPTX);
    return 0;
}

일반적인 문제 및 해결책

슬라이드 표시 영역 밖에 도형이 나타남

슬라이드는 기본적으로 720 × 540 포인트입니다. x 또는 y 값이 해당 범위를 초과하면 도형이 슬라이드 밖으로 배치됩니다. x < 720y < 540을 유지하고, x + width <= 720y + height <= 540을 보장하십시오.

add_auto_shape()은 파괴된 객체에 대한 참조를 반환합니다

Presentation의 수명보다 오래 참조를 저장하지 마십시오. 모든 자식 객체는 Presentation에 의해 소유되며 파괴 시 무효화됩니다.

테이블 셀 텍스트가 할당 후 비어 있습니다

올바른 방법은 .text_frame()->set_text()입니다. 셀에 접근하려면 table.rows()[row_index][col_index].text_frame()->set_text("value")를 사용하십시오.


자주 묻는 질문

슬라이드에 추가할 수 있는 도형은 몇 개입니까?

라이브러리에서 강제하는 제한은 없습니다. 실용적인 제한은 파일 크기와 대상 PPTX 뷰어의 렌더링 능력에 따라 달라집니다.

추가한 후에 도형의 위치를 변경할 수 있나요?

예. add_auto_shape()에서 반환된 shape 객체는 set_x(), set_y(), set_width()set_height() 메서드를 가지고 있습니다:

shape.set_x(100);
shape.set_y(200);
shape.set_width(400);
shape.set_height(80);

도형 외곽선(테두리) 색상을 설정할 수 있나요?

예, shape.line_format()을 통해:

shape.line_format().fill_format().solid_fill_color().set_color(
    asf::Color::from_argb(255, 200, 0, 0));

차트가 지원되나요?

아니요. 차트, SmartArt 및 OLE 개체는 이 버전에서 구현되지 않았습니다.


참조

 한국어