Cách định dạng văn bản bằng C++

Cách định dạng văn bản bằng C++

Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat lớp. A Portion là đơn vị độc lập nhỏ nhất của văn bản; nó tương ứng với một chuỗi định dạng duy nhất trong một đoạn văn. Hướng dẫn này cho thấy cách áp dụng định dạng in đậm, in nghiêng, kích thước phông chữ và màu sắc cho văn bản trong một bản trình chiếu.

Hướng Dẫn Từng Bước

Bước 1: Xây dựng và Liên kết Thư viện

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 .

Bước 2: Thêm một Hình dạng với Khung Văn bản

Trước khi định dạng văn bản, hãy thêm một hình dạng và đặt nội dung văn bản của nó qua 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;
}

Bước 3: Truy cập TextFrame

shape.text_frame() trả về một con trỏ tới shape’s TextFrame. Sử dụng -> để gọi các phương thức trên nó.

auto* tf = shape.text_frame();          // pointer to the shape's text frame
tf->set_text("your text here");

Một TextFrame chứa Paragraph các đối tượng (tf->paragraphs()). Mỗi Paragraph chứa Portion các đối tượng (paragraph.portions()).


Bước 4: Áp dụng định dạng In đậm và In nghiêng

Sử dụng portion_format().set_font_bold()portion_format().set_font_italic(). Các phương thức này chấp nhận NullableBool::TRUE, NullableBool::FALSE, hoặc NullableBool::NOT_DEFINED (kế thừa từ 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;
}

Bước 5: Đặt Kích thước Phông chữ và Màu sắc

Đặt portion_format().set_font_height() cho kích thước (đơn vị điểm) và sử dụng fill_format() cho màu.

#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) chấp nhận các giá trị 0-255 cho mỗi kênh.


Bước 6: Nhiều Portion trong một Đoạn văn

Một đoạn văn duy nhất có thể chứa nhiều phần với định dạng khác nhau. Thêm một Portion vào portions() bộ sưu tập:

#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;
}

Các Vấn Đề Thường Gặp và Cách Khắc Phục

Văn bản vẫn hiển thị màu đen ngay cả sau khi đã đặt màu

Đảm bảo fill_format().set_fill_type(FillType::SOLID) được đặt trước khi gán màu. Nếu không đặt loại tô, việc thay đổi màu có thể không có hiệu lực.

NullableBool::TRUE vs true

portion_format().set_font_bold() mong đợi NullableBool::TRUE, không phải C++ true. Truyền true trực tiếp sẽ không biên dịch được hoặc sẽ có hành vi không xác định tùy thuộc vào quá trình giải quyết quá tải.

Phông chữ không xuất hiện trong tệp đã lưu

Cái set_latin_font() phương thức đặt họ phông chữ Latin. Nếu không được đặt, phông chữ của giao diện trình chiếu sẽ được sử dụng. Phông chữ tùy chỉnh phải được nhúng hoặc có sẵn trên máy xem.


Câu hỏi thường gặp

Làm thế nào để thay đổi họ phông chữ?

Đặt portion_format().set_latin_font():

fmt.set_latin_font(asf::FontData("Arial"));

FontData chấp nhận tên họ phông chữ dưới dạng chuỗi.

Làm thế nào để thiết lập căn chỉnh đoạn văn?

Sử dụng paragraph_format().set_alignment():

tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);

Các giá trị được hỗ trợ: LEFT, CENTER, RIGHT, JUSTIFY.

Làm thế nào để thiết lập khoảng cách dòng?

Sử dụng paragraph_format().set_space_before() (điểm trước đoạn) hoặc paragraph_format().set_space_after() (điểm sau đoạn):

tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6);   // 6pt after

Xem thêm

 Tiếng Việt