Wie man Text in C++ formatiert

Wie man Text in C++ formatiert

Aspose.Slides FOSS für C++ bietet eine feinkörnige Textformatierung über die PortionFormat‑Klasse. Ein Portion ist die kleinste unabhängige Texteinheit; er entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett‑, Kursiv‑, Schriftgrößen‑ und Farbformatierungen auf Text in einer Präsentation anwendet.

Schritt-für-Schritt-Anleitung

Schritt 1: Bibliothek erstellen und verlinken

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 .

Schritt 2: Eine Form mit einem Textfeld hinzufügen

Vor dem Formatieren von Text fügen Sie eine Form hinzu und setzen Sie deren Textinhalt über 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;
}

Schritt 3: Zugriff auf das TextFrame

shape.text_frame() gibt einen Zeiger auf das TextFrame der Form zurück. Verwenden Sie ->, um Methoden darauf aufzurufen.

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

Ein TextFrame enthält Paragraph Objekte (tf->paragraphs()). Jeder Paragraph enthält Portion Objekte (paragraph.portions()).


Schritt 4: Fett‑ und Kursivformatierung anwenden

Verwenden Sie portion_format().set_font_bold() und portion_format().set_font_italic(). Diese Methoden akzeptieren NullableBool::TRUE, NullableBool::FALSE oder NullableBool::NOT_DEFINED (erben vom 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;
}

Schritt 5: Schriftgröße und Farbe festlegen

Setze portion_format().set_font_height() für die Größe (in Punkten) und verwende fill_format() für die Farbe.

#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) akzeptiert Werte von 0‑255 für jeden Kanal.


Schritt 6: Mehrere Abschnitte in einem Absatz

Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Fügen Sie einer Absatz-portions()-Sammlung ein neues Portion hinzu:

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

Häufige Probleme und Lösungen

Text erscheint schwarz, obwohl die Farbe eingestellt wurde

Stellen Sie sicher, dass fill_format().set_fill_type(FillType::SOLID) gesetzt ist, bevor Sie die Farbe zuweisen. Ohne das Festlegen des Fülltyps kann die Farbänderung keine Wirkung haben.

NullableBool::TRUE vs true

portion_format().set_font_bold() erwartet NullableBool::TRUE, nicht das C++ true. Das direkte Übergeben von true wird nicht kompilieren oder hat ein undefiniertes Verhalten, abhängig von der Überladungsauflösung.

Schriftart erscheint nicht in der gespeicherten Datei

Die set_latin_font()‑Methode legt die lateinische Schriftfamilie fest. Wenn nicht festgelegt, wird die Schriftart des Präsentationsthemas verwendet. Benutzerdefinierte Schriftarten müssen eingebettet oder auf dem Anzeigegerät verfügbar sein.


Häufig gestellte Fragen

Wie ändere ich die Schriftfamilie?

Setze portion_format().set_latin_font():

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

FontData akzeptiert den Schriftfamiliennamen als Zeichenkette.

Wie stelle ich die Absatzausrichtung ein?

Verwenden Sie paragraph_format().set_alignment():

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

Unterstützte Werte: LEFT, CENTER, RIGHT, JUSTIFY.

Wie stelle ich den Zeilenabstand ein?

Verwenden Sie paragraph_format().set_space_before() (Punkte vor dem Absatz) oder paragraph_format().set_space_after() (Punkte nach dem Absatz):

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

Siehe auch

 Deutsch