在 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();
}

結果:

Baseline、Top、Center、Bottom 字型對齊比較

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

此設定不同於 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();
}

結果:

文字片段的字型屬性

設定文字旋轉

使用 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 中,套用 全大寫 字型效果會使投影片上的文字顯示為大寫,即使原始輸入為小寫。使用 Aspose.Slides 取得此類文字片段時,函式庫會回傳原始輸入的文字。若要符合顯示效果,請檢查 TextCapType 並在值為 All 時將回傳的字串轉為大寫。

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

全大寫效果

以下程式碼示範如何擷取已套用 全大寫 效果的文字:

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,並配置漸層停止點、方向與透明度。