使用 PHP 管理簡報中的字型

管理字型相關屬性

使用 Aspose.Slides for PHP 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 物件
  $pres = new Presentation("FontProperties.pptx");
  try {
    # 使用投影片位置存取投影片
    $slide = $pres->getSlides()->get_Item(0);
    # 存取投影片中的第一個與第二個占位符,並將其型別轉換為 AutoShape
    $tf1 = $slide->getShapes()->get_Item(0)->getTextFrame();
    $tf2 = $slide->getShapes()->get_Item(1)->getTextFrame();
    # 存取第一個段落
    $para1 = $tf1->getParagraphs()->get_Item(0);
    $para2 = $tf2->getParagraphs()->get_Item(0);
    # 將段落設定為兩端對齊
    $para2->getParagraphFormat()->setAlignment(TextAlignment->JustifyLow);
    # 存取第一個 Portion
    $port1 = $para1->getPortions()->get_Item(0);
    $port2 = $para2->getPortions()->get_Item(0);
    # 定義新字型
    $fd1 = new FontData("Elephant");
    $fd2 = new FontData("Castellar");
    # 將新字型指派給 Portion
    $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(java("java.awt.Color")->BLUE);
    $port2->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $port2->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
    # 將 PPTX 儲存至磁碟
    $pres->save("WelcomeFont.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $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 PHP via Java 設定的文字字型屬性
  # 實例化一個代表 PPTX 檔案的 Presentation 物件
  $pres = new Presentation();
  try {
    # 取得第一張投影片
    $sld = $pres->getSlides()->get_Item(0);
    # 新增類型為 Rectangle 的 AutoShape
    $ashp = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 200, 50);
    # 移除與 AutoShape 相關的任何填充樣式
    $ashp->getFillFormat()->setFillType(FillType::NoFill);
    # 存取與 AutoShape 相關的 TextFrame
    $tf = $ashp->getTextFrame();
    $tf->setText("Aspose TextBox");
    # 存取與 TextFrame 相關的 Portion
    $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(java("java.awt.Color")->BLUE);
    # 將簡報儲存至磁碟
    $pres->save("pptxFont.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }