在 JavaScript 中從簡報取得文字片段邊界

概述

文字 Portion 代表段落內的特定文字片段,讓您能獨立於周圍內容操作該片段。在 Aspose.Slides 中,當您需要取得文字片段的邊界、僅對段落的一部分套用格式,或在更細緻的層面控制文字行為時,可使用 Portion。

本文說明如何使用 Portion.getRect 取得 Portion 的邊界矩形。也說明如何使用 Portion.getCoordinates 取得 Portion 起始位置的座標。此外,本文還強調了常見的與 Portion 相關的情境,例如對單一文字片段套用超連結、了解格式如何透過 Portion、Paragraph、TextFrame 和主題的繼承機制解析,以及處理指定字型不存在的情況。

取得文字 Portion 的邊界矩形

使用 Portion.getRect 取得文字 Portion 的邊界矩形:

const presentation = new aspose.slides.Presentation("Shapes.pptx");
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().get_Item(0);
    const paragraphs = shape.getTextFrame().getParagraphs();

    for (let paragraphIndex = 0; paragraphIndex < paragraphs.getCount(); paragraphIndex++) {
        const paragraph = paragraphs.get_Item(paragraphIndex);
        const portions = paragraph.getPortions();

        for (let portionIndex = 0; portionIndex < portions.getCount(); portionIndex++) {
            const portion = portions.get_Item(portionIndex);
            const rectangle = portion.getRect();
            console.log("X = " + rectangle.x + "; Y = " + rectangle.y + "; Width = " + rectangle.width + "; Height = " + rectangle.height);
        }
    }
} finally {
    presentation.dispose();
}

取得文字 Portion 的座標

使用 Portion.getCoordinates 取得文字 Portion 起始位置的座標:

const presentation = new aspose.slides.Presentation("Shapes.pptx");
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().get_Item(0);
    const paragraphs = shape.getTextFrame().getParagraphs();

    for (let paragraphIndex = 0; paragraphIndex < paragraphs.getCount(); paragraphIndex++) {
        const paragraph = paragraphs.get_Item(paragraphIndex);
        const portions = paragraph.getPortions();

        for (let portionIndex = 0; portionIndex < portions.getCount(); portionIndex++) {
            const portion = portions.get_Item(portionIndex);
            const point = portion.getCoordinates();
            console.log("X = " + point.x + "; Y = " + point.y);
        }
    }
} finally {
    presentation.dispose();
}

常見問題

我可以只對單一段落內的部分文字套用超連結嗎?

是的,您可以對單一 Portion指派超連結,只有該片段會變成可點擊,而不是整個段落。

樣式繼承如何運作:Portion 會覆寫哪些屬性,哪些會從 Paragraph 或 TextFrame 繼承?

Portion 級別的屬性具有最高優先權。如果在 Portion 上未設定屬性,Aspose.Slides 會從 Paragraph 繼承。若該處仍未設定,則使用 TextFrametheme 的樣式。

如果針對 Portion 指定的字型在目標機器或伺服器上不存在,會發生什麼情況?

字型替換規則 會生效。文字可能重新排版:度量、斷字與寬度都可能改變,這會影響精確的定位。

我可以為特定 Portion 設定文字填色的透明度或漸層,而不影響段落其他部分嗎?

是的,Portion 級別的文字顏色、填色與透明度可以與相鄰的片段不同。