如何在 C++ 中格式化文本
Aspose.Slides FOSS for C++ 通过 PortionFormat 类提供细粒度的文本格式化。Portion 是文本的最小独立单元;它映射到段落中的单个格式运行。本指南展示了如何在演示文稿中对文本应用粗体、斜体、字体大小和颜色格式化。
分步指南
步骤 1:构建并链接库
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 .步骤 2:添加带文本框的形状
在格式化文本之前,添加一个形状并通过 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;
}步骤 3: 访问 TextFrame
shape.text_frame() 返回指向形状的 TextFrame 的指针。使用 -> 在其上调用方法。
auto* tf = shape.text_frame(); // pointer to the shape's text frame
tf->set_text("your text here");一个TextFrame包含Paragraph对象(tf->paragraphs())。每个Paragraph包含Portion对象(paragraph.portions())。
第4步:应用粗体和斜体格式
使用 portion_format().set_font_bold() 和 portion_format().set_font_italic()。这些方法接受 NullableBool::TRUE、NullableBool::FALSE 或 NullableBool::NOT_DEFINED(继承自 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;
}步骤 5:设置字体大小和颜色
将 portion_format().set_font_height() 设置为大小(以点为单位),并使用 fill_format() 设置颜色。
#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) 接受每个通道的 0-255 值。
步骤 6:一个段落中的多个部分
单个段落可以包含具有不同格式的多个部分。向段落的 portions() 集合中添加一个新的 Portion:
#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;
}常见问题及解决方案
即使设置了颜色,文本仍显示为黑色
确保在分配颜色之前已设置 fill_format().set_fill_type(FillType::SOLID)。如果未设置填充类型,颜色更改可能不会生效。
NullableBool::TRUE 对比 true
portion_format().set_font_bold() 期望 NullableBool::TRUE,而不是 C++ true。直接传递 true 将无法编译,或根据重载解析会产生未定义行为。
保存的文件中未出现字体
set_latin_font() 方法设置拉丁字体族。如果未设置,则使用演示主题字体。自定义字体必须嵌入或在查看机器上可用。
常见问题
如何更改字体系列?
设置 portion_format().set_latin_font():
fmt.set_latin_font(asf::FontData("Arial"));FontData 接受字体族名称作为字符串。
如何设置段落对齐方式?
使用 paragraph_format().set_alignment():
tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);支持的值:LEFT, CENTER, RIGHT, JUSTIFY。
我该如何设置行间距?
使用 paragraph_format().set_space_before()(段前点)或 paragraph_format().set_space_after()(段后点):
tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6); // 6pt after