在 JavaScript 中格式化簡報文字
概述
本文說明如何使用 Aspose.Slides for Node.js via Java 於 PowerPoint 與 OpenDocument 簡報中格式化文字。內容涵蓋背景色、透明度、字元間距、字型屬性、旋轉、段落間距、自動調整行為、文字錨點、定位點以及語言設定。
在以下範例中,我們使用名為 sample.pptx 的檔案,該檔案的第一張投影片上有一個文字方塊,文字內容如下:

若要尋找並突出顯示文字或正則表達式匹配項,請參閱 Search and Replace Text。
設定文字背景色
使用 ParagraphFormat.getDefaultPortionFormat 為段落設定預設的突顯顏色,或使用 BasePortionFormat.getHighlightColor 為單獨的文字區段設定。
以下程式碼示範如何為 整個段落 設定背景色:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 設定整個段落的突顯顏色。
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
presentation.save("gray_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼示範如何為 粗體字的文字區段 設定背景色:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// 設定文字區段的突顯顏色。
portion.getPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
}
}
presentation.save("gray_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

對齊文字段落
使用 ParagraphFormat.setAlignment 設定文字框內段落的對齊方式。可設定為置中、左對齊、右對齊、兩端對齊等。
以下程式碼示範如何將段落 置中:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 設定段落的對齊方式為置中。
paragraph.getParagraphFormat().setAlignment(aspose.slides.TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定文字透明度
文字透明度透過指派給 BasePortionFormat.getFillFormat 的顏色之 alpha 成分來控制。以下範例中的 alpha = 50 為 ARGB alpha 通道值,範圍 0–255,並非透明度百分比。
以下程式碼示範如何為 整個段落 套用透明度:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const fillFormat = paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat();
// 設定文字的填充顏色為透明顏色。
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
presentation.save("transparent_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼示範如何為 粗體字的文字區段 套用透明度:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const fillFormat = portion.getPortionFormat().getFillFormat();
// 設定文字區段的透明度。
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
}
}
presentation.save("transparent_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定文字字元間距
使用 BasePortionFormat.setSpacing 來擴展或縮緊文字方塊內字元之間的間距。
以下 JavaScript 程式碼示範如何在 整個段落 中擴展字元間距:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 注意:使用負值可壓縮字元間距。
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // 展開字元間距。
presentation.save("character_spacing_in_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼示範如何在 粗體字的文字區段 中擴展字元間距:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// 注意:使用負值可壓縮字元間距。
portion.getPortionFormat().setSpacing(3); // 展開字元間距。
}
}
presentation.save("character_spacing_in_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

停用特定字型的字距調整 (Kerning)
在某些情況下,Aspose.Slides 所渲染的文字會比 PowerPoint 中的相同文字稍微緊密。這可能是因為 PowerPoint 會忽略某些字型的字距調整資料,即使該字型本身具有有效的字距資訊且在 PowerPoint 設定中已啟用字距調整。
若要使渲染結果更接近 PowerPoint,可對使用受影響字型的文字區段停用字距調整。將 BasePortionFormat.setKerningMinimalSize 設為遠大於實際字型大小的值:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraphs = autoShape.getTextFrame().getParagraphs();
const paragraphCount = paragraphs.getCount();
const targetFont = "Roboto";
for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
const portions = paragraphs.get_Item(paragraphIndex).getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
const portionFormat = portion.getPortionFormat();
const latinFont = portionFormat.getLatinFont();
const eastAsianFont = portionFormat.getEastAsianFont();
const complexScriptFont = portionFormat.getComplexScriptFont();
if ((latinFont !== null && latinFont.getFontName() === targetFont) ||
(eastAsianFont !== null && eastAsianFont.getFontName() === targetFont) ||
(complexScriptFont !== null && complexScriptFont.getFontName() === targetFont)) {
portionFormat.setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
此設定會阻止對符合條件的文字區段套用字距調整,協助 Aspose.Slides 的渲染與 PowerPoint 在受影響字型上的視覺輸出保持一致。
管理文字字型屬性
可透過 ParagraphFormat.getDefaultPortionFormat 在段落層級設定字型屬性,或透過 PortionFormat 在個別區段設定。
以下程式碼為整個段落設定字型與文字樣式:套用字型大小、粗體、斜體、點狀底線以及 Times New Roman 字型至段落內所有區段。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const defaultPortionFormat = paragraph.getParagraphFormat().getDefaultPortionFormat();
// 設定段落的字型屬性。
defaultPortionFormat.setFontHeight(12);
defaultPortionFormat.setFontBold(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
defaultPortionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼為 粗體字的文字區段 套用相同屬性:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const portionFormat = portion.getPortionFormat();
// 設定文字區段的字型屬性。
portionFormat.setFontHeight(13);
portionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
portionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
portionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定文字旋轉
使用 TextFrameFormat.setTextVerticalType 在形狀內設定預定義的文字方向。
以下程式碼將文字方向設為 Vertical270,即文字 逆時針旋轉 90 度:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(java.newByte(aspose.slides.TextVerticalType.Vertical270));
presentation.save("text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

為文字框設定自訂旋轉角度
使用 TextFrameFormat.setRotationAngle 為 TextFrame 設定自訂的旋轉角度。
以下程式碼將文字框在形狀內順時針旋轉 3 度:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定段落的行距
Aspose.Slides 提供 ParagraphFormat.setSpaceAfter、ParagraphFormat.setSpaceBefore 與 ParagraphFormat.setSpaceWithin 以控制段落間距。這些屬性的使用方式如下:
- 使用正值表示行距為行高的百分比。
- 使用負值表示行距以點 (pt) 為單位。
以下程式碼示範如何在段落內設定行距:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定文字框的自動調整類型
TextFrameFormat.setAutofitType 決定文字超出容器邊界時的行為。可用來控制文字是否縮小、溢出或自動調整形狀大小。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
presentation.save("autofit_type.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定文字框的錨點
TextFrameFormat.setAnchoringType 定義文字在形狀內的垂直位置,例如置頂、置中或置底。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(java.newByte(aspose.slides.TextAnchorType.Bottom));
presentation.save("text_anchor.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定文字定位點 (Tab)
使用 ParagraphFormat.setDefaultTabSize 以及 ParagraphFormat.getTabs 來配置段落的定位點。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, java.newByte(aspose.slides.TabAlignment.Left));
presentation.save("paragraph_tabs.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定校對語言
Aspose.Slides 提供 BasePortionFormat.setLanguageId,可為文字區段設定校對語言。校對語言決定在 PowerPoint 中執行拼寫與文法檢查時使用的語言。
以下程式碼示範如何為文字區段設定校對語言:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
const font = new aspose.slides.FontData("SimSun");
const textPortion = new aspose.slides.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", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
設定預設語言
使用 LoadOptions.setDefaultTextLanguage 來定義在載入或建立簡報時,所產生文字的預設語言。
const aspose = { slides: require("aspose.slides.via.java") };
const loadOptions = new aspose.slides.LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
const presentation = new aspose.slides.Presentation(loadOptions);
try {
const slide = presentation.getSlides().get_Item(0);
// 新增一個帶文字的矩形形狀。
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// 檢查第一個文字區段的語言。
const portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
設定預設文字樣式
若要在簡報層級套用預設文字格式,請使用 Presentation.getDefaultTextStyle。
以下程式碼示範如何在新簡報的所有投影片中,設定預設的粗體字型,字型大小為 14 pt。
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
// 取得頂層段落格式。
const paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat !== null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
}
presentation.save("default_text_style.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
擷取帶有全部大寫效果的文字
在 PowerPoint 中,套用 All Caps 字型效果會使文字在投影片上以大寫形式顯示,即使原始輸入為小寫。當使用 Aspose.Slides 取得此類文字區段時,函式庫會返回其原始輸入內容。若要讓取得的文字與顯示一致,請檢查 TextCapType 並在值為 All 時將返回的字串轉為大寫。
以下為 sample2.pptx 首張投影片中的文字方塊範例:

以下程式碼示範如何擷取帶有 All Caps 效果的文字:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample2.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log("Original text: " + textPortion.getText());
const textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() === aspose.slides.TextCapType.All) {
const text = textPortion.getText().toUpperCase();
console.log("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
輸出:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常見問題
如何在投影片的表格中修改文字?
使用 Table 來遍歷儲存格,並透過 Cell.getTextFrame 取得文字框,使用 Paragraph.getParagraphFormat 變更段落格式。
如何在 PowerPoint 投影片的文字上套用漸層顏色?
使用 BasePortionFormat.getFillFormat,將 FillFormat.setFillType 設為 FillType.Gradient,並配置漸層停止點、方向與透明度。