在 Android 上管理演示文稿的字体

管理字体相关属性

使用 Aspose.Slides for Android via Java 管理段落的字体属性:

  1. 创建 Presentation 类的实例。
  2. 使用索引获取幻灯片的引用。
  3. 访问幻灯片中的 Placeholder 形状并将其强制转换为 AutoShape。
  4. 从由 AutoShape 暴露的 TextFrame 中获取 Paragraph。
  5. 对段落进行两端对齐。
  6. 访问 Paragraph 的文本 Portion。
  7. 使用 FontData 定义字体,并相应地设置文本 Portion 的 Font。
    1. 将字体设为粗体。
    2. 将字体设为斜体。
  8. 使用由 Portion 对象暴露的 FillFormat 设置字体颜色。
  9. 将修改后的演示文稿保存为 PPTX 文件。

以下给出上述步骤的实现示例。它接受一个未装饰的演示文稿,并对其中一张幻灯片的字体进行格式化。下面的截图展示了输入文件以及代码片段如何更改它。代码会更改字体、颜色和字体样式。

todo:image_alt_text
图:输入文件中的文本
todo:image_alt_text
图:相同文本的更新后格式
	// 实例化一个表示 PPTX 文件的 Presentation 对象
	Presentation pres = new Presentation("FontProperties.pptx");
	try {
		// 使用幻灯片位置访问幻灯片
		ISlide slide = pres.getSlides().get_Item(0);

		// 访问幻灯片中的第一个和第二个占位符并将其强制转换为 AutoShape
		ITextFrame tf1 = ((IAutoShape) slide.getShapes().get_Item(0)).getTextFrame();
		ITextFrame tf2 = ((IAutoShape) slide.getShapes().get_Item(1)).getTextFrame();

		// 访问第一个段落
		IParagraph para1 = tf1.getParagraphs().get_Item(0);
		IParagraph para2 = tf2.getParagraphs().get_Item(0);

		// 对段落进行两端对齐
		para2.getParagraphFormat().setAlignment(TextAlignment.JustifyLow);

		// 访问第一个文本段
		IPortion port1 = para1.getPortions().get_Item(0);
		IPortion port2 = para2.getPortions().get_Item(0);

		// 定义新字体
		FontData fd1 = new FontData("Elephant");
		FontData fd2 = new FontData("Castellar");

		// 将新字体分配给文本段
		port1.getPortionFormat().setLatinFont(fd1);
		port2.getPortionFormat().setLatinFont(fd2);

		// 将字体设置为粗体
		port1.getPortionFormat().setFontBold(NullableBool.True);
		port2.getPortionFormat().setFontBold(NullableBool.True);

		// 将字体设置为斜体
		port1.getPortionFormat().setFontItalic(NullableBool.True);
		port2.getPortionFormat().setFontItalic(NullableBool.True);

		// 设置字体颜色
		port1.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
		port1.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
		port2.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
		port2.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GREEN);

		// 将 PPTX 保存到磁盘
		pres.save("WelcomeFont.pptx", SaveFormat.Pptx);
	} finally {
		if (pres != null) pres.dispose();
	}

设置文本字体属性

创建文本框并设置其中文本的字体属性:

  1. 创建 Presentation 类的实例。
  2. 使用索引获取幻灯片的引用。
  3. 向幻灯片添加类型为 Rectangle 的 AutoShape。
  4. 移除与 AutoShape 关联的填充样式。
  5. 访问 AutoShape 的 TextFrame。
  6. 向 TextFrame 添加一些文本。
  7. 访问与 TextFrame 关联的 Portion 对象。
  8. 为 Portion 定义要使用的字体。
  9. 使用 Portion 对象暴露的相关属性设置其他字体属性,如粗体、斜体、下划线、颜色和高度。
  10. 将修改后的演示文稿写入为 PPTX 文件。

以下给出上述步骤的实现示例。

todo:image_alt_text
图:使用 Aspose.Slides for Android via Java 设置的带有部分字体属性的文本
// 实例化一个表示 PPTX 文件的 Presentation 对象
Presentation pres = new Presentation();
try {
	// 获取第一张幻灯片
	ISlide sld = pres.getSlides().get_Item(0);
	
	// 添加一个矩形类型的 AutoShape
	IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 50);
	
	// 移除与 AutoShape 关联的填充样式
	ashp.getFillFormat().setFillType(FillType.NoFill);
	
	// 访问与 AutoShape 关联的 TextFrame
	ITextFrame tf = ashp.getTextFrame();
	tf.setText("Aspose TextBox");
	
	// 访问与 TextFrame 关联的 Portion
	IPortion port = tf.getParagraphs().get_Item(0).getPortions().get_Item(0);
	
	// 为 Portion 设置字体
	port.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
	
	// 设置字体的粗体属性
	port.getPortionFormat().setFontBold(NullableBool.True);
	
	// 设置字体的斜体属性
	port.getPortionFormat().setFontItalic(NullableBool.True);
	
	// 设置字体的下划线属性
	port.getPortionFormat().setFontUnderline(TextUnderlineType.Single);
	
	// 设置字体的高度
	port.getPortionFormat().setFontHeight(25);
	
	// 设置字体的颜色
	port.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
	port.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
	
	// 保存演示文稿到磁盘
	pres.save("pptxFont.pptx", SaveFormat.Pptx);
} finally {
	if (pres != null) pres.dispose();
}