C++ ile PowerPoint'e Şekil Ekleme

C++ ile PowerPoint'e Şekil Ekleme

Aspose.Slides FOSS for C++ AutoShapes, Tables, Connectors ve PictureFrames’i sunum slaytlarına eklemeyi destekler. Tüm şekil türleri slide.shapes() koleksiyonu aracılığıyla eklenir.

Adım Adım Kılavuz

Adım 1: Kütüphaneyi Derle ve Bağla

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 .

Adım 2: Sunum Oluştur

RAII temizliği için yığın tahsisi kullanın.

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

Adım 3: Bir AutoShape ekleyin

slide.shapes().add_auto_shape(shape_type, x, y, width, height) verilen konum ve boyutta (tümü puan cinsinden) bir şekil yerleştirir. Şekli seçmek için ShapeType sabitlerini kullanın.

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

Adım 4: Tablo Ekle

slide.shapes().add_table(x, y, col_widths, row_heights) belirtilen konumda bir tablo oluşturur. Sütun genişlikleri ve satır yükseklikleri nokta değerlerinden oluşan vektörlerdir.

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

Adım 5: Bağlayıcı Ekle

Bağlayıcılar iki şekli görsel olarak bağlar. Şekilleri önce oluşturun, ardından bir bağlayıcı ekleyin ve başlangıç ve bitiş bağlantı noktalarını ayarlayın.

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

Bağlantı noktası indeksleri bir dikdörtgen için 0‑3 olarak numaralandırılır: üst=0, sol=1, alt=2, sağ=3.


Adım 6: Bir Resim Çerçevesi Ekle

Bir resmi gömün ve slayta PictureFrame olarak ekleyin. Önce resim baytlarını okuyun, bunları sunumun resim koleksiyonuna ekleyin, ardından çerçeveyi oluşturun.

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

Yaygın Sorunlar ve Çözümler

Şekil görünür slayt alanının dışında görünüyor

Slaytlar varsayılan olarak 720 x 540 puandır. x veya y değerleri bu sınırların dışına çıktığında şekil slayt dışına yerleştirilir. x < 720 ve y < 540 tutun ve x + width <= 720 ve y + height <= 540 sağlandığından emin olun.

add_auto_shape() yok edilmiş bir nesneye referans döndürür

Presentation ömrünün ötesinde referansları saklamayın. Tüm alt nesneler Presentation tarafından sahiplenilir ve yok edildiğinde geçersiz hale gelir.

Tablo hücresi metni atamadan sonra boş

Doğru yöntem .text_frame()->set_text()‘dir. Hücrelere table.rows()[row_index][col_index].text_frame()->set_text("value") olarak erişin.


Sıkça Sorulan Sorular

Bir slayta kaç şekil ekleyebilirim?

Kütüphane tarafından uygulanan bir sınırlama yoktur. Pratik sınırlamalar, dosya boyutuna ve hedef PPTX görüntüleyicinizin renderleme yeteneğine bağlıdır.

Bir şeklin konumunu ekledikten sonra değiştirebilir miyim?

Evet. add_auto_shape() tarafından döndürülen şekil nesnesi set_x(), set_y(), set_width() ve set_height() yöntemlerine sahiptir:

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

Şekil anahat (kenarlık) rengini ayarlayabilir miyim?

Evet, shape.line_format() aracılığıyla:

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

Grafikler destekleniyor mu?

Hayır. Çizelgeler, SmartArt ve OLE nesneleri bu sürümde uygulanmadı.


Ayrıca Bakınız

 Türkçe