PHPでPowerPointテキスト段落を管理する
概要
Aspose.Slides for PHP via Java は、テキストをテキストフレーム、段落、ポーションの階層で表します:
- TextFrame は、シェイプ内のテキスト コンテナを表し、段落コレクションへのアクセスを提供します。
- Paragraph は、テキストフレーム内の 1 つの段落を表し、ポーションと段落レベルの書式設定へのアクセスを提供します。
- Portion は、段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。
したがって、段落は複数のポーションを使用することで、異なるフォント、色、サイズ、その他の書式設定を持つテキストを含めることができます。
段落の作成と書式設定
シェイプのテキストフレーム内で段落を作成し、ポーションを使用して個々のテキスト ランの書式設定を行います。このセクションでは、各段落内で複数のポーションを組み合わせ、異なる色、フォント サイズ、スタイルを適用する方法を示します。
複数のポーションを使用した段落の作成
以下の PHP の例は、Paragraph::getPortions が返すコレクション内に 3 つのテキスト ランを含む 3 つの段落を作成します。デフォルトの段落と空のポーションを再利用し、残りの段落とポーションを追加します。書式設定は Portion::getPortionFormat を通じて適用されます。各段落の最初のポーションは赤、太字、15 ポイントです。2 番目は青、イタリック、18 ポイントです。3 番目のポーションはデフォルトの書式設定のままです:
use aspose\slides\FillType;
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 150, 300, 150);
$textFrame = $shape->getTextFrame();
$firstParagraph = $textFrame->getParagraphs()->get_Item(0);
$firstParagraph->getPortions()->add(new Portion());
$firstParagraph->getPortions()->add(new Portion());
$secondParagraph = new Paragraph();
$secondParagraph->getPortions()->add(new Portion());
$secondParagraph->getPortions()->add(new Portion());
$secondParagraph->getPortions()->add(new Portion());
$textFrame->getParagraphs()->add($secondParagraph);
$thirdParagraph = new Paragraph();
$thirdParagraph->getPortions()->add(new Portion());
$thirdParagraph->getPortions()->add(new Portion());
$thirdParagraph->getPortions()->add(new Portion());
$textFrame->getParagraphs()->add($thirdParagraph);
$paragraphCount = java_values($textFrame->getParagraphs()->getCount());
for ($paragraphIndex = 0; $paragraphIndex < $paragraphCount; $paragraphIndex++) {
$paragraph = $textFrame->getParagraphs()->get_Item($paragraphIndex);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
$portion->setText("Portion " . ($paragraphIndex + 1) . "." . ($portionIndex + 1));
if ($portionIndex == 0) {
$portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$portion->getPortionFormat()->setFontBold(NullableBool::True);
$portion->getPortionFormat()->setFontHeight(15);
} else if ($portionIndex == 1) {
$portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$portion->getPortionFormat()->setFontItalic(NullableBool::True);
$portion->getPortionFormat()->setFontHeight(18);
}
}
}
$presentation->save("paragraphs_with_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
箇条書きおよび番号付きリストの作成
段落テキストを記号、番号、またはカスタム画像を使ったリストに整理します。このセクションの例は、箇条書きの書式設定、階層リスト、カスタム開始番号をカバーします。
箇条書きまたは番号付きリストの作成
箇条書きと番号付けにより、関連項目を簡単に閲覧できます。Aspose.Slides では、リストの設定は BulletFormat を通じて定義されます。
この PHP の例は、同じテキストフレームに記号箇条書きと番号項目を追加します。BulletFormat::setType は箇条書きのタイプを選択し、BulletFormat::setChar は記号を指定し、BulletFormat::setNumberedBulletStyle は番号スタイルを選択します。両方の項目は、100% の高さと 25 ポイントの段落インデントを持つ黒い箇条書きを使用します:
use aspose\slides\BulletType;
use aspose\slides\ColorType;
use aspose\slides\NullableBool;
use aspose\slides\NumberedBulletStyle;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$symbolParagraph = new Paragraph();
$symbolParagraph->setText("Welcome to Aspose.Slides");
$symbolParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$symbolParagraph->getParagraphFormat()->getBullet()->setChar("•");
$symbolParagraph->getParagraphFormat()->setIndent(25);
$symbolParagraph->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
$symbolParagraph->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
$symbolParagraph->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);
$symbolParagraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($symbolParagraph);
$numberedParagraph = new Paragraph();
$numberedParagraph->setText("This is a numbered item");
$numberedParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$numberedParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStyle(NumberedBulletStyle::BulletCircleNumWDBlackPlain);
$numberedParagraph->getParagraphFormat()->setIndent(25);
$numberedParagraph->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
$numberedParagraph->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
$numberedParagraph->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);
$numberedParagraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($numberedParagraph);
$presentation->save("bulleted_and_numbered_list.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
画像箇条書きの使用
画像箇条書きは、記号や番号の代わりにカスタム画像を使用できます。
この PHP の例は、PPImage としてプレゼンテーションの画像コレクションに画像を追加し、段落の箇条書きとして使用します。BulletFormat::setType を呼び出して画像箇条書きを使用し、画像は BulletFormat::getPicture で割り当てます。BulletFormat::setHeight を 100% に設定して、段落テキストに対する箇条書きのサイズを決定します:
use aspose\slides\BulletType;
use aspose\slides\Images;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$bulletImage = Images::fromFile("bullets.png");
try {
$presentationImage = $presentation->getImages()->addImage($bulletImage);
} finally {
$bulletImage->dispose();
}
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$paragraph = new Paragraph();
$paragraph->setText("Welcome to Aspose.Slides");
$paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Picture);
$paragraph->getParagraphFormat()->getBullet()->getPicture()->setImage($presentationImage);
$paragraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($paragraph);
$presentation->save("picture_bullet.pptx", SaveFormat::Pptx);
$presentation->save("picture_bullet.ppt", SaveFormat::Ppt);
} finally {
$presentation->dispose();
}
階層リストの作成
ParagraphFormat::setDepth を設定して、段落をリストの異なるレベルに配置します。上位レベルの深さは 0 です。
この PHP の例は、4 つの段落に対して ParagraphFormat::setDepth の値を 0 から 3 に割り当て、1 つのテキストフレーム内に階層を作成します。各段落は記号箇条書きを使用し、BulletFormat::setChar で丸い箇条書きとハイフンを交互に設定して隣接レベルを区別します:
use aspose\slides\BulletType;
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("Content");
$firstParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$firstParagraph->getParagraphFormat()->getBullet()->setChar("•");
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setDepth(0);
$secondParagraph = new Paragraph();
$secondParagraph->setText("Second level");
$secondParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$secondParagraph->getParagraphFormat()->getBullet()->setChar('-');
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setDepth(1);
$thirdParagraph = new Paragraph();
$thirdParagraph->setText("Third level");
$thirdParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$thirdParagraph->getParagraphFormat()->getBullet()->setChar("•");
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$thirdParagraph->getParagraphFormat()->setDepth(2);
$fourthParagraph = new Paragraph();
$fourthParagraph->setText("Fourth level");
$fourthParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$fourthParagraph->getParagraphFormat()->getBullet()->setChar('-');
$fourthParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$fourthParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$fourthParagraph->getParagraphFormat()->setDepth(3);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$textFrame->getParagraphs()->add($thirdParagraph);
$textFrame->getParagraphs()->add($fourthParagraph);
$presentation->save("multilevel_list.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
カスタム値で番号付きリスト項目を開始する
BulletFormat::setNumberedBulletStartWith を使用して、番号付き段落の初期番号を設定します。
この PHP の例は、3 つの段落で番号付けを使用するために BulletFormat::setType を設定し、BulletFormat::setNumberedBulletStartWith の値をそれぞれ 2、3、7 に割り当てます。開始値は段落ごとに個別に設定でき、表示される番号を飛ばすことができます:
use aspose\slides\BulletType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("Start at 2");
$firstParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$firstParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(2);
$textFrame->getParagraphs()->add($firstParagraph);
$secondParagraph = new Paragraph();
$secondParagraph->setText("Start at 3");
$secondParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$secondParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(3);
$textFrame->getParagraphs()->add($secondParagraph);
$thirdParagraph = new Paragraph();
$thirdParagraph->setText("Start at 7");
$thirdParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$thirdParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(7);
$textFrame->getParagraphs()->add($thirdParagraph);
$presentation->save("custom_numbered_list.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
段落レイアウトと終了プロパティの制御
段落の余白とインデントを調整して、最初の行と折り返し行の位置を制御します。また、段落の終了マークを可視テキストのポーションとは別に書式設定できます。
最初の行インデントの設定
ParagraphFormat::setIndent メソッドを使用して、段落の最初の行インデントを制御します。この設定は段落の左余白に対して最初の行のみを移動させます。正の値は最初の行を右にシフトし、残りの行は段落本体に揃ったままです。
段落全体を移動させる必要がある場合は ParagraphFormat::setMarginLeft を使用します。最初の行だけを移動させる場合は ParagraphFormat::setIndent を使用します。
以下の例は、3 つの段落に同じ 20 ポイントの ParagraphFormat::setMarginLeft と、0、20、40 ポイントの ParagraphFormat::setIndent の値を設定します。最初の行は徐々に大きなオフセットで開始し、折り返し行は同じ左余白を保持します:
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$shape->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("No first-line indent. Wrapped lines start at the same position as the first line.");
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setMarginLeft(20.0);
$firstParagraph->getParagraphFormat()->setIndent(0.0);
$secondParagraph = new 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(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setMarginLeft(20.0);
$secondParagraph->getParagraphFormat()->setIndent(20.0);
$thirdParagraph = new 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(FillType::Solid);
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$thirdParagraph->getParagraphFormat()->setMarginLeft(20.0);
$thirdParagraph->getParagraphFormat()->setIndent(40.0);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$textFrame->getParagraphs()->add($thirdParagraph);
$presentation->save("paragraph_indent.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

ハンギングインデントの設定
ハンギングインデントは、最初の行が残りの行より左側で開始する段落レイアウトです。Aspose.Slides では、ParagraphFormat::setIndent プロパティでこの効果を作成します。Indent に負の値を設定すると、段落本体に対して最初の行が左に移動します。
実際には、ParagraphFormat::setMarginLeft が段落本体の左位置を定義し、ParagraphFormat::setIndent がその余白に対する最初の行の位置を定義します。ハンギングインデントを作成するには、正の MarginLeft 値と負の Indent 値を設定します。
この書式設定は、文献リスト、参考文献、用語集の項目、その他折り返し行が段落本体の下に揃う必要がある段落に便利です。
以下の PHP の例は、40 ポイントと 60 ポイントの ParagraphFormat::setMarginLeft の値を、-20 と -30 ポイントの ParagraphFormat::setIndent の値と組み合わせます。各段落で、最初の行は折り返し行より左側で開始し、2 番目の段落はより深いハンギングインデントになります:
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$shape->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
$textFrame->getParagraphs()->clear();
$firstParagraph = new 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(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setMarginLeft(40.0);
$firstParagraph->getParagraphFormat()->setIndent(-20.0);
$secondParagraph = new 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(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setMarginLeft(60.0);
$secondParagraph->getParagraphFormat()->setIndent(-30.0);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$presentation->save("hanging_indent.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

段落終了ランプロパティの設定
Paragraph::setEndParagraphPortionFormat メソッドは段落の終了マークの書式設定を制御します。以下の例は、PortionFormat を作成し、BasePortionFormat::setFontHeight を 48 ポイント、BasePortionFormat::setLatinFont を Times New Roman に設定します。この書式を第2段落の終了マークに割り当て、可視テキストのポーションは変更しません:
use aspose\slides\FontData;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\PortionFormat;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation("Test.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, 200, 250);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->getPortions()->add(new Portion("Sample text"));
$secondParagraph = new Paragraph();
$secondParagraph->getPortions()->add(new Portion("Sample text 2"));
$endParagraphFormat = new PortionFormat();
$endParagraphFormat->setFontHeight(48);
$endParagraphFormat->setLatinFont(new FontData("Times New Roman"));
$secondParagraph->setEndParagraphPortionFormat($endParagraphFormat);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$presentation->save("end_paragraph_format.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
レンダリングされた行数のカウント
自動折り返しや行末の句読点に影響を与える段落ルールについては、行の折り返し制御 と ハンギング句読点制御 を参照してください。
Paragraph::getLinesCount を使用して、テキストレイアウト後の段落が占める行数(自動折り返しを含む)をカウントします。これは、プレゼンテーション テンプレートでテキストの長さとレイアウトをチェックする際に便利です。
段落は TextFrame::getParagraphs の項目の 1 つで、複数のレンダリング行を占めることがあります。段落内の明示的な改行は別の段落を作成せずに新しい行を強制します。自動折り返しはテキストに明示的な改行を挿入せず、利用可能な幅に基づいて行を生成します。したがって、段落数や改行文字を数えてもレンダリングされた行数は得られません。
以下の例は、テキストシェイプを作成し、行数をカウントし、シェイプを狭め、テキストを短い文字列に置き換えます。この例は、TextFrameFormat::setWrapText で折り返しを有効にし、TextFrameFormat::setAutofitType で自動フィットを無効にして、シェイプの幅が折り返しを制御し、テキストやシェイプのサイズが自動的に縮小されないようにします。各測定は Paragraph::getLinesCount を使用します。シェイプの寸法はポイント単位です。最後に、例は別の段落を追加し、テキストフレーム全体の行数を合計します。
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setWrapText(NullableBool::True);
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
$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.");
echo "Original width: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$shape->setWidth(150);
echo "Narrower shape: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$paragraph->setText("Short text.");
echo "Shorter text: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$secondParagraph = new Paragraph();
$secondParagraph->setText("Another paragraph.");
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->setFontHeight(20);
$textFrame->getParagraphs()->add($secondParagraph);
$totalLineCount = 0;
for ($i = 0; $i < java_values($textFrame->getParagraphs()->getCount()); $i++) {
$currentParagraph = $textFrame->getParagraphs()->get_Item($i);
$totalLineCount += java_values($currentParagraph->getLinesCount());
}
echo "Total lines in the text frame: " . $totalLineCount . PHP_EOL;
} finally {
$presentation->dispose();
}
このテキストとこれらの寸法では、シェイプを狭めると行数が増え、短い文字列に置き換えると減ります。正確なカウントはフォントの利用可能性や代替、フォントサイズ、余白、インデント、折り返し、自動フィット設定により変わる可能性があります。テンプレートをチェックする際は、対象環境向けのフォントとレイアウト設定を使用してください。
行数だけではテキストがコンテナを超えるかどうかは判断できません。利用可能な高さ、行高、段落と行の間隔、自動フィットの動作も重要です。折り返しが無効な場合、1 行だけでも利用可能な幅を超えることがあります。
段落コンテンツのインポートとエクスポート
HTML を使用してテキストをプレゼンテーションに取り込んだり、既存の段落を外部で使用するためにエクスポートしたりできます。このセクションでは、テーブルセル内の段落を含む、個々の段落を画像としてレンダリングする方法も示します。
HTML テキストを段落にインポートする
ParagraphCollection::addFromHtml を使用して、HTML マークアップをテキストフレーム内の段落とポーションに変換します。
この PHP の例は、ファイルから HTML を読み取り、マークアップを ParagraphCollection::addFromHtml に渡します。テキストフレームのデフォルト段落をクリアして、フレームにインポートされたコンテンツのみが含まれるようにします。長方形シェイプはスライドの各側に 10 ポイントの余白を残します:
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shapeWidth = java_values($presentation->getSlideSize()->getSize()->getWidth()) - 20;
$shapeHeight = java_values($presentation->getSlideSize()->getSize()->getHeight()) - 20;
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, $shapeWidth, $shapeHeight);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getTextFrame()->getParagraphs()->clear();
$html = file_get_contents("file.html");
if ($html !== false) {
$shape->getTextFrame()->getParagraphs()->addFromHtml($html);
$presentation->save("html_text.pptx", SaveFormat::Pptx);
} else {
echo "The HTML file could not be read.";
}
} finally {
$presentation->dispose();
}
段落テキストを HTML にエクスポートする
ParagraphCollection::exportToHtml を使用して、選択した範囲の段落を HTML としてエクスポートします。プレゼンテーションのテキストをウェブサイトで再利用する場合、フォント、サイズ、テキストボックスのスタイルを持ち込まずにローカルの強調表示を保持したいことがあります。TextToHtmlConversionOptions::setTextInheritanceLimit を設定して、エクスポート時の書式継承の範囲を制御します。
サンプルプレゼンテーション をダウンロードし、アプリケーションの作業ディレクトリに配置します。最初のスライドには、テキスト “Inherited text; explicit red 18pt” を含むテキストシェイプが 1 つあります。入力は意図的に複数の書式レベルを使用しています:
- プレゼンテーションのデフォルトテキストスタイルは Calibri と 28 pt のサイズを提供します。
- テキストフレームの第一レベルのテキストスタイルは青いテキストとイタリックを提供します。
- 段落は太字テキストを明示的に設定します。
- 2 番目のポーション “explicit red 18pt” は、赤いテキストと 18 pt のサイズを明示的に設定します。
以下の完全な例は、同じ段落を 3 回エクスポートします。TextToHtmlConversionOptions をエクスポートメソッドに渡し、各結果を個別の UTF-8 HTML ファイルとして書き込みます:
use aspose\slides\Presentation;
use aspose\slides\TextInheritanceLimit;
use aspose\slides\TextToHtmlConversionOptions;
$presentation = new Presentation("text-inheritance.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$textFrame = $shape->getTextFrame();
if (!java_is_null($textFrame)) {
$paragraphs = $textFrame->getParagraphs();
$modes = array(TextInheritanceLimit::All, TextInheritanceLimit::TextBox, TextInheritanceLimit::ParagraphOnly);
$fileNames = array("All.html", "TextBox.html", "ParagraphOnly.html");
for ($i = 0; $i < count($modes); $i++) {
$options = new TextToHtmlConversionOptions();
$options->setTextInheritanceLimit($modes[$i]);
$html = $paragraphs->exportToHtml(0, $paragraphs->getCount(), $options);
if (file_put_contents($fileNames[$i], java_values($html)) === false) {
echo "The HTML file could not be written." . PHP_EOL;
}
}
} else {
echo "The first shape does not contain a text frame.";
}
} else {
echo "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 の 28 pt で青色、太字、イタリック体テキスト |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
青色、太字、イタリック体テキスト;フォントファミリーとサイズは周囲のページまたはブラウザーが提供します |
ParagraphOnly.html |
font-weight:bold |
太字テキスト;フォントファミリー、サイズ、色、イタリックスタイルは周囲のページまたはブラウザーが提供します |
2 番目のポーションは、3 つの出力すべてで 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 の UTF-8 バイトオフセットを示し、StartFragment と EndFragment は貼り付けるコンテンツを特定します。
この例は、2 つの書式設定された段落を普通の HTML ($html) と HTML クリップボードフラグメント ($clipboardHtml) としてエクスポートします。アプリケーションはそのフラグメントをシステムクリップボードに書き込むことができます。クリップボード API が生バイトを期待する場合、BOM(バイト順マーク)なしの UTF-8 でフラグメントを提供してください。
use aspose\slides\Presentation;
use aspose\slides\ShapeType;
use aspose\slides\TextToHtmlConversionOptions;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 0, 0, 400, 100);
$paragraphs = $shape->getTextFrame()->getParagraphs();
$paragraphs->clear();
$paragraphs->addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
$options = new TextToHtmlConversionOptions();
$options->setAddClipboardFragmentHeader(false);
$html = java_values($paragraphs->exportToHtml(0, $paragraphs->getCount(), $options));
$options->setAddClipboardFragmentHeader(true);
$clipboardHtml = java_values($paragraphs->exportToHtml(0, $paragraphs->getCount(), $options));
} finally {
$presentation->dispose();
}
段落を画像としてレンダリングする
Paragraph::getImage は個々の段落を直接レンダリングし、IImage を返します。IImage::save を使用して結果をファイルまたはストリームに保存します。包含するシェイプをレンダリングしたり、ビットマップを手動でトリミングする必要はありません。
Paragraph::getImage は、段落が親コレクションに見つからない、妥当なレンダリング境界がない、またはレンダリングできない場合に null を返すことがあります。保存する前に結果を確認し、使用後は返された画像を破棄してください。
デフォルト スケールで段落をレンダリングする
この例は、1 枚のスライドを持つプレゼンテーションを使用し、最初のシェイプが 3 段落を含むテキスト ボックスです。

以下の例は、インデックス 1 の 2 番目の段落を選択し、スケール パラメータなしで Paragraph::getImage を呼び出します。シェイプが必要な段落を含んでいることと、レンダリングが画像を返すことを確認した上で、IImage::save を使用して PNG 形式で保存します。finally ブロックで使用後に画像を破棄します。
use aspose\slides\ImageFormat;
use aspose\slides\Presentation;
$presentation = new Presentation("sample.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$textFrame = $shape->getTextFrame();
if (!java_is_null($textFrame) && java_values($textFrame->getParagraphs()->getCount()) > 1) {
$paragraph = $textFrame->getParagraphs()->get_Item(1);
$paragraphImage = $paragraph->getImage();
if (!java_is_null($paragraphImage)) {
try {
$paragraphImage->save("paragraph.png", ImageFormat::Png);
} finally {
$paragraphImage->dispose();
}
} else {
echo "The paragraph could not be rendered.";
}
} else {
echo "The expected paragraph was not found.";
}
} else {
echo "The first shape is not a text shape.";
}
} finally {
$presentation->dispose();
}
結果:

テーブルセル内の段落をスケーリングしてレンダリングする
$scaleX と $scaleY パラメータを受け取る Paragraph::getImage のオーバーロードを使用して、水平および垂直のスケール係数を設定します。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅と高さの 2 倍でレンダリングし、結果を PNG 画像として保存します。
use aspose\slides\ImageFormat;
use aspose\slides\Presentation;
$scaleX = 2;
$scaleY = 2;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$table = $slide->getShapes()->addTable(50, 50, array(300), array(80));
$paragraph = $table->get_Item(0, 0)->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->setText("Text in a table cell");
$paragraphImage = $paragraph->getImage($scaleX, $scaleY);
if (!java_is_null($paragraphImage)) {
try {
$paragraphImage->save("table_paragraph.png", ImageFormat::Png);
} finally {
$paragraphImage->dispose();
}
} else {
echo "The paragraph could not be rendered.";
}
} finally {
$presentation->dispose();
}
1 のスケール係数はその軸をデフォルトのピクセルサイズに保ちます。たとえば、両方の係数を 2 にすると、幅と高さがデフォルトの約 2 倍になり、ピクセル数は 4 倍になります。大きな係数はズームや高解像度出力に対してテキストをより鮮明にしますが、メモリ使用量とファイルサイズも増加します。1 未満の係数は詳細が少ない小さい画像を生成します。アスペクト比を保持するには係数を等しく設定し、異なる水平・垂直係数は出力を個別に伸張します。
Shape::getImage を使用してシェイプ全体をレンダリングすることは、出力にシェイプの塗りや枠線、その他のビジュアルコンテキストを含める必要がある場合に有用です。段落のみの画像の場合は、Paragraph::getImage を使用してください。
よくある質問
テキストフレーム内の行折り返しを完全に無効にできますか?
はい。TextFrameFormat::setWrapText を設定して折り返しを無効にすると、行はテキストフレームの端で改行しなくなります。
特定の段落のスライド上での正確な境界を取得するにはどうすればよいですか?
Paragraph::getRect を使用して段落のバウンディング矩形を取得します。Portion::getRect は個々のポーションの境界を提供します。
段落の配置(左揃え、右揃え、中央揃え、両端揃え)はどこで制御されますか?
ParagraphFormat::setAlignment は段落レベルの設定であり、個々のポーションの書式設定に関係なく、段落全体に適用されます。
各行で異なるフォントサイズのポーションを垂直に揃えるには、行内のフォントを垂直揃え を参照してください。
段落の一部に校正言語を設定できますか?
はい。BasePortionFormat::setLanguageId を個々のポーションに設定すれば、1 つの段落に複数の言語のテキストを含めることができます。