Kaip pridėti formas į PowerPoint naudojant C++
Aspose.Slides FOSS for C++ palaiko AutoShapes, Tables, Connectors ir PictureFrames pridėjimą į pristatymo skaidres. Visi formų tipai pridedami per slide.shapes() kolekciją.
Žingsnis po žingsnio vadovas
Žingsnis 1: sukurti ir susieti biblioteką
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 .Žingsnis 2: Sukurti prezentaciją
Naudokite steko paskirstymą RAII valymui.
#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;
}Žingsnis 3: Pridėti AutoShape
slide.shapes().add_auto_shape(shape_type, x, y, width, height) patalpina formą nurodytoje vietoje ir dydyje (visi taškais). Naudokite ShapeType konstantas, kad pasirinktumėte formą.
#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;
}Žingsnis 4: Pridėti lentelę
slide.shapes().add_table(x, y, col_widths, row_heights) sukuria lentelę nurodytoje vietoje. Stulpelių plotiai ir eilučių aukščiai yra taškų verčių vektoriai.
#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 žingsnis: Pridėti jungiklį
Jungikliai vizualiai sujungia dvi figūras. Pirma sukurkite figūras, tada pridėkite jungiklį ir nustatykite jo pradžios ir pabaigos prisijungimo taškus.
#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;
}Jungties vietų indeksai yra sunumeruoti 0-3 stačiakampiui: viršus=0, kairė=1, apačia=2, dešinė=3.
Žingsnis 6: Pridėti paveikslėlio rėmelį
Įterpkite image ir pridėkite jį į slide kaip PictureFrame. Pirmiausia perskaitykite image bytes, pridėkite juos į presentation’s image collection, tada sukurkite frame.
#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;
}Dažnos problemos ir sprendimai
Figūra atsiranda už matomo skaidrės ploto
Skaidrės pagal numatytuosius nustatymus yra 720 × 540 taškų. x arba y reikšmės, viršijančios šias ribas, padeda formą už skaidrės ribų. Išlaikykite x < 720 ir y < 540, ir užtikrinkite x + width <= 720 ir y + height <= 540.
add_auto_shape() grąžina nuorodą į sunaikintą objektą
Nelaikykite nuorodų ilgiau nei Presentation gyvavimo trukmė. Visi vaikiniai objektai priklauso Presentation ir yra panaikinami sunaikinimo metu.
Lentelės langelio tekstas po priskyrimo yra tuščias
Teisingas metodas yra .text_frame()->set_text(). Pasiekite langelius kaip table.rows()[row_index][col_index].text_frame()->set_text("value").
Dažniausiai užduodami klausimai
Kiek figūrų galiu pridėti į skaidrę?
Nėra bibliotekos nustatyto apribojimo. Praktiniai apribojimai priklauso nuo failo dydžio ir jūsų tikslo PPTX peržiūros programos atvaizdavimo galimybių.
Ar galiu pakeisti figūros padėtį po jos pridėjimo?
Taip. Formos objektas, grąžintas add_auto_shape(), turi set_x(), set_y(), set_width() ir set_height() metodus:
shape.set_x(100);
shape.set_y(200);
shape.set_width(400);
shape.set_height(80);Ar galiu nustatyti figūros kontūro (rėmo) spalvą?
Taip, per shape.line_format():
shape.line_format().fill_format().solid_fill_color().set_color(
asf::Color::from_argb(255, 200, 0, 0));Ar diagramos palaikomos?
Ne. Diagramos, SmartArt ir OLE objektai šioje leidimo versijoje nėra įgyvendinti.