كيفية تنسيق النص في C++
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat الفئة A Portion هو أصغر وحدة مستقلة من النص؛ فإنه خرائط إلى تنسيق واحد يحدث في إطار الفقرة.هذا الدليل يعرض كيفية تطبيق حريص، الإيطالية، حجم الخط، وتكوين الألوان على النص في عرض.
خطوة بخطوة دليل
الخطوة 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: إضافة شكل مع إطار نصي
قبل تنسيق النص، إضافة شكل وإعداد محتوى النص من خلال 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() إعادة تشكيل الرمز إلى شكل TextFrame.استخدام -> وطلب منه أن يُدعى له طرق.
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()).
الخطوة 4: تطبيق الشجاعة والإيطالية
استخدام portion_format().set_font_bold() و portion_format().set_font_italic().هذه الأساليب تقبل NullableBool::TRUE, NullableBool::FALSE,أو NullableBool::NOT_DEFINED (أرث من الماجستير).
#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;
}الخطوة 5: إعداد حجم الصورة والألوان
مجموعة 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::Drawing::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 لكل قناة.
الخطوة 6: مضاعفة الأجزاء في الفقرة الواحدة
يمكن أن يحتوي الفقرة الواحدة على عدة أجزاء مع تنسيقات مختلفة.إضافة جديد Portion إلى الفقرة الأولى من 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::Drawing::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 vs 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