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 Einheit des Textes; es erfasst eine einzige Formatierung innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett-, Kursiv- und Farbformatierungen auf den Text in einer Präsentation anwenden kann.
Schritt für Schritte
Schritt 1: Erstellen und Verknüpfen der Bibliothek
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: Fügen Sie eine Form mit einem Textrahmen hinzu
Bevor Sie Text formatieren, fügen Sie eine Form hinzu und setzen Sie den Inhalt des Texts über die 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 den Textrahmen
shape.text_frame() gibt einen Zeiger zu der Form zurück TextFrame.- Verwenden . -> - Um Methoden darauf anzurufen.
auto* tf = shape.text_frame(); // pointer to the shape's text frame
tf->set_text("your text here");A) Die TextFrame enthält: Paragraph Objekte (tf->paragraphs()) Jeder einzelne. Paragraph enthält: Portion Objekte (paragraph.portions()).
Schritt 4: Fett- und Kursivformatierung anwenden
Verwendung portion_format().set_font_bold() und . portion_format().set_font_italic().Diese Methoden akzeptieren die NullableBool::TRUE, NullableBool::FALSE,oder , NullableBool::NOT_DEFINED (Erbe vom Meister).
#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: Fontskala und Farbe festlegen
Setz portion_format().set_font_height() für Größe (in Punkten) und Verwendung fill_format() für 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::Drawing::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 0-255 für jeden Kanal.
Schritt 6: Mehrfach in einem Absatz
Ein einzelner Absatz kann mehrere Teile mit unterschiedlicher Formatierung enthalten. Portion Die Kommission hat die 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::Drawing::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 auch nach Einstellung der Farbe schwarz
Sicherstellen fill_format().set_fill_type(FillType::SOLID) ohne den Fülltyp zu setzen, hat die Farbänderung möglicherweise keine Wirkung.
NullableBool::TRUE gegen true
portion_format().set_font_bold() Erwartungen NullableBool::TRUE, nicht C++ true.- Übergeben . true Die Daten werden nicht direkt kompiliert oder haben ein undefiniertes Verhalten, je nach Überlastungslösung.
Schriftart nicht in der gespeicherten Datei angezeigt
Die set_latin_font() method setzt die lateinische Schriftfamilie fest. Wenn nicht festgelegt, wird die Präsentations-Themenschrift verwendet. Benutzerdefinierte Schriften müssen eingebettet oder auf der Betrachtungsmaschine verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Setz portion_format().set_latin_font():
fmt.set_latin_font(asf::FontData("Arial"));FontData Akzeptiert den Schriftfamiliennamen als Zeichenfolge.
Wie kann ich die Absätze ausrichten?
Verwendung paragraph_format().set_alignment():
tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);Unterstützte Werte: LEFT, CENTER, RIGHT, JUSTIFY.
Wie kann ich den Linienabstand einstellen?
Verwendung 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