如何在 C++ 中格式化文本
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat class. A 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:在同一段落中使用多个 Portion
单个段落可以包含具有不同格式的多个部分。添加一个新的 Portion 到段落的 portions() 集合::
#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 vs 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