Com formatar text en C++

Com formatar text en C++

Aspose.Slides FOSS per a C++ ofereix formatació de text de gran precisió mitjançant la classe PortionFormat. Un Portion és la unitat independent més petita de text; s’associa a una única seqüència de format dins d’un paràgraf. Aquesta guia mostra com aplicar format de negreta, cursiva, mida de lletra i color al text d’una presentació.

Guia pas a pas

Pas 1: Compila i enllaça la biblioteca

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 .

Pas 2: Afegeix una forma amb un marc de text

Abans de formatar el text, afegeix una forma i estableix el seu contingut de text mitjançant 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;
}

Pas 3: Accedir al TextFrame

shape.text_frame() retorna un punter al TextFrame de la forma. Utilitzeu -> per cridar mètodes sobre aquest.

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

Un TextFrame conté Paragraph objectes (tf->paragraphs()). Cada Paragraph conté Portion objectes (paragraph.portions()).


Pas 4: Aplica el format en negreta i cursiva

Utilitzeu portion_format().set_font_bold() i portion_format().set_font_italic(). Aquests mètodes accepten NullableBool::TRUE, NullableBool::FALSE o NullableBool::NOT_DEFINED (hereten del mestre).

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

Pas 5: Estableix la mida i el color de la lletra

Estableix portion_format().set_font_height() per a la mida (en punts) i utilitza fill_format() per al color.

#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) accepta valors de 0‑255 per a cada canal.


Pas 6: Múltiples porcions en un paràgraf

Un únic paràgraf pot contenir diverses porcions amb formatatge diferent. Afegeix un nou Portion a la col·lecció portions() d’un paràgraf:

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

Problemes comuns i solucions

El text apareix negre fins i tot després d’establir el color

Assegureu que fill_format().set_fill_type(FillType::SOLID) estigui configurat abans d’assignar el color. Sense configurar el tipus d’emplenament, el canvi de color pot no tenir cap efecte.

NullableBool::TRUE vs true

portion_format().set_font_bold() espera NullableBool::TRUE, no el C++ true. Passar true directament no compilarà o tindrà un comportament indefinit segons la resolució de sobrecàrrega.

La font no apareix al fitxer desat

El mètode set_latin_font() estableix la família de fonts llatines. Si no s’estableix, s’utilitza la font del tema de la presentació. Les fonts personalitzades han d’estar incrustades o disponibles a la màquina de visualització.


Preguntes freqüents

Com canvio la família de fonts?

Estableix portion_format().set_latin_font():

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

FontData accepta el nom de la família de fonts com a cadena.

Com puc establir l’alineació del paràgraf?

Utilitza paragraph_format().set_alignment():

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

Valors compatibles: LEFT, CENTER, RIGHT, JUSTIFY.

Com puc establir l’interlineat?

Utilitzeu paragraph_format().set_space_before() (punts abans del paràgraf) o paragraph_format().set_space_after() (punts després del paràgraf):

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

Vegeu també

 Català