چگونه متن را در C++ قالببندی کنیم
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat کلاس. A Portion کوچکترین واحد مستقل متن است؛ به یک اجرای قالببندی تک در یک پاراگراف نگاشت میشود. این راهنما نشان میدهد چگونه قالببندی bold، italic، font size و color را بر متن در یک ارائه اعمال کنید.
راهنمای گام به گام
مرحله ۱: ساخت و لینک کردن کتابخانه
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 .مرحله ۲: افزودن یک شکل با یک قاب متن
قبل از قالببندی متن، یک shape اضافه کنید و محتوای متنی آن را از طریق shape.text_frame()->set_text().
#include <Aspose/Slides/Foss/presentation.h>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0];
auto& shape = slide.shapes().add_auto_shape(
asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
shape.text_frame()->set_text("Default text: will be formatted");
prs.save("output.pptx", asf::SaveFormat::PPTX);
return 0;
}مرحله ۳: دسترسی به TextFrame
shape.text_frame() یک pointer به shape’s برمیگرداند TextFrame. استفاده کنید -> برای فراخوانی methods روی آن.
auto* tf = shape.text_frame(); // pointer to the shape's text frame
tf->set_text("your text here");یک TextFrame حاوی Paragraph اشیاء (tf->paragraphs()). هر Paragraph شامل Portion اشیاء (paragraph.portions()).
مرحله ۴: اعمال قالببندی Bold و Italic
استفاده کنید portion_format().set_font_bold() و portion_format().set_font_italic(). این متدها میپذیرند NullableBool::TRUE, NullableBool::FALSE, یا NullableBool::NOT_DEFINED (از master به ارث میبرد).
#include <Aspose/Slides/Foss/presentation.h>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0];
auto& shape = slide.shapes().add_auto_shape(
asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
shape.text_frame()->set_text("Bold and italic text");
auto* tf = shape.text_frame();
auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
fmt.set_font_bold(asf::NullableBool::TRUE);
fmt.set_font_italic(asf::NullableBool::TRUE);
prs.save("bold-italic.pptx", asf::SaveFormat::PPTX);
return 0;
}مرحله ۵: تنظیم اندازه قلم و رنگ
تنظیم portion_format().set_font_height() برای اندازه (به نقطه) و استفاده fill_format() برای رنگ.
#include <Aspose/Slides/Foss/presentation.h>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0];
auto& shape = slide.shapes().add_auto_shape(
asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
shape.text_frame()->set_text("Large corporate-blue heading");
auto* tf = shape.text_frame();
auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
fmt.set_font_height(32); // 32pt font
fmt.set_font_bold(asf::NullableBool::TRUE);
fmt.fill_format().set_fill_type(asf::FillType::SOLID);
fmt.fill_format().solid_fill_color().set_color(
asf::Color::from_argb(255, 0, 70, 127));
prs.save("colored-text.pptx", asf::SaveFormat::PPTX);
return 0;
}Color::from_argb(alpha, red, green, blue) مقادیر 0-255 را برای هر کانال میپذیرد.
مرحله ۶: چندین Portion در یک پاراگراف
یک پاراگراف میتواند شامل چندین بخش با قالببندیهای مختلف باشد. یک مورد جدید Portion به یک پاراگراف’s portions() مجموعه:
#include <Aspose/Slides/Foss/presentation.h>
int main() {
namespace asf = Aspose::Slides::Foss;
asf::Presentation prs;
auto& slide = prs.slides()[0];
auto& shape = slide.shapes().add_auto_shape(
asf::ShapeType::RECTANGLE, 50, 50, 600, 100);
shape.text_frame()->set_text(""); // start with empty text
auto* tf = shape.text_frame();
auto& paragraph = tf->paragraphs()[0];
// First portion: normal text
auto& portion1 = paragraph.portions()[0];
portion1.set_text("Normal text followed by ");
portion1.portion_format().set_font_height(20);
// Second portion: bold red text
asf::Portion portion2;
portion2.set_text("bold red text");
portion2.portion_format().set_font_height(20);
portion2.portion_format().set_font_bold(asf::NullableBool::TRUE);
portion2.portion_format().fill_format().set_fill_type(asf::FillType::SOLID);
portion2.portion_format().fill_format().solid_fill_color().set_color(
asf::Color::from_argb(255, 200, 0, 0));
paragraph.portions().add(portion2);
prs.save("mixed-format.pptx", asf::SaveFormat::PPTX);
return 0;
}مشکلات رایج و راهحلها
متن پس از تنظیم رنگ همچنان بهصورت سیاه نمایش داده میشود
اطمینان حاصل کنید fill_format().set_fill_type(FillType::SOLID) قبل از اختصاص رنگ تنظیم شود. بدون تنظیم نوع پر کردن، ممکن است تغییر رنگ تأثیری نداشته باشد.
NullableBool::TRUE در مقابل true
portion_format().set_font_bold() انتظار دارد NullableBool::TRUE,، نه C++ true. ارسال true بهصورت مستقیم کامپایل نخواهد شد یا بسته به حل بارگذاری، رفتار تعریفنشدهای خواهد داشت.
قلم در فایل ذخیرهشده ظاهر نمیشود
این set_latin_font() متد خانوادهٔ قلم لاتین را تنظیم میکند. اگر تنظیم نشود، قلم تم ارائه استفاده میشود. قلمهای سفارشی باید جاسازی شوند یا در دستگاه مشاهدهکننده موجود باشند.
سوالات متداول
چگونه میتوانم خانوادهٔ قلم را تغییر دهم؟?
تنظیم portion_format().set_latin_font():
fmt.set_latin_font(asf::FontData("Arial"));FontData نام خانوادهٔ قلم را بهصورت رشته میپذیرد.
چگونه میتوانم تراز پاراگراف را تنظیم کنم؟?
استفاده paragraph_format().set_alignment():
tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);مقادیر پشتیبانیشده: LEFT, CENTER, RIGHT, JUSTIFY.
چگونه میتوانم فاصلهٔ خطوط را تنظیم کنم؟?
استفاده paragraph_format().set_space_before() (نقطهها قبل از پاراگراف) یا paragraph_format().set_space_after() (نقطهها بعد از پاراگراف):
tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6); // 6pt after