چگونه متن را در C++ قالب‌بندی کنیم

چگونه متن را در C++ قالب‌بندی کنیم

Aspose.Slides FOSS for C++ امکان قالب‌بندی دقیق متن را از طریق کلاس PortionFormat فراهم می‌کند. یک 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 .

مرحله ۲: افزودن یک شکل با قاب متن

قبل از قالب‌بندی متن، یک شکل اضافه کنید و محتوای متنی آن را از طریق 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()) است.


مرحله ۴: اعمال قالب‌بندی بولد و ایتالیک

از 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;
}

مرحله 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::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::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

همچنین ببینید

 فارسی