JavaでPowerPointテキスト段落を管理する

概要

Aspose.Slides for Java はテキストをテキストフレーム、段落、ポーションの階層として表現します。

  • ITextFrame はシェイプ内のテキストコンテナを表し、段落コレクションへのアクセスを提供します。
  • IParagraph はテキストフレーム内の 1 つの段落を表し、ポーションと段落レベルの書式設定へのアクセスを提供します。
  • IPortion は段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。

そのため、段落は複数のポーションを使用して、フォント、色、サイズ、その他の書式が異なるテキストを含めることができます。

段落の作成と書式設定

複数のポーションを持つ段落の作成

以下の手順は、3 つの段落を持ち、各段落に 3 つのポーションを含むテキストフレームを作成します。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象のスライドにアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスします。
  5. デフォルトの段落を使用し、テキストフレームにさらに 2 つの IParagraph オブジェクトを追加します。
  6. 各段落に 3 つのポーションを含めるのに十分な数の IPortion オブジェクトを追加します。デフォルトの段落にはすでに空のポーションが 1 つ含まれています。
  7. 各ポーションのテキストを設定します。
  8. IPortion.getPortionFormat を使用して文字レベルの書式設定を適用します。
  9. 変更されたプレゼンテーションを保存します。

この Java の例は手順を実装しています:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
    ITextFrame textFrame = shape.getTextFrame();

    IParagraph firstParagraph = textFrame.getParagraphs().get_Item(0);
    firstParagraph.getPortions().add(new Portion());
    firstParagraph.getPortions().add(new Portion());

    IParagraph secondParagraph = new Paragraph();
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(secondParagraph);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(thirdParagraph);

    int paragraphCount = textFrame.getParagraphs().getCount();
    for (int paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
        IParagraph paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
        int portionCount = paragraph.getPortions().getCount();
        for (int portionIndex = 0; portionIndex < portionCount; portionIndex++) {
            IPortion 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(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(Color.BLUE);
                portion.getPortionFormat().setFontItalic(NullableBool.True);
                portion.getPortionFormat().setFontHeight(18);
            }
        }
    }

    presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

箇条書きと番号付きリストの作成

箇条書きまたは番号付きリストの作成

箇条書きと番号付けにより、関連項目を簡単に参照できます。Aspose.Slides では、リスト設定は IBulletFormat を介して定義されます。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象のスライドにアクセスします。
  3. 選択したスライドに IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスします。
  5. テキストフレームからデフォルトの段落を削除します。
  6. シンボル箇条書き用に Paragraph を作成します。
  7. IBulletFormat.setType を BulletType.Symbol に設定し、箇条書き文字を指定します。
  8. 段落テキスト、インデント、箇条書きの色、箇条書きの高さを設定します。
  9. 段落をテキストフレームに追加します。
  10. 2 番目の段落を作成し、IBulletFormat.setType を BulletType.Numbered に設定します。
  11. 番号付き箇条書きのスタイルを構成し、段落をテキストフレームに追加します。
  12. プレゼンテーションを保存します。

この Java の例はシンボル箇条書きと番号付き箇条書きを作成します:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph symbolParagraph = new Paragraph();
    symbolParagraph.setText("Welcome to Aspose.Slides");
    symbolParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    symbolParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    symbolParagraph.getParagraphFormat().setIndent(25);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
    symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
    symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(symbolParagraph);

    Paragraph 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(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();
}

画像箇条書きの使用

画像箇条書きでは、シンボルや番号の代わりにカスタム画像を使用できます。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象のスライドにアクセスします。
  3. IAutoShape を追加し、その ITextFrame にアクセスします。
  4. テキストフレームからデフォルトの段落を削除します。
  5. 箇条書き画像を読み込み、プレゼンテーションの画像コレクションに IPPImage として追加します。
  6. Paragraph を作成し、テキストを設定します。
  7. IBulletFormat.setType を BulletType.Picture に設定します。
  8. IBulletFormat.getPicture で画像を割り当て、箇条書きの高さを設定します。
  9. 段落をテキストフレームに追加します。
  10. 変更されたプレゼンテーションを保存します。

この Java の例は画像箇条書きを作成します:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IImage bulletImage = Images.fromFile("bullets.png");
    IPPImage presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(bulletImage);
    } finally {
        bulletImage.dispose();
    }

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph 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();
}

多層リストの作成

IParagraphFormat.setDepth を設定して、段落をリストの異なるレベルに配置します。最上位レベルの深さは 0 です。

  1. Presentation を作成し、スライドにアクセスします。
  2. IAutoShape を追加し、テキストフレームからデフォルトの段落をクリアします。
  3. 4 つの段落を作成し、箇条書き記号を構成します。
  4. それぞれの IParagraphFormat.setDepth 値を 0、1、2、3 に設定します。
  5. 段落をテキストフレームに追加し、プレゼンテーションを保存します。

