How to Format Text in C++
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat class. A Portion is the smallest independent unit of text; it maps to a single formatting run within a paragraph. This guide shows how to apply bold, italic, font size, and color formatting to text in a presentation.
Step-by-Step Guide
Step 1: Build and Link the Library
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 .Step 2: Add a Shape with a Text Frame
Before formatting text, a shape must contain a TextFrame. Use shape.add_text_frame() to create one.
#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);
auto& tf = shape.add_text_frame("Default text: will be formatted");
prs.save("output.pptx", asf::SaveFormat::PPTX);
return 0;
}Step 3: Access the TextFrame
shape.add_text_frame() returns a reference to the TextFrame object. You can also retrieve it later via shape.text_frame().
auto& tf = shape.text_frame(); // if the frame already exists
auto& tf = shape.add_text_frame(""); // creates a new frame
A TextFrame contains Paragraph objects (tf.paragraphs()). Each Paragraph contains Portion objects (paragraph.portions()).
Step 4: Apply Bold and Italic Formatting
Use portion_format().set_font_bold() and portion_format().set_font_italic(). These methods accept NullableBool::TRUE, NullableBool::FALSE, or NullableBool::NOT_DEFINED (inherit from 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);
auto& tf = shape.add_text_frame("Bold and italic text");
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;
}Step 5: Set Font Size and Color
Set portion_format().set_font_height() for size (in points) and use fill_format() for 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);
auto& tf = shape.add_text_frame("Large corporate-blue heading");
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) accepts values 0-255 for each channel.
Step 6: Multiple Portions in One Paragraph
A single paragraph can contain multiple portions with different formatting. Add a new Portion to a paragraph’s portions() collection:
#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);
auto& tf = shape.add_text_frame(""); // start with empty 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;
}Common Issues and Fixes
Text appears black even after setting color
Ensure fill_format().set_fill_type(FillType::SOLID) is set before assigning the color. Without setting the fill type, the color change may have no effect.
NullableBool::TRUE vs true
portion_format().set_font_bold() expects NullableBool::TRUE, not the C++ true. Passing true directly will not compile or will have undefined behavior depending on the overload resolution.
Font does not appear in the saved file
The set_latin_font() method sets the Latin font family. If not set, the presentation theme font is used. Custom fonts must be embedded or available on the viewing machine.
Frequently Asked Questions
How do I change the font family?
Set portion_format().set_latin_font():
fmt.set_latin_font(asf::FontData("Arial"));FontData accepts the font family name as a string.
How do I set paragraph alignment?
Use paragraph_format().set_alignment():
tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);Supported values: LEFT, CENTER, RIGHT, JUSTIFY.
How do I set line spacing?
Use paragraph_format().set_space_before() (points before paragraph) or paragraph_format().set_space_after() (points after paragraph):
tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6); // 6pt after