JavaScriptでPowerPointテキスト段落を管理する
概要
Aspose.Slides for Node.js via Java は、テキストをテキストフレーム、段落、ポーションの階層として表します。
- TextFrame は、シェイプ内のテキスト コンテナを表し、段落コレクションへのアクセスを提供します。
- Paragraph は、テキストフレーム内の 1 つの段落を表し、ポーションおよび段落レベルの書式設定へのアクセスを提供します。
- Portion は、段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。
したがって、段落は複数のポーションを使用して、フォント、色、サイズ、その他の書式が異なるテキストを含めることができます。
段落の作成と書式設定
シェイプのテキストフレーム内で段落を作成し、ポーションを使用して個々のテキスト ランの書式を設定します。このセクションでは、各段落に複数のポーションを組み合わせ、異なる色、フォントサイズ、スタイルを適用する方法を示します。
複数のポーションを持つ段落の作成
次の JavaScript の例は、Paragraph.getPortions が返すコレクション内に 3 つのテキスト ランを含む 3 つの段落を作成します。デフォルトの段落と空のポーションを再利用し、残りの段落とポーションを追加します。書式は Portion.getPortionFormat を介して適用されます。各段落の最初のポーションは赤、太字、15pt、2 番目は青、斜体、18pt、3 番目はデフォルトの書式のままです。
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% 、段落インデントは 25pt に設定されています。
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 まで 4 つの段落に割り当て、1つのテキストフレーム内に階層を作ります。各段落はシンボル箇条書きを使用し、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 の例は、3 つの段落に対して 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 を使用します。
以下の例は、3 つの段落に同じ ParagraphFormat.setMarginLeft 20pt と、ParagraphFormat.setIndent をそれぞれ 0、20、40pt に設定します。先頭行は徐々に大きなオフセットで開始し、折り返し行は同じ左余白を保持します。
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 を 40pt と 60pt に、ParagraphFormat.setIndent を -20pt と -30pt にそれぞれ組み合わせます。各段落で先頭行は折り返し行の左側に開始し、2 番目の段落はより深い行下げインデントになります。
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 を 48pt、BasePortionFormat.setLatinFont を Times New Roman に設定します。この書式を第2段落の終端記号に割り当て、表示テキストのポーションは変更しません。
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 つで、複数の描画行を占めることがあります。段落内の明示的な改行は新しい段落を作成せずに新行を強制します。自動折り返しは利用可能な幅に基づいて行を作成し、テキストに明示的な改行文字を挿入しません。そのため、段落数や改行文字の数だけで描画行数を求めることはできません。
以下の例はテキスト シェイプを作成し、行数を取得し、シェイプを狭め、テキストを短い文字列に置き換えます。例では 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();
}
このテキストと寸法でシェイプを狭めると行数が増加し、短い文字列に置き換えると減少します。正確な行数はフォントの可用性と置換、フォントサイズ、余白、インデント、折り返し、オートフィット設定により変わります。テンプレートをチェックする際は、対象環境で使用するフォントとレイアウト設定を使用してください。
行数だけではテキストがコンテナからはみ出すかどうかは判断できません。利用可能な高さ、行の高さ、段落および行間、オートフィット動作も考慮する必要があります。折り返しが無効の場合、1 行だけでも幅を超えることがあります。
段落コンテンツのインポートとエクスポート
HTML を使用してテキストをプレゼンテーションに取り込んだり、既存の段落を外部で使用できるようにエクスポートしたりします。このセクションでは、テーブル セル内の段落を含む、個々の段落を画像としてレンダリングする方法も示します。
HTML テキストを段落にインポート
ParagraphCollection.addFromHtml を使用して、HTML マークアップをテキストフレーム内の段落とポーションに変換します。
この JavaScript の例は、アプリケーションの作業ディレクトリにある file.html から HTML を読み取り、マークアップを ParagraphCollection.addFromHtml に渡します。テキストフレームのデフォルト段落をクリアして、フレームにインポートされたコンテンツだけが残ります。矩形シェイプはスライドの各側に 10pt の余白を残します。
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 としてエクスポートします。プレゼンテーションのテキストを Web サイトで再利用する場合、フォントやサイズ、テキスト ボックスのスタイルを持ち込まずにローカルの強調表示だけを保持したいことがあります。その際は、TextToHtmlConversionOptions.setTextInheritanceLimit を設定して、エクスポート時の書式継承の深さを制御します。
サンプル プレゼンテーション をダウンロードし、アプリケーションの作業ディレクトリに配置します。最初のスライドには、テキスト “Inherited text; explicit red 18pt” を含むテキスト シェイプが 1 つあります。入力は意図的に複数の書式レベルを使用しています。
- プレゼンテーションのデフォルト テキスト スタイルは Calibri と 28pt を提供します。
- テキストフレームの第一レベル テキスト スタイルは青色テキストと斜体を提供します。
- 段落は太字テキストを明示的に設定します。
- 第二ポーション “explicit red 18pt” は赤色テキストと 18pt を明示的に設定します。
以下の完全な例は、同じ段落を 3 回エクスポートします。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 |
Calibri が利用可能な場合、青、太字、斜体の 28pt テキスト |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
青、太字、斜体のテキスト。フォントファミリとサイズはページまたはブラウザが供給 |
ParagraphOnly.html |
font-weight:bold |
太字テキスト。フォントファミリ、サイズ、色、斜体はページまたはブラウザが供給 |
第2ポーションはすべての出力で color:#ff0000、font-size:18pt、font-weight:bold を保持します。また、All と TextBox では斜体も保持され、ParagraphOnly では保持されません。継承を制限しても、段落またはポーションに明示的に割り当てられた書式は削除されません。
同じブラウザでファイルを開いて比較してください。2 つの制限モードでは、欠落した CSS プロパティが転送先ページのスタイルまたはブラウザのデフォルトで解決されるため、最終的な外観は HTML の使用場所に依存します。
HTML クリップボード フラグメントの生成
TextToHtmlConversionOptions.setAddClipboardFragmentHeader を使用して、ParagraphCollection.exportToHtml の出力を選択します。false の場合、メソッドは普通の HTML を返します。true の場合、返される文字列は Windows クリップボード ヘッダーで始まり、StartHTML と EndHTML が HTML のバイトオフセット、StartFragment と EndFragment が貼り付け対象のコンテンツを示します。
この例は、2 つの書式付き段落を普通の HTML (html) と HTML クリップボード フラグメント (clipboardHtml) の両方でエクスポートします。アプリケーションはそのフラグメントをシステム クリップボードに書き込むことができます。クリップボード API が生バイトを期待する場合は、BOM なしの UTF‑8 としてエンコードしてください。
const slides = require("aspose.slides.via.java");
const presentation = new slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(slides.ShapeType.Rectangle, 0, 0, 400, 100);
const paragraphs = shape.getTextFrame().getParagraphs();
paragraphs.clear();
paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
const options = new slides.TextToHtmlConversionOptions();
options.setAddClipboardFragmentHeader(false);
const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
options.setAddClipboardFragmentHeader(true);
const clipboardHtml = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
} finally {
presentation.dispose();
}
段落を画像としてレンダリング
Paragraph.getImage は個々の段落を直接レンダリングし、IImage を返します。結果は IImage.save でファイルまたはストリームに保存できます。 containing shape をレンダリングしたり、ビットマップを手動で切り取る必要はありません。
Paragraph.getImage は、段落が親コレクションに見つからない、描画境界が有効でない、またはレンダリングできない場合に null を返すことがあります。保存する前に結果を確認し、使用後は返された画像を破棄してください。
デフォルト スケールで段落をレンダリング
この例は、1 つのスライドを持つプレゼンテーションを作成し、最初のシェイプが 3 つの段落を含むテキスト ボックスです。

以下の例はインデックス 1 の第2段落を選択し、スケール パラメータなしで 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 のオーバーロードを使用して、横方向と縦方向のスケール係数を設定します。以下の例はテーブルを作成し、最初のセル内の段落を横・縦ともデフォルトの 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 つの段落内で複数の言語のテキストを扱うことができます。