この Java の例は 4 レベルの箇条書きリストを作成します:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    IParagraph firstParagraph = new Paragraph();
    firstParagraph.setText("Content");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    firstParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setDepth((short) 0);

    IParagraph 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(Color.BLACK);
    secondParagraph.getParagraphFormat().setDepth((short) 1);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Third level");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    thirdParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    thirdParagraph.getParagraphFormat().setDepth((short) 2);

    IParagraph 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(Color.BLACK);
    fourthParagraph.getParagraphFormat().setDepth((short) 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();
}

番号付きリスト項目をカスタム開始値で開始

IBulletFormat.setNumberedBulletStartWith を使用して、番号付き段落の最初に表示される番号を設定します。

  1. Presentation を作成し、スライドに IAutoShape を追加します。
  2. シェイプのテキストフレームからデフォルトの段落をクリアします。
  3. 3 つの番号付き段落を作成します。
  4. 各段落に対して、IBulletFormat.setNumberedBulletStartWith をそれぞれ 2、3、7 に設定します。
  5. 段落をテキストフレームに追加し、プレゼンテーションを保存します。

この Java の例は各段落にカスタム開始番号を割り当てます:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.setText("Start at 2");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 2);
    textFrame.getParagraphs().add(firstParagraph);

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("Start at 3");
    secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 3);
    textFrame.getParagraphs().add(secondParagraph);

    Paragraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Start at 7");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 7);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

段落のレイアウトと終端プロパティの制御

先頭行インデントの設定

IParagraphFormat.setIndent を使用して段落の先頭行インデントを制御します。このメソッドは段落の左余白に対して最初の行だけを移動させます。正の値は先頭行を右にシフトし、残りの行は段落本文に揃ったままです。

段落全体を移動する必要がある場合は IParagraphFormat.setMarginLeft を使用し、先頭行だけを移動する場合は IParagraphFormat.setIndent を使用します。

以下の例は複数の段落を作成し、異なる IParagraphFormat.setIndent 値を適用して、先頭行インデントが段落レイアウトに与える影響を示します。

  1. Presentation クラスのインスタンスを作成します。
  2. 対象のスライドにアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスし、デフォルトの段落を削除します。
  5. 複数の段落を作成し、各段落に異なる IParagraphFormat.setIndent 値を設定します。
  6. 段落をテキストフレームに追加します。
  7. 変更されたプレゼンテーションを保存します。

