C++ में PowerPoint में आकृतियों को कैसे जोड़ें

C++ में PowerPoint में आकृतियों को कैसे जोड़ें

Aspose.Slides FOSS for C++ प्रस्तुति स्लाइड्स में AutoShapes, Tables, Connectors, और PictureFrames जोड़ने का समर्थन करता है। सभी shape प्रकार 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 तक क्रमांकित होते हैं: शीर्ष=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 < 720 और y < 540 को रखें, और x + width <= 720 और y + 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 ऑब्जेक्ट्स इस संस्करण में लागू नहीं किए गए हैं।


संबंधित देखें

 हिन्दी