上标和下标

管理上标和下标文本

您可以在任何段落部分中添加上标和下标文本。要在 Aspose.Slides 文本框中添加上标或下标文本,必须使用 Escapement 属性,属于 PortionFormat 类。

该属性返回或设置上标或下标文本(值在 -100%(下标)到 100%(上标)之间)。例如:

  • 创建一个 Presentation 类的实例。
  • 通过使用其索引获取幻灯片的引用。
  • 向幻灯片添加一个矩形类型的 IAutoShape。
  • 访问与 IAutoShape 关联的 ITextFrame。
  • 清除现有的段落。
  • 创建一个新的段落对象用于保存上标文本,并将其添加到 ITextFrame 的 IParagraphs 集合中。
  • 创建一个新的部分对象。
  • 设置该部分的 Escapement 属性为 0 到 100 之间的值以添加上标。(0 表示没有上标)
  • 为部分设置一些文本,然后将其添加到段落的部分集合中。
  • 创建一个新的段落对象用于保存下标文本,并将其添加到 ITextFrame 的 IParagraphs 集合中。
  • 创建一个新的部分对象。
  • 设置该部分的 Escapement 属性为 0 到 -100 之间的值以添加下标。(0 表示没有下标)
  • 为部分设置一些文本,然后将其添加到段落的部分集合中。
  • 将演示文稿保存为 PPTX 文件。

上述步骤的实现如下所示。

// The path to the documents directory.
const String outPath = u"../out/AddingSuperscriptAndSubscriptTextInTextFrame_out.pptx";
//const String templatePath = u"../templates/DefaultFonts.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> sld = pres->get_Slides()->idx_get(0);
// Add an AutoShape of Rectangle type
SharedPtr<IAutoShape> ashp = sld->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 100, 100, 300, 300);
// Add TextFrame to the Rectangle
SharedPtr<ITextFrame> tf = ashp->AddTextFrame(String::Empty);
tf->get_Paragraphs()->Clear();
// Adding the first Paragraph
SharedPtr<Paragraph> superPar = MakeObject<Paragraph>();
SharedPtr<Portion> portion1 = MakeObject<Portion>(u"SlideTitle");
superPar->get_Portions()->Add(portion1);
SharedPtr<Portion> superPortion = MakeObject<Portion>();
superPortion->get_PortionFormat()->set_Escapement(30);
superPortion->set_Text(u"TM");
superPar->get_Portions()->Add(superPortion);
// Adding the first Paragraph
SharedPtr<Paragraph> subPar = MakeObject<Paragraph>();
SharedPtr<Portion> portion2 = MakeObject<Portion>(u"a");
subPar->get_Portions()->Add(portion2);
SharedPtr<Portion> subPortion = MakeObject<Portion>();
subPortion->get_PortionFormat()->set_Escapement(-25);
subPortion->set_Text(u"i");
subPar->get_Portions()->Add(subPortion);
//Adding to text frame
ashp->get_TextFrame()->get_Paragraphs()->Add(superPar);
ashp->get_TextFrame()->get_Paragraphs()->Add(subPar);
// Save PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);