このコードは段落インデントの設定方法を示します:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape 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(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

    Paragraph 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(Color.BLACK);
    firstParagraph.getParagraphFormat().setMarginLeft(20f);
    firstParagraph.getParagraphFormat().setIndent(0f);

    Paragraph 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(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(20f);
    secondParagraph.getParagraphFormat().setIndent(20f);

    Paragraph 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(Color.BLACK);
    thirdParagraph.getParagraphFormat().setMarginLeft(20f);
    thirdParagraph.getParagraphFormat().setIndent(40f);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("paragraph_indent.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

段落の先頭行インデント

ぶら下げインデントの設定

ぶら下げインデントは、最初の行が残りの行より左に開始する段落レイアウトです。Aspose.Slides では IParagraphFormat.setIndent に負の値を指定して、段落本文に対して最初の行を左に移動させます。

実際には、IParagraphFormat.setMarginLeft が段落本文の左位置を定義し、IParagraphFormat.setIndent がその余白に対する最初の行の位置を定義します。ぶら下げインデントを作成するには、setMarginLeft に正の値を、setIndent に負の値を渡します。

この書式は、参考文献、文献リスト、用語集エントリ、その他の段落で、折り返し行が段落本文の下に揃う必要がある場合に便利です。

  1. Presentation クラスのインスタンスを作成します。
  2. 対象のスライドにアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスし、デフォルトの段落を削除します。
  5. 各段落に対して、IParagraphFormat.setMarginLeft に正の値を設定します。
  6. IParagraphFormat.setIndent に負の値を渡して、ぶら下げインデント効果を作成します。
  7. 段落をテキストフレームに追加します。
  8. 変更されたプレゼンテーションを保存します。

このコードは段落のぶら下げインデントの設定方法を示します:

import com.aspose.slides.*;
import java.awt.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape 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(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

    Paragraph 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(Color.BLACK);
    firstParagraph.getParagraphFormat().setMarginLeft(40f);
    firstParagraph.getParagraphFormat().setIndent(-20f);

    Paragraph 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(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(60f);
    secondParagraph.getParagraphFormat().setIndent(-30f);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("hanging_indent.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

段落のぶら下げインデント

段落終端の実行プロパティの設定

IParagraph.setEndParagraphPortionFormat は段落終端マークの書式設定を制御します。以下の例は、2 番目の段落の終端マークにフォントサイズとラテン文字フォントを割り当てます。

  1. Presentation をロードし、スライドにアクセスします。
  2. IAutoShape を追加し、デフォルトの段落をクリアします。
  3. 2 つの段落を作成し、テキストポーションを追加します。
  4. 2 番目の段落の終端マーク用に PortionFormat を作成します。
  5. IBasePortionFormat.setFontHeight と IBasePortionFormat.setLatinFont を設定します。
  6. IParagraph.setEndParagraphPortionFormat で書式を割り当て、プレゼンテーションを保存します。
import com.aspose.slides.*;

Presentation presentation = new Presentation("Test.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.getPortions().add(new Portion("Sample text"));

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.getPortions().add(new Portion("Sample text 2"));

    PortionFormat 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();
}

描画された行数のカウント

段落の自動折り返しや行末句読点に影響する規則については、Control Line Breaking と Control Hanging Punctuation を参照してください。

IParagraph.getLinesCount を使用して、テキストレイアウト後に段落が占める行数をカウントできます。自動折り返しを含むため、テンプレートでテキスト長とレイアウトをチェックする際に便利です。

段落は ITextFrame.getParagraphs の 1 アイテムであり、複数の描画行を占めることがあります。段落内の明示的な改行は新しい行を強制しますが、別の段落は作成しません。自動折り返しは利用可能な幅に基づいて行を生成し、テキストに明示的な改行文字を挿入しません。そのため、段落数や改行文字のカウントだけでは描画行数は得られません。

以下の例はテキストシェイプを作成し、行数をカウントし、シェイプを狭めてからテキストを短い文字列に置き換えます。折り返しは有効で、オートフィットは無効にしているため、シェイプの幅で折り返しが制御され、テキストやシェイプの自動縮小は行われません。シェイプの寸法はポイント単位です。最後に、別の段落を追加し、テキストフレーム全体の行数を合計します。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);

    IParagraph 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.");
    System.out.println("Original width: " + paragraph.getLinesCount());

    shape.setWidth(150);
    System.out.println("Narrower shape: " + paragraph.getLinesCount());

    paragraph.setText("Short text.");
    System.out.println("Shorter text: " + paragraph.getLinesCount());

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("Another paragraph.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
    textFrame.getParagraphs().add(secondParagraph);

    int totalLineCount = 0;
    for (IParagraph currentParagraph : textFrame.getParagraphs()) {
        totalLineCount += currentParagraph.getLinesCount();
    }
    System.out.println("Total lines in the text frame: " + totalLineCount);
} finally {
    presentation.dispose();
}

このテキストと寸法では、シェイプを狭めると行数が増加し、短い文字列に置き換えると行数が減少します。フォントの可用性や置換、フォントサイズ、余白、インデント、折り返し、オートフィット設定により正確なカウントは変動します。テンプレートをチェックする際は、対象環境で使用するフォントとレイアウト設定を使用してください。

行数だけでテキストがコンテナからはみ出すかは判断できません。利用可能な高さ、行高、段落間および行間の設定、オートフィット動作も考慮する必要があります。折り返しが無効の場合、1 行でも利用可能な幅を超えることがあります。

段落コンテンツのインポートとエクスポート

HTML テキストを段落にインポート

ParagraphCollection.addFromHtml を使用して、HTML マークアップをテキストフレーム内の段落とポーションに変換します。

  1. Presentation クラスのインスタンスを作成します。
  2. スライドにアクセスし、IAutoShape を追加します。
  3. シェイプの ITextFrame にアクセスし、デフォルトの段落をクリアします。
  4. ソース HTML ファイルを読み取ります。
  5. HTML 文字列を ParagraphCollection.addFromHtml に渡します。
  6. 変更されたプレゼンテーションを保存します。

この Java の例は HTML をテキストフレームにインポートします:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    float shapeWidth = (float) presentation.getSlideSize().getSize().getWidth() - 20;
    float shapeHeight = (float) presentation.getSlideSize().getSize().getHeight() - 20;
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getTextFrame().getParagraphs().clear();

    try {
        byte[] htmlBytes = Files.readAllBytes(Paths.get("file.html"));
        String html = new String(htmlBytes, StandardCharsets.UTF_8);
        shape.getTextFrame().getParagraphs().addFromHtml(html);
        presentation.save("html_text.pptx", SaveFormat.Pptx);
    } catch (IOException exception) {
        System.out.println("The HTML file could not be read: " + exception.getMessage());
    }
} finally {
    presentation.dispose();
}

段落テキストを HTML にエクスポート

ParagraphCollection.exportToHtml を使用して、選択した範囲の段落を HTML としてエクスポートします。

  1. Presentation のインスタンスを作成し、目的のプレゼンテーションをロードします。
  2. スライドにアクセスし、テキストを含む IAutoShape を見つけます。
  3. シェイプの ITextFrame にアクセスします。
  4. 開始段落インデックスとエクスポートする段落数を指定して、ParagraphCollection.exportToHtml を呼び出します。
  5. 返された HTML 文字列をファイルに書き込みます。

この Java の例は最初のテキストシェイプのすべての段落をエクスポートします:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation("ExportingHTMLText.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null) {
            IParagraphCollection paragraphs = textFrame.getParagraphs();
            String html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
            try {
                Files.write(Paths.get("paragraphs.html"), html.getBytes(StandardCharsets.UTF_8));
            } catch (IOException exception) {
                System.out.println("The HTML file could not be written: " + exception.getMessage());
            }
        } else {
            System.out.println("The first shape does not contain a text frame.");
        }
    } else {
        System.out.println("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

段落を画像としてレンダリング

IParagraph.getImage は個々の段落を直接レンダリングし、IImage を返します。結果は IImage.save を使用してファイルまたはストリームに保存できます。シェイプ全体をレンダリングしたり、ビットマップを手動で切り取る必要はありません。

IParagraph.getImage は、段落が親コレクションに見つからない、または有効なレンダリング境界がない、あるいはレンダリングできない場合に null を返すことがあります。保存する前に結果を確認し、使用後は画像を破棄してください。

デフォルトスケールで段落をレンダリング

サンプルとして、sample.pptx というプレゼンテーション ファイルが 1 枚のスライドを持ち、最初のシェイプが 3 つの段落を含むテキスト ボックスであるとします。

3 つの段落を含むテキストボックス

以下の例は、通常のテキストシェイプ内の 2 番目の段落をデフォルトスケールでレンダリングし、PNG 形式で保存します。finally ブロックで画像の破棄を確実に行います。

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null && textFrame.getParagraphs().getCount() > 1) {
            IParagraph paragraph = textFrame.getParagraphs().get_Item(1);
            IImage paragraphImage = paragraph.getImage();

            if (paragraphImage != null) {
                try {
                    paragraphImage.save("paragraph.png", ImageFormat.Png);
                } finally {
                    paragraphImage.dispose();
                }
            } else {
                System.out.println("The paragraph could not be rendered.");
            }
        } else {
            System.out.println("The expected paragraph was not found.");
        }
    } else {
        System.out.println("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

結果:

段落の画像

テーブルセル内の段落をスケーリングしてレンダリング

IParagraph.getImage のオーバーロードを使用して、float scaleX と float scaleY パラメーターで水平・垂直のスケール係数を指定します。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅と高さの 2 倍でレンダリングし、PNG 画像として保存します。

import com.aspose.slides.*;

float scaleX = 2f;
float scaleY = 2f;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = slide.getShapes().addTable(50, 50, new double[] { 300 }, new double[] { 80 });
    IParagraph paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
    paragraph.setText("Text in a table cell");

    IImage paragraphImage = paragraph.getImage(scaleX, scaleY);
    if (paragraphImage != null) {
        try {
            paragraphImage.save("table_paragraph.png", ImageFormat.Png);
        } finally {
            paragraphImage.dispose();
        }
    } else {
        System.out.println("The paragraph could not be rendered.");
    }
} finally {
    presentation.dispose();
}

スケール係数 1 はその軸をデフォルトのピクセルサイズに保ちます。たとえば、両方の係数を 2 にすると、幅と高さが約 2 倍になり、ピクセル数は 4 倍になります。大きな係数はズームや高解像度出力でテキストをより鮮明にしますが、メモリ使用量とファイルサイズも増加します。1 未満の係数は詳細が少ない小さな画像を生成します。アスペクト比を保つには同じ係数を使用し、水平と垂直で異なる係数を設定すると出力が個別に伸縮します。

シェイプ全体を画像化するには IShape.getImage が有用です。段落だけの画像が必要な場合は IParagraph.getImage を使用してください。

FAQ

テキストフレーム内の行折り返しを完全に無効にできますか?

はい。ITextFrameFormat.setWrapText を設定して折り返しを無効にすると、行はテキストフレームの端で折り返されません。

特定の段落のスライド上での正確な境界を取得する方法は?

IParagraph.getRect を使用して段落の境界矩形を取得できます。IPortion.getRect は個々のポーションの境界を提供します。

段落の配置(左揃え、右揃え、中央揃え、両端揃え)はどこで設定しますか?

IParagraphFormat.setAlignment は段落レベルの設定であり、個々のポーションの書式設定に関係なく段落全体に適用されます。

行内の異なるフォントサイズのポーションを垂直に揃える方法については、Align Fonts Within a Line を参照してください。

段落の一部に校正言語を設定できますか?

はい。個々のポーションに対して IBasePortionFormat.setLanguageId を設定すれば、1 つの段落内で複数の言語のテキストを扱うことができます。