在 PHP 中格式化簡報文字

概述

本文說明如何使用 Aspose.Slides for PHP via Java 於 PowerPoint 與 OpenDocument 簡報中格式化文字。內容包括背景色、透明度、字元間距、字型屬性、旋轉、段落間距、自動調整行為、文字錨點、定位點與語言設定。

除非另有說明,範例皆使用 sample.pptx。第一張投影片的第一個形狀是一個文字方塊,其第一段文字如下所示。投影片與形狀的索引皆為零基礎。選取粗體部分的範例使用有效格式,包括繼承的粗體格式:

範例文字

若要尋找並突顯文字或正則表達式匹配項目,請參閱 Search and Replace Text。

設定文字背景色

使用 ParagraphFormat::getDefaultPortionFormat 來設定段落的預設醒目顏色,或使用 BasePortionFormat::getHighlightColor 針對個別文字區塊設定。

以下範例將淺灰色醒目設定為第一段的預設。個別區塊的顯式醒目顏色會覆蓋此預設:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $highlightColor = java("java.awt.Color")->LIGHT_GRAY;

    // 為整段設定醒目顏色。
    $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getHighlightColor()->setColor($highlightColor);

    $presentation->save("gray_paragraph.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

灰色段落

以下程式碼示範如何為 粗體字型的文字區塊 設定背景色:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $highlightColor = java("java.awt.Color")->LIGHT_GRAY;

    $portionCount = java_values($paragraph->getPortions()->getCount());
    for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
        $portion = $paragraph->getPortions()->get_Item($portionIndex);
        if (java_values($portion->getPortionFormat()->getEffective()->getFontBold())) {
            // 為文字區塊設定醒目顏色。
            $portion->getPortionFormat()->getHighlightColor()->setColor($highlightColor);
        }
    }

    $presentation->save("gray_text_portions.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

灰色文字區塊

對齊文字段落

使用 ParagraphFormat::setAlignment 來設定文字框內段落的對齊方式。可設定為置中、左對齊、右對齊、兩端對齊等。

以下程式碼示範如何將段落對齊至 置中:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextAlignment;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);

    // 設定段落的對齊方式為置中。
    $paragraph->getParagraphFormat()->setAlignment(TextAlignment::Center);

    $presentation->save("aligned_paragraph.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

對齊的段落

對齊行內字型

使用 ParagraphFormat::setFontAlignment 以在同一行中垂直對齊不同字型大小的文字區塊。此設定套用於整個段落,並控制每一行內的對齊。

以下獨立範例在同一張投影片上建立四個帶標籤的文字方塊。每個段落的文字分別為 18、36、54 點,且使用不同的字型對齊方式。範例使用 Arial、停用自動調整與換行,並保持文字框足夠大以容納單行文字。

use aspose\slides\FillType;
use aspose\slides\FontAlignment;
use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAlignment;
use aspose\slides\TextAnchorType;
use aspose\slides\TextAutofitType;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $alignments = [FontAlignment::Baseline, FontAlignment::Top, FontAlignment::Center, FontAlignment::Bottom];
    $alignmentNames = ["Baseline", "Top", "Center", "Bottom"];
    $fontSizes = [18, 36, 54];
    $font = new FontData("Arial");
    $gray = java("java.awt.Color")->GRAY;
    $black = java("java.awt.Color")->BLACK;

    for ($i = 0; $i < count($alignments); $i++) {
        $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 30, 20 + $i * 130, 660, 120);
        $shape->getFillFormat()->setFillType(FillType::NoFill);
        $shape->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);

        $textFrame = $shape->getTextFrame();
        $textFrame->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);
        $textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
        $textFrame->getTextFrameFormat()->setWrapText(NullableBool::False);

        $label = $textFrame->getParagraphs()->get_Item(0);
        $label->setText($alignmentNames[$i]);
        $label->getParagraphFormat()->setAlignment(TextAlignment::Left);
        $label->getParagraphFormat()->getDefaultPortionFormat()->setFontHeight(14);
        $label->getParagraphFormat()->getDefaultPortionFormat()->setLatinFont($font);
        $label->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
        $label->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($gray);

        $paragraph = new Paragraph();
        $paragraph->getParagraphFormat()->setFontAlignment($alignments[$i]);
        $paragraph->getParagraphFormat()->setAlignment(TextAlignment::Left);
        $paragraph->getParagraphFormat()->getDefaultPortionFormat()->setLatinFont($font);
        $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
        $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($black);

        foreach ($fontSizes as $fontSize) {
            $portion = new Portion("Ag ");
            $portion->getPortionFormat()->setFontHeight($fontSize);
            $paragraph->getPortions()->add($portion);
        }

        $textFrame->getParagraphs()->add($paragraph);
    }

    $presentation->save("font_alignment.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

基線、上方、置中與下方字型對齊的比較

字型對齊使用字型度量,因此個別字母的可見邊緣不一定完全對齊。範例同時包含大寫字母與下垂部,以說明基線與底部對齊的差異。字型的可用性與替代、所使用的字元,以及字型大小的差異皆會影響結果。框架尺寸、邊距、行距、換行與自動調整亦會影響版面配置;比較不同模式時請使用相同的字型與版面設定。

此設定與 ParagraphFormat::setAlignment(水平段落對齊)以及 TextFrameFormat::setAnchoringType(在形狀內垂直定位文字區塊)不同。透過 BasePortionFormat::setEscapement 進行上標與下標格式時,只是相對於基線位移個別區塊,而非為段落行設定字型對齊。

設定文字透明度

文字透明度透過指派給 BasePortionFormat::getFillFormat 的顏色之 alpha 成分來控制。以下範例中,alpha = 50 為 0–255 之 ARGB alpha 通道值,非透明度百分比。

以下程式碼示範如何將 整段 文字設為透明:

use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$alpha = 50;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $fillFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat();

    // 將文字的填充顏色設為透明顏色。
    $fillFormat->setFillType(FillType::Solid);
    $transparentColor = new Java("java.awt.Color", 0, 0, 0, $alpha);
    $fillFormat->getSolidFillColor()->setColor($transparentColor);

    $presentation->save("transparent_paragraph.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

透明段落

以下程式碼示範如何將 粗體字型的文字區塊 設為透明:

use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$alpha = 50;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $transparentColor = new Java("java.awt.Color", 0, 0, 0, $alpha);

    $portionCount = java_values($paragraph->getPortions()->getCount());
    for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
        $portion = $paragraph->getPortions()->get_Item($portionIndex);
        if (java_values($portion->getPortionFormat()->getEffective()->getFontBold())) {
            // 設定文字區塊的透明度。
            $fillFormat = $portion->getPortionFormat()->getFillFormat();
            $fillFormat->setFillType(FillType::Solid);
            $fillFormat->getSolidFillColor()->setColor($transparentColor);
        }
    }

    $presentation->save("transparent_text_portions.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

透明文字區塊

設定文字字元間距

使用 BasePortionFormat::setSpacing 來在文字方塊中擴張或收緊字元間距。範例中加入 3 點間距;負值則收緊文字。

以下 PHP 程式碼示範如何在 整段 文字中擴張字元間距:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);

    // 注意:使用負值可壓縮字元間距。
    $paragraph->getParagraphFormat()->getDefaultPortionFormat()->setSpacing(3); // 擴大字元間距。

    $presentation->save("character_spacing_in_paragraph.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

段落中的字元間距

以下程式碼示範如何在 粗體字型的文字區塊 中擴張字元間距:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);

    $portionCount = java_values($paragraph->getPortions()->getCount());
    for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
        $portion = $paragraph->getPortions()->get_Item($portionIndex);
        if (java_values($portion->getPortionFormat()->getEffective()->getFontBold())) {
            // 注意:使用負值可壓縮字元間距。
            $portion->getPortionFormat()->setSpacing(3); // 擴大字元間距。
        }
    }

    $presentation->save("character_spacing_in_text_portions.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

文字區塊中的字元間距

針對特定字型停用字距調整

在某些情況下,Aspose.Slides 所呈現的文字可能比 PowerPoint 顯示的稍微緊密。這可能是因為 PowerPoint 會忽略特定字型的字距調整資料,即使字型本身包含有效的字距資訊且在 PowerPoint 設定中已啟用。

若要使渲染結果更接近 PowerPoint,可針對使用受影響字型的文字區塊停用字距調整。將 BasePortionFormat::setKerningMinimalSize 設為大於實際字型大小的值。本範例需要「presentation.pptx」且第一張投影片的第一個形狀為文字方塊。它會檢查有效字型名稱(包括繼承的字型),並對使用 Roboto 且字型大小低於 100 點的區塊設定 100 點門檻,以停用字距調整:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("presentation.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $targetFont = "Roboto";

    $paragraphCount = java_values($autoShape->getTextFrame()->getParagraphs()->getCount());
    for ($paragraphIndex = 0; $paragraphIndex < $paragraphCount; $paragraphIndex++) {
        $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item($paragraphIndex);
        $portionCount = java_values($paragraph->getPortions()->getCount());
        for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
            $portion = $paragraph->getPortions()->get_Item($portionIndex);
            $portionFormat = $portion->getPortionFormat()->getEffective();
            $latinFont = $portionFormat->getLatinFont();
            $eastAsianFont = $portionFormat->getEastAsianFont();
            $complexScriptFont = $portionFormat->getComplexScriptFont();

            if ((!java_is_null($latinFont) && $latinFont->getFontName() == $targetFont) ||
                (!java_is_null($eastAsianFont) && $eastAsianFont->getFontName() == $targetFont) ||
                (!java_is_null($complexScriptFont) && $complexScriptFont->getFontName() == $targetFont)) {
                $portion->getPortionFormat()->setKerningMinimalSize(100);
            }
        }
    }

    $presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

對於低於門檻的相符文字,此設定會阻止字距調整,並有助於使 Aspose.Slides 的渲染與 PowerPoint 在受此 PowerPoint 特定行為影響的字型呈現上更為一致。

管理文字字型屬性

字型屬性可以透過 ParagraphFormat::getDefaultPortionFormat 在段落層級設定,或透過 PortionFormat 在個別區塊設定。

以下範例將第一段的預設字型設為 12 點 Times New Roman,並套用粗體、斜體與點狀底線。個別區塊的顯式格式會覆蓋這些預設值:

use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextUnderlineType;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $defaultPortionFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat();
    $font = new FontData("Times New Roman");

    // 設定段落的字型屬性。
    $defaultPortionFormat->setFontHeight(12);
    $defaultPortionFormat->setFontBold(NullableBool::True);
    $defaultPortionFormat->setFontItalic(NullableBool::True);
    $defaultPortionFormat->setFontUnderline(TextUnderlineType::Dotted);
    $defaultPortionFormat->setLatinFont($font);

    $presentation->save("font_properties_for_paragraph.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

段落的字型屬性

以下範例將 13 點 Times New Roman、斜體與點狀底線套用於有效格式為粗體的區塊:

use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextUnderlineType;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $font = new FontData("Times New Roman");

    $portionCount = java_values($paragraph->getPortions()->getCount());
    for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
        $portion = $paragraph->getPortions()->get_Item($portionIndex);
        if (java_values($portion->getPortionFormat()->getEffective()->getFontBold())) {
            // 設定文字區塊的字型屬性。
            $portionFormat = $portion->getPortionFormat();
            $portionFormat->setFontHeight(13);
            $portionFormat->setFontItalic(NullableBool::True);
            $portionFormat->setFontUnderline(TextUnderlineType::Dotted);
            $portionFormat->setLatinFont($font);
        }
    }

    $presentation->save("font_properties_for_text_portions.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

文字區塊的字型屬性

控制文字高度正規化

使用 BasePortionFormat::setNormaliseHeight 以在重現範本排版或研究字元高度差異時正規化文字高度。在 PowerPoint 中,啟用正規化會垂直拉伸或壓縮個別字元,使其在文字區塊內具有統一的可見高度,但不保證不同字型之間的文字高度相等。

將屬性設為 NullableBool::True 或 False 以明確啟用或停用正規化。NullableBool::NotDefined 會保留本地設定未設定,讓區塊繼承段落的 getDefaultPortionFormat 等值,這不等於 False。使用 PortionFormat::getEffective 並讀取返回的有效格式資料之 getNormaliseHeight 可取得解析後的布林值。

以下獨立範例需要安裝 Arial。它建立兩個文字區塊,內容相同,包含大寫字母、小寫字母、下垂部份與重音字元,字型大小為 54 點。兩段皆預設啟用正規化;上方區塊明確停用,下方區塊則繼承。框架尺寸、邊距與段落間距相同,且停用換行與自動調整,以保持比較一致。範例會輸出 False: false 與 NotDefined: true,並將檔案儲存為「text_height_normalization.pptx」:

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAlignment;
use aspose\slides\TextAnchorType;
use aspose\slides\TextAutofitType;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $settings = [NullableBool::False, NullableBool::NotDefined];
    $settingNames = ["False", "NotDefined"];

    for ($i = 0; $i < count($settings); $i++) {
        $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 30, 60 + $i * 200, 660, 150);
        $shape->getFillFormat()->setFillType(FillType::NoFill);
        $shape->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);

        $textFrame = $shape->getTextFrame();
        $textFrame->setText("Hxgjpq ÅÉ 0123");
        $textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
        $textFrame->getTextFrameFormat()->setWrapText(NullableBool::False);
        $textFrame->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);
        $textFrame->getTextFrameFormat()->setMarginTop(0);
        $textFrame->getTextFrameFormat()->setMarginLeft(0);

        $paragraph = $textFrame->getParagraphs()->get_Item(0);
        $paragraph->getParagraphFormat()->setAlignment(TextAlignment::Left);
        $paragraph->getParagraphFormat()->setSpaceBefore(0);
        $paragraph->getParagraphFormat()->setSpaceAfter(0);
        $paragraph->getParagraphFormat()->setSpaceWithin(100);
        $paragraph->getParagraphFormat()->getDefaultPortionFormat()->setNormaliseHeight(NullableBool::True);

        $portion = $paragraph->getPortions()->get_Item(0);
        $font = new FontData("Arial");
        $portion->getPortionFormat()->setLatinFont($font);
        $portion->getPortionFormat()->setFontHeight(54);
        $portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
        $black = java("java.awt.Color")->BLACK;
        $portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($black);
        $portion->getPortionFormat()->setNormaliseHeight($settings[$i]);

        $normaliseHeight = java_values($portion->getPortionFormat()->getEffective()->getNormaliseHeight());
        echo $settingNames[$i], ": ", $normaliseHeight ? "true" : "false", PHP_EOL;
    }

    $presentation->save("text_height_normalization.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

以下比較圖來自已儲存的簡報。上列為正規化停用 (False);下列為繼承已啟用 (NotDefined → True)。請保留 Arial 安裝以重現此外觀。

Arial 54 點文字,上方正規化停用,下方繼承正規化啟用

正規化會改變字元高度,卻不改變指派的 getFontHeight 值(即字型大小)。段落的 setSpaceWithin 控制行距,文字框的 setAutofitType 控制文字或容器的調整方式。這些設定各有目的,會獨立影響整體版面配置。欲了解更多段落格式與繼承,請參閱 Manage Paragraph。

套用與移除刪除線

使用 BasePortionFormat::setStrikethroughType 於文字區塊上顯示更正、取消的文字或先前價格,而不必刪除原始文字。可將其設為 TextStrikethroughType::Single 或 Double 以產生單線或雙線刪除線。

若要移除刪除線,傳入 TextStrikethroughType::None。此設定會明確停用效果,即使周圍的格式指定了刪除線。相較之下,TextStrikethroughType::NotDefined 會保留本地設定未定義,讓區塊繼承段落等上層格式,並不保證刪除線被移除。使用 PortionFormat::getEffective 可讀取最終格式,包括繼承值。

以下獨立範例建立三個價格比較。每段都有標籤、先前價格與目前價格的區塊,僅先前價格受到刪除線影響。第一列先套用刪除線再移除,其他列保留單線與雙線刪除線。變更此設定可保留文字及其他格式(例如粗體的先前價格)。

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAlignment;
use aspose\slides\TextStrikethroughType;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 620, 220);
    $shape->getFillFormat()->setFillType(FillType::NoFill);
    $shape->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
    $shape->getTextFrame()->getParagraphs()->clear();

    $labels = [ "None: ", "Single: ", "Double: " ];

    foreach ($labels as $label) {
        $paragraph = new Paragraph();
        $paragraph->getParagraphFormat()->setAlignment(TextAlignment::Left);
        $paragraph->getParagraphFormat()->setSpaceAfter(20);

        $format = $paragraph->getParagraphFormat()->getDefaultPortionFormat();
        $format->setFontHeight(28);
        $font = new FontData("Arial");
        $format->setLatinFont($font);
        $format->getFillFormat()->setFillType(FillType::Solid);
        $blackColor = java("java.awt.Color")->BLACK;
        $format->getFillFormat()->getSolidFillColor()->setColor($blackColor);

        $labelPortion = new Portion($label);
        $paragraph->getPortions()->add($labelPortion);

        $previousPrice = new Portion("$100");
        $previousPrice->getPortionFormat()->setFontBold(NullableBool::True);
        $paragraph->getPortions()->add($previousPrice);
        $currentPrice = new Portion(" → $80");
        $paragraph->getPortions()->add($currentPrice);

        $shape->getTextFrame()->getParagraphs()->add($paragraph);
    }

    $paragraphs = $shape->getTextFrame()->getParagraphs();

    // 套用刪除線,然後明確移除第一個先前價格的刪除線。
    $paragraphs->get_Item(0)->getPortions()->get_Item(1)->getPortionFormat()->setStrikethroughType(TextStrikethroughType::Single);
    $paragraphs->get_Item(0)->getPortions()->get_Item(1)->getPortionFormat()->setStrikethroughType(TextStrikethroughType::None);

    $paragraphs->get_Item(1)->getPortions()->get_Item(1)->getPortionFormat()->setStrikethroughType(TextStrikethroughType::Single);
    $paragraphs->get_Item(2)->getPortions()->get_Item(1)->getPortionFormat()->setStrikethroughType(TextStrikethroughType::Double);

    $presentation->save("strikethrough.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果(由上至下):無刪除線、單線刪除線、雙線刪除線。

先前價格的無刪除線、單線刪除線與雙線刪除線比較

設定文字旋轉

使用 TextFrameFormat::setTextVerticalType 以在形狀內設定預定義的文字方向。

以下程式碼將形狀內的文字方向設為 TextVerticalType::Vertical270,即文字 逆時針旋轉 90 度:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextVerticalType;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $autoShape->getTextFrame()->getTextFrameFormat()->setTextVerticalType(TextVerticalType::Vertical270);

    $presentation->save("text_rotation.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

文字旋轉

為文字框設定自訂旋轉角度

使用 TextFrameFormat::setRotationAngle 為 TextFrame 設定自訂旋轉角度。

以下程式碼將文字框在形狀內順時針旋轉 3 度:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $autoShape->getTextFrame()->getTextFrameFormat()->setRotationAngle(3);

    $presentation->save("custom_text_rotation.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

自訂文字旋轉

設定段落行距

Aspose.Slides 提供 ParagraphFormat::setSpaceAfter、ParagraphFormat::setSpaceBefore 與 ParagraphFormat::setSpaceWithin 以控制段落間距。使用方式如下:

  • 正值表示以行高的百分比指定行距。
  • 負值表示以點數指定行距。

以下範例將第一段的內部間距設定為行高的 200%(雙倍行距):

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);

    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $paragraph->getParagraphFormat()->setSpaceWithin(200);

    $presentation->save("line_spacing.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

段落內的行距

控制斷行規則

段落斷行規則在窄文字區塊與混合拉丁文與東亞文字的簡報中相當有用。以下方法屬於 ParagraphFormat,因此會套用於整個段落:

  • setLatinLineBreak 控制拉丁文字的斷行規則。於混合文字時,變更此設定亦可能影響相鄰的東亞文字與標點換行位置。
  • setEastAsianLineBreak 控制東亞文字的斷行規則,包含行首與行尾字元的限制。

這些規則不會取代 TextFrameFormat::setWrapText,後者啟用文字框內的自動換行。它們僅在換行發生時影響版面配置,並不會插入斷行字元。使用明確的斷行字元則會在段落內強制換行,與可用寬度無關。

以下獨立範例建立一個包含中文與拉丁文字的窄文字區塊,同時明確設定兩種斷行選項,並儲存為「line_breaking.pptx」。欲實驗任一規則,只需變更對應的值,同時保持其他設定不變。範例使用 24 點 Arial 與 SimSun,框寬 160 點,水平文字框邊距為 0。 TextFrameFormat::setAutofitType 以 TextAutofitType::None 呼叫,確保文字大小與框尺寸固定。

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAlignment;
use aspose\slides\TextAutofitType;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 160, 300);
    $shape->getFillFormat()->setFillType(FillType::NoFill);

    $textFrame = $shape->getTextFrame();
    $textFrame->getTextFrameFormat()->setWrapText(NullableBool::True);
    $textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
    $textFrame->getTextFrameFormat()->setMarginLeft(0);
    $textFrame->getTextFrameFormat()->setMarginRight(0);

    $paragraph = $textFrame->getParagraphs()->get_Item(0);
    $paragraph->setText("中文排版测试,PowerPoint 中文演示。");

    $format = $paragraph->getParagraphFormat();
    $format->setAlignment(TextAlignment::Left);
    $format->getDefaultPortionFormat()->setFontHeight(24);
    $latinFont = new FontData("Arial");
    $format->getDefaultPortionFormat()->setLatinFont($latinFont);
    $eastAsianFont = new FontData("SimSun");
    $format->getDefaultPortionFormat()->setEastAsianFont($eastAsianFont);
    $format->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $blackColor = java("java.awt.Color")->BLACK;
    $format->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($blackColor);
    $format->setLatinLineBreak(NullableBool::False);
    $format->setEastAsianLineBreak(NullableBool::True);

    $presentation->save("line_breaking.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

控制懸掛標點

ParagraphFormat::setHangingPunctuation 允許符合條件的標點超出文字行的右邊緣,而非佔據下一行。此設定套用於整段落,與懸掛縮排不同。

以下獨立範例在寬度 100 點的文字框內啟用懸掛標點,並儲存為「hanging_punctuation.pptx」。使用 24 點 Arial 與水平文字框邊距為 0 時,句點會留在「sentence」之後,並延伸至右側文字邊緣。將屬性設為 NullableBool::False 可作比較:此設定下,句點會佔據單獨一行。為了保持寬度固定,啟用換行並停用自動調整。

use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAlignment;
use aspose\slides\TextAutofitType;

$presentation = new Presentation();
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 100, 200);
    $shape->getFillFormat()->setFillType(FillType::NoFill);

    $textFrame = $shape->getTextFrame();
    $textFrame->getTextFrameFormat()->setWrapText(NullableBool::True);
    $textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
    $textFrame->getTextFrameFormat()->setMarginLeft(0);
    $textFrame->getTextFrameFormat()->setMarginRight(0);

    $paragraph = $textFrame->getParagraphs()->get_Item(0);
    $paragraph->setText("Simple text, next sentence.");

    $format = $paragraph->getParagraphFormat();
    $format->setAlignment(TextAlignment::Left);
    $format->getDefaultPortionFormat()->setFontHeight(24);
    $latinFont = new FontData("Arial");
    $format->getDefaultPortionFormat()->setLatinFont($latinFont);
    $format->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
    $blackColor = java("java.awt.Color")->BLACK;
    $format->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($blackColor);
    $format->setHangingPunctuation(NullableBool::True);

    $presentation->save("hanging_punctuation.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

並非所有標點都能懸掛。上述 字型與版面條件 亦適用於此比較:變更字型、可用寬度、邊距或自動調整設定,都可能消除可見差異。

設定文字框的自動調整類型

TextFrameFormat::setAutofitType 決定文字超出容器邊界時的行為。可用來控制文字是縮小、溢出,或自動調整形狀以容納文字。以下範例將形狀設定為自動調整以適合文字,並將結果儲存為「autofit_type.pptx」。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextAutofitType;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $autoShape->getTextFrame()->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);

    $presentation->save("autofit_type.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

若要在自動換行後計算行數,並觀察文字或形狀寬度變化的結果,請參閱 Count Rendered Lines。僅行數本身無法判斷文字是否溢出容器。

設定文字框的錨點

TextFrameFormat::setAnchoringType 定義文字在形狀內的垂直定位方式,例如置頂、置中或置底。以下範例將文字錨定於第一個形狀的底部,並將結果儲存為「text_anchor.pptx」。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TextAnchorType;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);
    $autoShape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Bottom);

    $presentation->save("text_anchor.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

設定文字定位點

使用 ParagraphFormat::setDefaultTabSize 與 ParagraphFormat::getTabs 來設定段落中的定位點。以下範例將預設定位間距設定為 100 點,並在 30 點處新增一個左對齊的定位點。此設定會影響包含定位字元的文字。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\TabAlignment;

$presentation = new Presentation("sample.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);

    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $paragraph->getParagraphFormat()->setDefaultTabSize(100);
    $paragraph->getParagraphFormat()->getTabs()->add(30, TabAlignment::Left);

    $presentation->save("paragraph_tabs.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

結果:

段落定位點

設定校對語言

Aspose.Slides 提供 BasePortionFormat::setLanguageId,可為文字區塊設定校對語言。校對語言決定 PowerPoint 中的拼寫與文法檢查語言。

以下範例需要「presentation.pptx」,其第一張投影片的第一個形狀為文字方塊且至少包含一段文字。範例會將第一段內容取代為「1。」,將字型設定為 SimSun,並指派簡體中文校對語言 (zh-CN)。最後將結果儲存為「proofing_language.pptx」:

use aspose\slides\FontData;
use aspose\slides\Portion;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("presentation.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);

    $autoShape = $slide->getShapes()->get_Item(0);

    $paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
    $paragraph->getPortions()->clear();

    $font = new FontData("SimSun");

    $textPortion = new Portion();
    $textPortion->getPortionFormat()->setComplexScriptFont($font);
    $textPortion->getPortionFormat()->setEastAsianFont($font);
    $textPortion->getPortionFormat()->setLatinFont($font);

    // 設定校對語言的 Id。
    $textPortion->getPortionFormat()->setLanguageId("zh-CN");

    $textPortion->setText("1。");
    $paragraph->getPortions()->add($textPortion);

    $presentation->save("proofing_language.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

設定預設語言

使用 LoadOptions::setDefaultTextLanguage 定義載入或建立簡報時所產生文字的預設語言。以下範例建立一個預設文字語言為美式英語的簡報,加入文字方塊,並列印其第一個文字區塊的語言為 en-US。

use aspose\slides\LoadOptions;
use aspose\slides\Presentation;
use aspose\slides\ShapeType;

$loadOptions = new LoadOptions();
$loadOptions->setDefaultTextLanguage("en-US");

$presentation = new Presentation($loadOptions);
try {
    $slide = $presentation->getSlides()->get_Item(0);

    // 新增一個包含文字的矩形形狀。
    $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 150, 50);
    $shape->getTextFrame()->setText("Sample text");

    // 檢查第一個文字區塊的語言。
    $portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
    echo $portion->getPortionFormat()->getLanguageId();
} finally {
    $presentation->dispose();
}

設定預設文字樣式

若要在簡報層級套用預設文字格式,請使用 Presentation::getDefaultTextStyle。

以下範例將新簡報中最高層段落的預設字型設定為 14 點粗體,並將檔案儲存為「default_text_style.pptx」。文字會繼承這些預設值,除非更具體的格式覆寫它們。

use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation();
try {
    // 取得頂層段落格式。
    $paragraphFormat = $presentation->getDefaultTextStyle()->getLevel(0);

    if (!java_is_null($paragraphFormat)) {
        $paragraphFormat->getDefaultPortionFormat()->setFontHeight(14);
        $paragraphFormat->getDefaultPortionFormat()->setFontBold(NullableBool::True);
    }

    $presentation->save("default_text_style.pptx", SaveFormat::Pptx);
} finally {
    $presentation->dispose();
}

擷取全部大寫效果的文字

在 PowerPoint 中,套用 All Caps 字型效果會使文字在投影片上以大寫顯示,即使原始輸入為小寫。使用 Aspose.Slides 取得此類文字區塊時,函式庫會回傳原始輸入的文字。若要匹配顯示的文字,請檢查 TextCapType 並在值為 All 時將回傳的字串轉為大寫。

此範例需要「sample2.pptx」,其第一張投影片的第一個形狀為文字方塊。第一段的第一個區塊包含「Hello, Aspose!」並套用 All Caps 效果,如下圖所示。

全部大寫效果

以下程式碼示範如何擷取套用 All Caps 效果的文字:

use aspose\slides\Presentation;
use aspose\slides\TextCapType;

$presentation = new Presentation("sample2.pptx");
try {
    $slide = $presentation->getSlides()->get_Item(0);
    
    $autoShape = $slide->getShapes()->get_Item(0);
    $textPortion = $autoShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);

    $originalText = $textPortion->getText();
    echo "Original text: ", $originalText, "\n";

    $textFormat = $textPortion->getPortionFormat()->getEffective();
    if (java_values($textFormat->getTextCapType()) === TextCapType::All) {
        $text = strtoupper($originalText);
        echo "All-Caps effect: ", $text, "\n";
    }
} finally {
    $presentation->dispose();
}

輸出:

Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!

常見問題集

如何修改投影片上表格中的文字?

請使用 Table。遍歷儲存格,並透過 Cell::getTextFrame 以及 Paragraph::getParagraphFormat 來更新每個儲存格的文字與段落格式。

如何在 PowerPoint 投影片上的文字套用漸層顏色?

請使用 BasePortionFormat::getFillFormat。將 FillFormat::setFillType 設為 FillType::Gradient,並配置漸層停點、方向與透明度。