JavaScript で PowerPoint テキスト段落を管理
概要
Aspose.Slides for Node.js via Java は、テキストをテキスト フレーム、段落、ポーションの階層として表現します。
- TextFrame は、シェイプ内のテキスト コンテナを表し、その段落コレクションへのアクセスを提供します。
- Paragraph は、テキスト フレーム内の 1 つの段落を表し、そのポーションと段落レベルの書式設定へのアクセスを提供します。
- Portion は、段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。
したがって、段落は複数のポーションを使用することで、異なるフォント、色、サイズ、その他の書式設定を持つテキストを含めることができます。
段落の作成と書式設定
複数のポーションを持つ段落の作成
以下の手順では、3 つの段落を持ち、各段落に 3 つのポーションが含まれるテキスト フレームを作成します。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象のスライドにアクセスします。
- スライドに長方形の AutoShape を追加します。
- シェイプの TextFrame にアクセスします。
- デフォルトの段落を使用し、テキスト フレームにさらに 2 つの Paragraph オブジェクトを追加します。
- 各段落が 3 つのポーションを含むように十分な数の Portion オブジェクトを追加します。デフォルトの段落はすでに空のポーションを 1 つ含んでいます。
- 各ポーションのテキストを設定します。
- Portion.getPortionFormat を使用して文字レベルの書式設定を適用します。
- 変更したプレゼンテーションを保存します。
この JavaScript の例は手順を実装しています:
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 を使用して定義されます。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象のスライドにアクセスします。
- 選択したスライドに AutoShape を追加します。
- シェイプの TextFrame にアクセスします。
- テキスト フレームからデフォルトの段落を削除します。
- 記号箇条書き用に Paragraph を作成します。
- BulletFormat.setType を BulletType.Symbol に設定し、箇条書き文字を指定します。
- 段落のテキスト、インデント、箇条書きの色、箇条書きの高さを設定します。
- 段落をテキスト フレームに追加します。
- 2 番目の段落を作成し、BulletFormat.setType を BulletType.Numbered に設定します。
- 番号付き箇条書きのスタイルを構成し、段落をテキスト フレームに追加します。
- プレゼンテーションを保存します。
この JavaScript の例は記号箇条書きと番号付き箇条書きを作成します:
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();
}
画像箇条書きの使用
画像箇条書きを使用すると、記号や番号の代わりにカスタム画像を使用できます。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象のスライドにアクセスします。
- AutoShape を追加し、その TextFrame にアクセスします。
- テキスト フレームからデフォルトの段落を削除します。
- 箇条書き画像を読み込み、プレゼンテーションの画像コレクションに PPImage として追加します。
- Paragraph を作成し、テキストを設定します。
- BulletFormat.setType を BulletType.Picture に設定します。
- BulletFormat.getPicture で画像を割り当て、箇条書きの高さを設定します。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
この JavaScript の例は画像箇条書きを作成します:
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 です。
- Presentation を作成し、スライドにアクセスします。
- AutoShape を追加し、テキスト フレームのデフォルト段落をクリアします。
- 4 つの段落を作成し、箇条書きシンボルを構成します。
- それらの ParagraphFormat.setDepth の値を
0、1、2、3に設定します。 - 段落をテキスト フレームに追加し、プレゼンテーションを保存します。
この JavaScript の例は 4 レベルの箇条書きリストを作成します:
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 を使用して、番号付き段落の最初に表示される番号を設定します。
- Presentation を作成し、スライドに AutoShape を追加します。
- シェイプのテキスト フレームからデフォルトの段落をクリアします。
- 番号付き段落を 3 つ作成します。
- 各段落に対して、BulletFormat.setNumberedBulletStartWith をそれぞれ
2、3、7に設定します。 - 段落をテキスト フレームに追加し、プレゼンテーションを保存します。
この JavaScript の例は各段落にカスタム開始番号を割り当てます:
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.setIndent の値を適用して、最初の行インデントが段落レイアウトに与える影響を示します。
- Presentation クラスのインスタンスを作成します。
- 対象のスライドにアクセスします。
- スライドに長方形の AutoShape を追加します。
- シェイプの TextFrame にアクセスし、デフォルトの段落を削除します。
- 複数の段落を作成し、それぞれに異なる ParagraphFormat.setIndent の値を設定します。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
このコードは段落のインデント設定方法を示しています:
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 がその余白に対する最初の行の位置を定義します。ぶら下げインデントを作成するには、setMarginLeft に正の値を、setIndent に負の値を渡します。
この書式は、参考文献、文献リスト、用語集項目など、折り返し行が段落本体の下に揃う必要がある場合に便利です。
- Presentation クラスのインスタンスを作成します。
- 対象のスライドにアクセスします。
- スライドに長方形の AutoShape を追加します。
- シェイプの TextFrame にアクセスし、デフォルトの段落を削除します。
- 段落を作成し、各段落に対して ParagraphFormat.setMarginLeft に正の値を設定します。
- ParagraphFormat.setIndent に負の値を渡してぶら下げインデント効果を作ります。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
このコードは段落のぶら下げインデント設定方法を示しています:
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 は段落終了記号の書式設定を制御します。次の例では、2 番目の段落の終了記号にフォントサイズとラテン文字フォントを割り当てます。
- Presentation を作成またはロードし、スライドにアクセスします。
- AutoShape を追加し、デフォルトの段落をクリアします。
- 2 つの段落を作成し、テキスト ポーションを追加します。
- 2 番目の段落の終端記号用に PortionFormat を作成します。
- BasePortionFormat.setFontHeight と BasePortionFormat.setLatinFont を設定します。
- Paragraph.setEndParagraphPortionFormat で書式を割り当て、プレゼンテーションを保存します。
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 の 1 要素であり、複数のレンダリング行を占めることがあります。段落内の明示的な改行は新しい行を強制しますが、別の段落は作成しません。自動折り返しはテキストに明示的な改行を挿入せず、利用可能な幅に基づいて行を生成します。そのため、段落数や改行文字のカウントだけではレンダリングされた行数は得られません。
以下の例はテキスト シェイプを作成し、行数をカウントし、シェイプを狭め、そしてテキストを短い文字列に置き換えます。折り返しは有効にし、オートフィットは無効にしているため、シェイプ幅が折り返しを制御し、テキストやシェイプの自動縮小は行われません。シェイプの寸法はポイント単位です。最後に、例は別の段落を追加し、テキスト フレーム全体の行数を合計します。
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();
}
このテキストとこれらの寸法では、シェイプを狭めると行数が増加し、短い文字列に置き換えると減少します。正確なカウントは使用フォントの可用性と代替、フォントサイズ、余白、インデント、折り返し、オートフィット設定により変わります。テンプレートを確認する際は、対象環境で使用するフォントとレイアウト設定を使用してください。
行数だけではテキストがコンテナを超えているかは判断できません。利用可能な高さ、行の高さ、段落と行間、オートフィット動作も重要です。折り返しが無効の場合、1 行だけでも利用可能な幅を超えることがあります。
段落コンテンツのインポートとエクスポート
HTML テキストの段落へのインポート
HTML マークアップをテキスト フレーム内の段落とポーションに変換するには、ParagraphCollection.addFromHtml を使用します。
- Presentation クラスのインスタンスを作成します。
- スライドにアクセスし、AutoShape を追加します。
- シェイプの TextFrame にアクセスし、デフォルトの段落をクリアします。
- ソース HTML 文字列を定義または読み取ります。
- HTML 文字列を ParagraphCollection.addFromHtml に渡します。
- 変更したプレゼンテーションを保存します。
この JavaScript の例は HTML をテキスト フレームにインポートします:
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 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 = "<p><b>Aspose.Slides</b> imports HTML text into presentation paragraphs.</p>";
shape.getTextFrame().getParagraphs().addFromHtml(html);
presentation.save("html_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
段落テキストの HTML へのエクスポート
選択した範囲の段落を HTML としてエクスポートするには、ParagraphCollection.exportToHtml を使用します。
- Presentation のインスタンスを作成またはロードします。
- スライドにアクセスし、テキストを含む AutoShape を見つけます。
- シェイプの TextFrame にアクセスします。
- 開始段落インデックスとエクスポートする段落数を指定して ParagraphCollection.exportToHtml を呼び出します。
- 返された HTML 文字列をファイルに書き込みます。
この自己完結型 JavaScript の例はテキスト シェイプを作成し、すべての段落をエクスポートします:
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 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) {
const paragraphs = textFrame.getParagraphs();
const html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
fs.writeFileSync("paragraphs.html", 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();
}
段落を画像としてレンダリングする
Paragraph.getImage は個々の段落を直接レンダリングし、IImage を返します。結果は IImage.save を使用してファイルに保存できます。シェイプ全体をレンダリングしたり、ビットマップを手動で切り取る必要はありません。
段落が親コレクションに存在しない、または有効な描画境界がない、あるいはレンダリングできない場合、Paragraph.getImage は null を返すことがあります。保存する前に結果を確認し、使用後は返された画像を破棄してください。
デフォルトスケールで段落をレンダリング
以下のテキスト ボックスには 3 つの段落が含まれています:

以下の例は通常のテキスト シェイプ内の 2 番目の段落をデフォルトスケールでレンダリングし、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 のオーバーロードを使用します。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅・高さの 2 倍でレンダリングし、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 にすると、幅と高さが概ねデフォルトの 2 倍になり、ピクセル数は 4 倍になります。大きな係数はズームや高解像度出力でテキストをより鮮明にしますが、メモリ使用量とファイルサイズも増加します。1 未満の係数は詳細が減少した小さな画像を生成します。段落のアスペクト比を保ちたい場合は同一係数を使用し、水平と垂直で異なる係数を指定すると出力がそれぞれ伸縮します。
出力にシェイプの塗りつぶし、境界線、その他のビジュアル コンテキストが必要な場合は、Shape.getImage でシェイプ全体をレンダリングするのが有用です。段落のみの画像が必要な場合は、Paragraph.getImage を使用してください。
FAQ
テキスト フレーム内の行折り返しを完全に無効にできますか?
はい。TextFrameFormat.setWrapText を設定してラップを無効にすると、テキスト フレームの端で行が折れません。
特定の段落のスライド上での正確な境界を取得するにはどうすればよいですか?
Paragraph.getRect を使用して段落の境界矩形を取得できます。Portion.getRect は個々のポーションの境界を提供します。
段落の配置(左寄せ、右寄せ、中央寄せ、両端揃え)はどこで制御されますか?
ParagraphFormat.setAlignment は段落レベルの設定であり、個々のポーションの書式設定に関係なく段落全体に適用されます。
各行内でフォントサイズが異なるポーションを垂直方向に揃える方法については、Align Fonts Within a Line を参照してください。
段落の一部に対して校正言語を設定できますか?
はい。個々のポーションに対して BasePortionFormat.setLanguageId を設定すれば、1 つの段落内で複数の言語のテキストを扱うことができます。