Wie man Text in C++ formatiert
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat Klasse. A Portion ist die kleinste unabhängige Texteinheit; sie entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett, Kursiv, Schriftgröße und Farbformatierung auf Text in einer Präsentation anwendet.
Schritt-für-Schritt-Anleitung
Schritt 1: Bibliothek bauen 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: Ein Shape mit einem Text Frame hinzufügen
Bevor Sie Text formatieren, fügen Sie ein Shape hinzu und setzen dessen 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: Auf das TextFrame zugreifen
shape.text_frame() gibt einen Zeiger auf das Shape zurück TextFrame. 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()) Paragraph enthält Portion Objekte (paragraph.portions()).
Schritt 4: Fett- und Kursivformatierung anwenden
Verwenden portion_format().set_font_bold() und portion_format().set_font_italic(). Diese Methoden akzeptieren NullableBool::TRUE, NullableBool::FALSE, oder NullableBool::NOT_DEFINED (erbt 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
Setzen portion_format().set_font_height() für die Größe (in Punkten) und verwenden 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 Portionen in einem Paragraph
Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Füge ein neues Portion zu einem Absatz portions() Sammlung:
#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
Der Text erscheint schwarz, selbst nach dem Festlegen der Farbe
Stellen Sie sicher fill_format().set_fill_type(FillType::SOLID) muss festgelegt werden, bevor die Farbe zugewiesen wird. 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. Übergeben true direkt wird nicht kompilieren oder führt je nach Überladungsauflösung zu undefiniertem Verhalten.
Schriftart erscheint nicht in der gespeicherten Datei
Die set_latin_font() Methode legt die lateinische Schriftfamilie fest. Wenn sie nicht festgelegt ist, wird die Schriftart des Präsentationsthemas verwendet. Benutzerdefinierte Schriften müssen eingebettet oder auf dem Anzeigegerät verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Setzen 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 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 paragraph_format().set_space_before() (Punkte vor Absatz) oder paragraph_format().set_space_after() (Punkte nach Absatz):
tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6); // 6pt after