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

Step 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: コネクタを追加

コネクタは2つのシェイプを視覚的にリンクします。まずシェイプを作成し、次にコネクタを追加して開始点と終了点の接続ポイントを設定します。

#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 に番号付けされます: 上=0、左=1、下=2、右=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 オブジェクトは実装されていません。


参照

 日本語