使用 Java 管理投影片字型

概述

Aspose.Slides 允許您直接從程式碼中管理投影片文字的字型屬性。您可以透過形狀、文字框、段落和文字區塊存取投影片中的文字,然後對所選文字套用格式設定。

本篇文章說明如何為投影片中現有的文字設定字型相關屬性,包括字型系列、粗體與斜體樣式、段落對齊方式以及字型顏色。它同時示範如何建立文字方塊、向其中加入文字,並在將結果儲存為 PPTX 檔案之前設定字型屬性,如字型系列、粗體、斜體、底線、字型大小與顏色。

管理字型相關屬性

使用 Aspose.Slides for Java 來管理段落的字型屬性:

  1. 建立 Presentation 類別的實例。
  2. 使用索引取得投影片的參考。
  3. 存取投影片中的 Placeholder 形狀,並將其類型轉換為 AutoShape
  4. AutoShape 所提供的 TextFrame 取得 Paragraph
  5. 將段落設定為兩端對齊。
  6. 存取 Paragraph 的文字 Portion
  7. 使用 FontData 定義字型,並相應設定文字 PortionFont
    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);

	// 存取投影片中的第一個和第二個 Placeholder,並將其類型轉換為 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. 在投影片上加入類型為 RectangleAutoShape
  4. 移除與 AutoShape 相關的填充樣式。
  5. 存取 AutoShapeTextFrame
  6. TextFrame 中加入一些文字。
  7. 存取與 TextFrame 相關的 Portion 物件。
  8. 定義要用於 Portion 的字型。
  9. 使用 Portion 物件所提供的相關屬性,設定其他字型屬性,例如粗體、斜體、底線、顏色和高度。
  10. 將修改後的投影片寫入為 PPTX 檔案。

以下提供上述步驟的實作範例。

todo:image_alt_text
圖示:由 Aspose.Slides for Java 設定的部分字型屬性文字
// 實例化代表 PPTX 檔案的 Presentation 物件
Presentation pres = new Presentation();
try {
	// 取得第一張投影片
	ISlide sld = pres.getSlides().get_Item(0);
	
	// 新增類型為 Rectangle 的 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();
}