在 JavaScript 中管理 PowerPoint 文字段落
概觀
Aspose.Slides for Node.js via Java 將文字表示為文字框、段落和文字片段的層次結構:
- TextFrame 表示形狀中的文字容器,並提供對其段落集合的存取。
- Paragraph 表示文字框中的單一段落,並提供對其文字片段和段落層級格式的存取。
- Portion 表示段落內的文字執行。每個文字片段可以有自己的文字和字元層級格式。
因此,一個段落可以透過使用多個文字片段來包含字型、顏色、大小和其他格式各異的文字。
建立與格式化段落
在形狀的文字框中建立段落,並使用文字片段格式化單獨的文字執行。本節說明如何在每個段落中結合多個文字片段,並套用不同的顏色、字型大小和樣式。
使用多個文字片段建立段落
以下 JavaScript 範例建立三個段落,每個段落在由 Paragraph.getPortions 回傳的集合中包含三個文字執行。它重複使用預設段落及其空白文字片段,然後加入其餘的段落與文字片段。格式化透過 Portion.getPortionFormat 套用:每個段落的第一個文字片段為紅色、粗體、15 點;第二個為藍色、斜體、18 點。第三個文字片段保留預設格式:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 300, 150);
const textFrame = shape.getTextFrame();
const firstParagraph = textFrame.getParagraphs().get_Item(0);
firstParagraph.getPortions().add(new aspose.slides.Portion());
firstParagraph.getPortions().add(new aspose.slides.Portion());
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.getPortions().add(new aspose.slides.Portion());
secondParagraph.getPortions().add(new aspose.slides.Portion());
secondParagraph.getPortions().add(new aspose.slides.Portion());
textFrame.getParagraphs().add(secondParagraph);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.getPortions().add(new aspose.slides.Portion());
thirdParagraph.getPortions().add(new aspose.slides.Portion());
thirdParagraph.getPortions().add(new aspose.slides.Portion());
textFrame.getParagraphs().add(thirdParagraph);
const paragraphCount = textFrame.getParagraphs().getCount();
for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
const paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
const portionCount = paragraph.getPortions().getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = paragraph.getPortions().get_Item(portionIndex);
portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));
if (portionIndex === 0) {
portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
portion.getPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
portion.getPortionFormat().setFontHeight(15);
} else if (portionIndex === 1) {
portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
portion.getPortionFormat().setFontItalic(java.newByte(aspose.slides.NullableBool.True));
portion.getPortionFormat().setFontHeight(18);
}
}
}
presentation.save("paragraphs_with_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
建立項目符號與編號清單
將段落文字組織成使用符號、編號或自訂影像的清單。本節的範例涵蓋項目符號格式、多層清單,以及自訂起始編號。
建立項目符號或編號清單
項目符號與編號可讓相關項目更易於掃描。在 Aspose.Slides 中,清單設定是透過 BulletFormat 定義的。
此 JavaScript 範例在同一文字框中加入符號項目符號與編號項目。BulletFormat.setType 用於選取項目符號類型,而 BulletFormat.setChar 提供符號,BulletFormat.setNumberedBulletStyle 則選取編號樣式。兩個項目皆使用黑色項目符號,高度為 100%,段落縮排為 25 點:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const symbolParagraph = new aspose.slides.Paragraph();
symbolParagraph.setText("Welcome to Aspose.Slides");
symbolParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
symbolParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
symbolParagraph.getParagraphFormat().setIndent(25);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(symbolParagraph);
const numberedParagraph = new aspose.slides.Paragraph();
numberedParagraph.setText("This is a numbered item");
numberedParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(java.newByte(aspose.slides.NumberedBulletStyle.BulletCircleNumWDBlackPlain));
numberedParagraph.getParagraphFormat().setIndent(25);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(numberedParagraph);
presentation.save("bulleted_and_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
使用圖片項目符號
圖片項目符號讓您以自訂影像取代符號或編號。
此 JavaScript 範例從應用程式的工作目錄載入 image.png,將影像加入簡報的影像集合作為 PPImage,並將它作為段落的項目符號。它呼叫 BulletFormat.setType 使用圖片項目符號,並透過 BulletFormat.getPicture 指定影像。BulletFormat.setHeight 設為 100% 以相對於段落文字調整項目符號大小:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const bulletImage = aspose.slides.Images.fromFile("image.png");
let presentationImage;
try {
presentationImage = presentation.getImages().addImage(bulletImage);
} finally {
bulletImage.dispose();
}
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const paragraph = new aspose.slides.Paragraph();
paragraph.setText("Welcome to Aspose.Slides");
paragraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Picture));
paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
paragraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(paragraph);
presentation.save("picture_bullet.pptx", aspose.slides.SaveFormat.Pptx);
presentation.save("picture_bullet.ppt", aspose.slides.SaveFormat.Ppt);
} finally {
presentation.dispose();
}
建立多層清單
設定 ParagraphFormat.setDepth 以將段落放置於清單的不同層級。最高層的深度為 0。
此 JavaScript 範例將 ParagraphFormat.setDepth 的值從 0 到 3 指派給四個段落,在同一文字框中建立層次結構。每個段落使用符號項目符號,且 BulletFormat.setChar 在圓形項目符號與連字號之間交替,以區分相鄰層級:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("Content");
firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
firstParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setDepth(java.newShort(0));
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Second level");
secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
secondParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setDepth(java.newShort(1));
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("Third level");
thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
thirdParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
thirdParagraph.getParagraphFormat().setDepth(java.newShort(2));
const fourthParagraph = new aspose.slides.Paragraph();
fourthParagraph.setText("Fourth level");
fourthParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
fourthParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
fourthParagraph.getParagraphFormat().setDepth(java.newShort(3));
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
textFrame.getParagraphs().add(fourthParagraph);
presentation.save("multilevel_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
自訂編號清單起始值
使用 BulletFormat.setNumberedBulletStartWith 設定編號段落的起始編號。
此 JavaScript 範例將 BulletFormat.setType 設為使用編號,並為三個段落分別指派 BulletFormat.setNumberedBulletStartWith 的值 2、3 與 7。起始值為每個段落單獨設定,允許顯示的編號跳過特定數字:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("Start at 2");
firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(2));
textFrame.getParagraphs().add(firstParagraph);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Start at 3");
secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(3));
textFrame.getParagraphs().add(secondParagraph);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("Start at 7");
thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(7));
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("custom_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
控制段落布局與結尾屬性
調整段落的邊距與縮排,以控制第一行與換行行的位置。您也可以將段落結尾標記的格式與可見文字片段分開設定。
設定首行縮排
使用 ParagraphFormat.setIndent 方法控制段落的首行縮排。此設定只會移動第一行相對於段落左邊距的位移。正值會將第一行向右移動,而其餘行保持與段落本體對齊。
需要整段移動時,請使用 ParagraphFormat.setMarginLeft;僅需移動首行時,請使用 ParagraphFormat.setIndent。
下列範例為三個段落設定相同的 ParagraphFormat.setMarginLeft 為 20 點,並分別將 ParagraphFormat.setIndent 設為 0、20、40 點。首行的起始位置逐漸增大,而換行行則保留相同的左邊距:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setMarginLeft(20);
firstParagraph.getParagraphFormat().setIndent(0);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setMarginLeft(20);
secondParagraph.getParagraphFormat().setIndent(20);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
thirdParagraph.getParagraphFormat().setMarginLeft(20);
thirdParagraph.getParagraphFormat().setIndent(40);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("paragraph_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定懸掛縮排
懸掛縮排是一種段落布局,第一行起始位置位於其餘行的左側。於 Aspose.Slides 中,可使用 ParagraphFormat.setIndent 方法達成此效果。將縮排設定為負值,即可將第一行向左移動。
實務上,ParagraphFormat.setMarginLeft 定義段落本體的左側位置,而 ParagraphFormat.setIndent 定義第一行相對於該邊距的位置。若要建立懸掛縮排,請設定正向左邊距並將縮排設為負值。
此格式常用於參考文獻、書目、詞彙表條目等需要讓換行行對齊在段落本體之下,而非第一行第一個字元之下的段落。
以下 JavaScript 範例將 ParagraphFormat.setMarginLeft 設為 40 與 60 點,並將 ParagraphFormat.setIndent 設為 -20 與 -30 點。每個段落的第一行皆位於換行行左側;第二個段落的懸掛縮排更深:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.");
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setMarginLeft(40);
firstParagraph.getParagraphFormat().setIndent(-20);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setMarginLeft(60);
secondParagraph.getParagraphFormat().setIndent(-30);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("hanging_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

設定段落結尾執行屬性
Paragraph.setEndParagraphPortionFormat 方法控制段落結尾標記的格式。以下範例建立一個 PortionFormat ,將 BasePortionFormat.setFontHeight 設為 48 點,並將 BasePortionFormat.setLatinFont 設為 Times New Roman。將此格式指派給第二段落的結尾標記,保持可見文字片段不變:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, 200, 250);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.getPortions().add(new aspose.slides.Portion("Sample text"));
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.getPortions().add(new aspose.slides.Portion("Sample text 2"));
const endParagraphFormat = new aspose.slides.PortionFormat();
endParagraphFormat.setFontHeight(48);
endParagraphFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("end_paragraph_format.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
計算呈現行數
有關影響自動換行與行尾標點符號的段落規則,請參閱 Control Line Breaking 與 Control Hanging Punctuation。
使用 Paragraph.getLinesCount 可計算段落在文字布局後佔用的行數,包含自動換行。這在檢查簡報範本中的文字長度與布局時非常有用。
段落是 TextFrame.getParagraphs 中的一個項目,可能佔用多行已呈現的行。段落內的顯式換行符會強制產生新行,但不會建立另一個段落。自動換行則根據可用寬度產生行,且不會在文字中插入顯式換行符。因此,僅計算段落或換行字元並不會得到已呈現的行數。
以下範例建立一個文字形狀,計算其行數,縮小形狀後再以較短的字串取代文字。範例使用 TextFrameFormat.setWrapText 開啟換行,並使用 TextFrameFormat.setAutofitType 停用自動調整大小,使形狀寬度控制換行而不會自動縮小文字或調整形狀大小。每一次測量皆使用 Paragraph.getLinesCount。形狀尺寸以點為單位。最後,範例再加入一個段落,將文字框內的行數總合起來。
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(java.newByte(aspose.slides.NullableBool.True));
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.None));
const paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.");
console.log("Original width: " + paragraph.getLinesCount());
shape.setWidth(150);
console.log("Narrower shape: " + paragraph.getLinesCount());
paragraph.setText("Short text.");
console.log("Shorter text: " + paragraph.getLinesCount());
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Another paragraph.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
textFrame.getParagraphs().add(secondParagraph);
let totalLineCount = 0;
for (let i = 0; i < textFrame.getParagraphs().getCount(); i++) {
const currentParagraph = textFrame.getParagraphs().get_Item(i);
totalLineCount += currentParagraph.getLinesCount();
}
console.log("Total lines in the text frame: " + totalLineCount);
} finally {
presentation.dispose();
}
使用此文字與這些尺寸,縮小形狀會增加行數,而以短字串取代文字會減少行數。具體計數會因字型可用性與替代、字型大小、邊距、縮排、換行與自動調整設定而有所不同。檢查範本時請使用目標環境所預期的字型與布局設定。
僅憑行數無法判斷文字是否超出容器。可用高度、行高、段落與行間距以及自動調整行為同樣重要;即使是單行,若關閉換行,寬度仍可能超過可用空間。
匯入與匯出段落內容
使用 HTML 可將文字帶入簡報,或將現有段落匯出供外部使用。本節亦說明如何將單一段落渲染為影像,包括表格儲存格中的段落。
將 HTML 文字匯入段落
使用 ParagraphCollection.addFromHtml 將 HTML 標記轉換為文字框中的段落與文字片段。
此 JavaScript 範例從應用程式工作目錄的 file.html 讀取 HTML,並將標記傳遞給 ParagraphCollection.addFromHtml。它會清除文字框的預設段落,使框僅保留匯入的內容。矩形形狀在投影片四周留有 10 點的邊距:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shapeWidth = presentation.getSlideSize().getSize().getWidth() - 20;
const shapeHeight = presentation.getSlideSize().getSize().getHeight() - 20;
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getTextFrame().getParagraphs().clear();
const html = fs.readFileSync("file.html", "utf8");
shape.getTextFrame().getParagraphs().addFromHtml(html);
presentation.save("html_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
將段落文字匯出為 HTML
使用 ParagraphCollection.exportToHtml 將選取的段落範圍匯出為 HTML。當在網站上重新使用簡報文字時,您可能希望保留本地的強調效果,同時不帶入簡報的字型、大小或文字方塊樣式。設定 TextToHtmlConversionOptions.setTextInheritanceLimit 以控制匯出器在格式繼承上的深度。
下載 範例簡報 並放置於應用程式的工作目錄。其第一張投影片包含一個文字形狀,文字為「Inherited text; explicit red 18pt」。輸入特意使用了多層格式:
- 簡報的預設文字樣式提供 Calibri 與 28 pt 大小。
- 文字框的第一層文字樣式提供藍色文字與斜體。
- 段落明確設定為粗體。
- 第二個文字片段「explicit red 18pt」明確設定為紅色文字且大小為 18 pt。
以下完整範例將相同段落匯出三次。它將 TextToHtmlConversionOptions 傳遞給匯出方法,並將每個結果寫入獨立的 UTF-8 HTML 檔案:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");
const presentation = new aspose.slides.Presentation("text-inheritance.pptx");
try {
const shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
const textFrame = shape.getTextFrame();
if (textFrame !== null) {
const paragraphs = textFrame.getParagraphs();
const modes = [aspose.slides.TextInheritanceLimit.All, aspose.slides.TextInheritanceLimit.TextBox, aspose.slides.TextInheritanceLimit.ParagraphOnly];
const fileNames = ["All.html", "TextBox.html", "ParagraphOnly.html"];
for (let i = 0; i < modes.length; i++) {
const options = new aspose.slides.TextToHtmlConversionOptions();
options.setTextInheritanceLimit(modes[i]);
const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
fs.writeFileSync(fileNames[i], html, "utf8");
}
} else {
console.log("The first shape does not contain a text frame.");
}
} else {
console.log("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
產生的 HTML 為第一個文字片段「Inherited text; 」包含以下 CSS。表格列出相關宣告,其他產生的屬性為了清晰起見被省略。
| 輸出 | 第一段文字的相關 CSS | 外觀 |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
藍色、粗體、斜體文字,28 pt,使用 Calibri(若該字型可用)。 |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
藍色、粗體、斜體文字;字型與大小由頁面或瀏覽器提供。 |
ParagraphOnly.html |
font-weight:bold |
粗體文字;字型、大小、顏色與斜體樣式由頁面或瀏覽器提供。 |
第二個文字片段在所有三個輸出中皆保留 color:#ff0000、font-size:18pt 與 font-weight:bold。在 All 與 TextBox 中仍保留斜體,而 ParagraphOnly 則未保留。限制繼承因此不會移除明確指派給段落或文字片段的格式。
在同一瀏覽器中開啟這些檔案以比較結果。於受限制的兩種模式中,遺漏的 CSS 屬性會由目標頁面的樣式或瀏覽器預設解決,最終外觀取決於 HTML 被使用的環境。
將段落渲染為影像
Paragraph.getImage 直接渲染單一段落,並返回一個 IImage。可使用 IImage.save 將結果存檔或寫入串流。您不必渲染整個形狀或手動裁切位圖。
如果段落在其父集合中找不到、沒有有效的渲染邊界,或無法渲染,Paragraph.getImage 可能會傳回 null。請在保存之前檢查結果,使用後釋放返回的影像。
以預設比例呈現段落
此範例建立一個包含三個段落的文字方塊於單一投影片上。

以下範例選取索引為 1 的第二段落,呼叫 Paragraph.getImage 而不傳遞縮放參數。它先檢查形狀是否包含所需段落,且渲染返回影像後才呼叫 IImage.save 以 PNG 格式保存。finally 區塊會在使用後釋放影像。
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const sourceShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 400, 100);
const sourceTextFrame = sourceShape.getTextFrame();
sourceTextFrame.getParagraphs().clear();
for (const text of ["First paragraph", "Second paragraph", "Third paragraph"]) {
const sourceParagraph = new aspose.slides.Paragraph();
sourceParagraph.setText(text);
sourceTextFrame.getParagraphs().add(sourceParagraph);
}
const shape = slide.getShapes().get_Item(0);
if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
const textFrame = shape.getTextFrame();
if (textFrame !== null && textFrame.getParagraphs().getCount() > 1) {
const paragraph = textFrame.getParagraphs().get_Item(1);
const paragraphImage = paragraph.getImage();
if (paragraphImage !== null) {
try {
paragraphImage.save("paragraph.png", aspose.slides.ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
console.log("The paragraph could not be rendered.");
}
} else {
console.log("The expected paragraph was not found.");
}
} else {
console.log("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
結果:

在表格儲存格中以縮放呈現段落
使用接受 scaleX 與 scaleY 參數的 Paragraph.getImage 重載,以設定水平與垂直縮放系數。以下範例建立一個表格,將第一個儲存格中的段落以預設寬高的兩倍渲染,並將結果保存為 PNG 影像。
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const scaleX = 2;
const scaleY = 2;
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [300]);
const rowHeights = java.newArray("double", [80]);
const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
const paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
paragraph.setText("Text in a table cell");
const paragraphImage = paragraph.getImage(scaleX, scaleY);
if (paragraphImage !== null) {
try {
paragraphImage.save("table_paragraph.png", aspose.slides.ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
console.log("The paragraph could not be rendered.");
}
} finally {
presentation.dispose();
}
縮放系數為 1 時保持該軸的預設像素大小。例如,兩個系數皆為 2 時,產生的影像寬度與高度大約是預設尺寸的兩倍,像素數量約為四倍。較大的系數通常能提供放大或高解析度輸出的更銳利文字,但也會增加記憶體使用量與檔案大小。系數小於 1 會產生較小且細節較少的影像。使用相等的系數可保留段落的長寬比;不同的水平與垂直系數會分別拉伸輸出。
在需要包含形狀填色、邊框或其他視覺上下文時,仍可使用 Shape.getImage 來渲染整個形狀。若僅需段落影像,請使用 Paragraph.getImage。
常見問題
我可以完全停用文字框內的換行嗎?
可以。將 TextFrameFormat.setWrapText 設為停用,即可關閉換行,使行不會在文字框邊緣斷開。
如何取得特定段落在投影片上的精確邊界?
使用 Paragraph.getRect 取得段落的邊界矩形。Portion.getRect 則提供單一文字片段的邊界。
段落對齊(左、右、置中或兩端對齊)在何處控制?
ParagraphFormat.setAlignment 是段落層級的設定,會套用於整個段落,與個別文字片段的格式無關。
若要在同一行內垂直對齊不同字型大小的文字片段,請參閱 Align Fonts Within a Line。
我可以為段落的一部分設定校對語言嗎?
可以。對個別文字片段使用 BasePortionFormat.setLanguageId 設定,即可讓同一段落包含多種語言的文字。