JavaでPowerPointテキスト段落を管理する
Aspose.Slides は、Java で PowerPoint のテキスト、段落、そして部分を操作するために必要なすべてのインターフェイスとクラスを提供します。
- Aspose.Slides は、段落を表すオブジェクトを追加できるようにするための ITextFrame インターフェイスを提供します。
ITextFameオブジェクトは、1 つまたは複数の段落を持つことができます(各段落は改行で作成されます)。 - Aspose.Slides は、部分を表すオブジェクトを追加できるようにする IParagraph インターフェイスを提供します。
IParagraphオブジェクトは、1 つまたは複数の部分(iPortions オブジェクトのコレクション)を持つことができます。 - Aspose.Slides は、テキストとその書式設定プロパティを表すオブジェクトを追加できるようにする IPortion インターフェイスを提供します。
IParagraph オブジェクトは、基盤となる IPortion オブジェクトを通じて、異なる書式設定プロパティを持つテキストを処理できます。
複数の段落に複数の部分を含める
以下の手順では、3 つの段落を含むテキストフレームを追加し、各段落に 3 つの部分を含める方法を示します。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して、対象のスライドへの参照にアクセスします。
- スライドに矩形の IAutoShape を追加します。
- IAutoShape に関連付けられた ITextFrame を取得します。
- IParagraph オブジェクトを 2 つ作成し、ITextFrame の
IParagraphsコレクションに追加します。 - 新しい各
IParagraphに対して 3 つの IPortion オブジェクト(デフォルトの Paragraph には 2 つの Portion オブジェクト)を作成し、各IPortionオブジェクトを各IParagraphの IPortion コレクションに追加します。 - 各部分にテキストを設定します。
IPortionオブジェクトが提供する書式設定プロパティを使用して、各部分に好みの書式設定機能を適用します。- 変更されたプレゼンテーションを保存します。
この Java コードは、部分を含む段落を追加する手順の実装例です:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成
Presentation pres = new Presentation();
try {
// 最初のスライドにアクセス
ISlide slide = pres.getSlides().get_Item(0);
// Rectangle タイプの AutoShape を追加
IAutoShape ashp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
// AutoShape の TextFrame にアクセス
ITextFrame tf = ashp.getTextFrame();
// 異なるテキスト書式の Paragraph と Portion を作成
IParagraph para0 = tf.getParagraphs().get_Item(0);
IPortion port01 = new Portion();
IPortion port02 = new Portion();
para0.getPortions().add(port01);
para0.getPortions().add(port02);
IParagraph para1 = new Paragraph();
tf.getParagraphs().add(para1);
IPortion port10 = new Portion();
IPortion port11 = new Portion();
IPortion port12 = new Portion();
para1.getPortions().add(port10);
para1.getPortions().add(port11);
para1.getPortions().add(port12);
IParagraph para2 = new Paragraph();
tf.getParagraphs().add(para2);
IPortion port20 = new Portion();
IPortion port21 = new Portion();
IPortion port22 = new Portion();
para2.getPortions().add(port20);
para2.getPortions().add(port21);
para2.getPortions().add(port22);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
IPortion portion = tf.getParagraphs().get_Item(i).getPortions().get_Item(j);
portion.setText("Portion0" + j);
if (j == 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 (j == 1) {
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
portion.getPortionFormat().setFontItalic(NullableBool.True);
portion.getPortionFormat().setFontHeight(18);
}
}
}
// PPTX をディスクに保存
pres.save("multiParaPort_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
段落の箇条書きの管理
箇条書きリストは、情報を迅速かつ効率的に整理・提示するのに役立ちます。箇条書きの段落は常に読みやすく、理解しやすくなります。
- Presentation クラスのインスタンスを作成します。
- インデックスで対象スライドへの参照にアクセスします。
- スライドに autoshape を追加します。
- 自動シェイプの TextFrame にアクセスします。
TextFrameのデフォルト段落を削除します。- Paragraph クラスを使用して最初の段落インスタンスを作成します。
- 段落の箇条書き
TypeをSymbolに設定し、箇条書き文字を設定します。 - 段落の
Textを設定します。 - 段落の箇条書きの
Indentを設定します。 - 箇条書きの色を設定します。
- 箇条書きの高さを設定します。
- 新しい段落を
TextFrameの段落コレクションに追加します。 - 2 番目の段落を追加し、手順 7 から 13 のプロセスを繰り返します。
- プレゼンテーションを保存します。
この Java コードは、段落の箇条書きを追加する方法を示します:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成
Presentation pres = new Presentation();
try {
// 最初のスライドにアクセス
ISlide slide = pres.getSlides().get_Item(0);
// Autoshape を追加し、アクセス
IAutoShape aShp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// Autoshape のテキストフレームにアクセス
ITextFrame txtFrm = aShp.getTextFrame();
// デフォルトの段落を削除
txtFrm.getParagraphs().removeAt(0);
// 段落を作成
Paragraph para = new Paragraph();
// 段落の箇条書きスタイルとシンボルを設定
para.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para.getParagraphFormat().getBullet().setChar((char)8226);
// 段落のテキストを設定
para.setText("Welcome to Aspose.Slides");
// 箇条書きのインデントを設定
para.getParagraphFormat().setIndent(25);
// 箇条書きの色を設定
para.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
para.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
para.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True); // IsBulletHardColor を true に設定して独自の箇条書き色を使用
// 箇条書きの高さを設定
para.getParagraphFormat().getBullet().setHeight(100);
// 段落をテキストフレームに追加
txtFrm.getParagraphs().add(para);
// 2 番目の段落を作成
Paragraph para2 = new Paragraph();
// 段落の箇条書きタイプとスタイルを設定
para2.getParagraphFormat().getBullet().setType(BulletType.Numbered);
para2.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain);
// 段落のテキストを追加
para2.setText("This is numbered bullet");
// 箇条書きのインデントを設定
para2.getParagraphFormat().setIndent(25);
para2.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
para2.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
para2.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True); // IsBulletHardColor を true に設定して独自の箇条書き色を使用
// 箇条書きの高さを設定
para2.getParagraphFormat().getBullet().setHeight(100);
// 段落をテキストフレームに追加
txtFrm.getParagraphs().add(para2);
// 変更されたプレゼンテーションを保存
pres.save("Bullet_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
画像箇条書きの管理
箇条書きリストは、情報を迅速かつ効率的に整理・提示するのに役立ちます。画像段落は読みやすく、理解しやすいです。
- Presentation クラスのインスタンスを作成します。
- インデックスで対象スライドへの参照にアクセスします。
- スライドに autoshape を追加します。
- 自動シェイプの TextFrame にアクセスします。
TextFrameのデフォルト段落を削除します。- Paragraph クラスを使用して最初の段落インスタンスを作成します。
- IPPImage に画像をロードします。
- 箇条書きタイプを Picture に設定し、画像を指定します。
- 段落の
Textを設定します。 - 段落の箇条書きの
Indentを設定します。 - 箇条書きの色を設定します。
- 箇条書きの高さを設定します。
- 新しい段落を
TextFrameの段落コレクションに追加します。 - 2 番目の段落を追加し、前の手順に従って繰り返します。
- 変更されたプレゼンテーションを保存します。
この Java コードは、画像箇条書きを追加および管理する方法を示します:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成
Presentation presentation = new Presentation();
try {
// 最初のスライドにアクセス
ISlide slide = presentation.getSlides().get_Item(0);
// 箇条書き用の画像をインスタンス化
IPPImage picture;
IImage image = Images.fromFile("bullets.png");
try {
picture = presentation.getImages().addImage(image);
} finally {
if (image != null) image.dispose();
}
// AutoShape を追加し、アクセス
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// AutoShape のテキストフレームにアクセス
ITextFrame textFrame = autoShape.getTextFrame();
// デフォルトの段落を削除
textFrame.getParagraphs().removeAt(0);
// 新しい段落を作成
Paragraph paragraph = new Paragraph();
paragraph.setText("Welcome to Aspose.Slides");
// 段落の箇条書きスタイルと画像を設定
paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture);
paragraph.getParagraphFormat().getBullet().getPicture().setImage(picture);
// 箇条書きの高さを設定
paragraph.getParagraphFormat().getBullet().setHeight(100);
// 段落をテキストフレームに追加
textFrame.getParagraphs().add(paragraph);
// プレゼンテーションを PPTX ファイルとして保存
presentation.save("ParagraphPictureBulletsPPTX_out.pptx", SaveFormat.Pptx);
// プレゼンテーションを PPT ファイルとして保存
presentation.save("ParagraphPictureBulletsPPT_out.ppt", SaveFormat.Ppt);
} catch (IOException e) {
} finally {
if (presentation != null) presentation.dispose();
}
多階層箇条書きの管理
箇条書きリストは、情報を迅速かつ効率的に整理・提示するのに役立ちます。多階層箇条書きは読みやすく、理解しやすいです。
- Presentation クラスのインスタンスを作成します。
- インデックスで対象スライドへの参照にアクセスします。
- 新しいスライドに autoshape を追加します。
- 自動シェイプの TextFrame にアクセスします。
TextFrameのデフォルト段落を削除します。- Paragraph クラスを使用して最初の段落インスタンスを作成し、深さを 0 に設定します。
Paragraphクラスを使用して第 2 の段落インスタンスを作成し、深さを 1 に設定します。Paragraphクラスを使用して第 3 の段落インスタンスを作成し、深さを 2 に設定します。Paragraphクラスを使用して第 4 の段落インスタンスを作成し、深さを 3 に設定します。- 新しい段落を
TextFrameの段落コレクションに追加します。 - 変更されたプレゼンテーションを保存します。
この Java コードは、多階層箇条書きを追加および管理する方法を示します:
// PPTX ファイルを表す Presentation クラスのインスタンスを作成
Presentation pres = new Presentation();
try {
// 最初のスライドにアクセス
ISlide slide = pres.getSlides().get_Item(0);
// AutoShape を追加し、アクセス
IAutoShape aShp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 作成した AutoShape のテキストフレームにアクセス
ITextFrame text = aShp.addTextFrame("");
// デフォルトの段落をクリア
text.getParagraphs().clear();
// 最初の段落を追加
IParagraph para1 = new Paragraph();
para1.setText("Content");
para1.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para1.getParagraphFormat().getBullet().setChar((char)8226);
para1.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
para1.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
// 箇条書きレベルを設定
para1.getParagraphFormat().setDepth((short)0);
// 2 番目の段落を追加
IParagraph para2 = new Paragraph();
para2.setText("Second Level");
para2.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para2.getParagraphFormat().getBullet().setChar('-');
para2.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
para2.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
// 箇条書きレベルを設定
para2.getParagraphFormat().setDepth((short)1);
// 3 番目の段落を追加
IParagraph para3 = new Paragraph();
para3.setText("Third Level");
para3.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para3.getParagraphFormat().getBullet().setChar((char)8226);
para3.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
para3.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
// 箇条書きレベルを設定
para3.getParagraphFormat().setDepth((short)2);
// 4 番目の段落を追加
IParagraph para4 = new Paragraph();
para4.setText("Fourth Level");
para4.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para4.getParagraphFormat().getBullet().setChar('-');
para4.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
para4.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
// 箇条書きレベルを設定
para4.getParagraphFormat().setDepth((short)3);
// 段落をコレクションに追加
text.getParagraphs().add(para1);
text.getParagraphs().add(para2);
text.getParagraphs().add(para3);
text.getParagraphs().add(para4);
// プレゼンテーションを PPTX ファイルとして保存
pres.save("MultilevelBullet.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
カスタム番号付きリストの段落の管理
IBulletFormat インターフェイスは、NumberedBulletStartWith プロパティなどを提供し、カスタム番号付けや書式設定を持つ段落を管理できるようにします。
- Presentation クラスのインスタンスを作成します。
- 段落を含むスライドにアクセスします。
- スライドに autoshape を追加します。
- 自動シェイプの TextFrame にアクセスします。
TextFrameのデフォルト段落を削除します。- Paragraph クラスを使用して最初の段落インスタンスを作成し、[NumberedBulletStartWith] を 2 に設定します。
Paragraphクラスを使用して第 2 の段落インスタンスを作成し、NumberedBulletStartWithを 3 に設定します。Paragraphクラスを使用して第 3 の段落インスタンスを作成し、NumberedBulletStartWithを 7 に設定します。- 新しい段落を
TextFrameの段落コレクションに追加します。 - 変更されたプレゼンテーションを保存します。
この Java コードは、カスタム番号付けや書式設定を持つ段落を追加および管理する方法を示します:
Presentation presentation = new Presentation();
try {
IAutoShape shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// 作成した AutoShape のテキストフレームにアクセス
ITextFrame textFrame = shape.getTextFrame();
// 既存のデフォルト段落を削除
textFrame.getParagraphs().removeAt(0);
// 最初のリスト
Paragraph paragraph1 = new Paragraph();
paragraph1.setText("bullet 2");
paragraph1.getParagraphFormat().setDepth((short)4);
paragraph1.getParagraphFormat().getBullet().setNumberedBulletStartWith((short)2);
paragraph1.getParagraphFormat().getBullet().setType(BulletType.Numbered);
textFrame.getParagraphs().add(paragraph1);
Paragraph paragraph2 = new Paragraph();
paragraph2.setText("bullet 3");
paragraph2.getParagraphFormat().setDepth((short)4);
paragraph2.getParagraphFormat().getBullet().setNumberedBulletStartWith((short)3);
paragraph2.getParagraphFormat().getBullet().setType(BulletType.Numbered);
textFrame.getParagraphs().add(paragraph2);
Paragraph paragraph5 = new Paragraph();
paragraph5.setText("bullet 7");
paragraph5.getParagraphFormat().setDepth((short)4);
paragraph5.getParagraphFormat().getBullet().setNumberedBulletStartWith((short)7);
paragraph5.getParagraphFormat().getBullet().setType(BulletType.Numbered);
textFrame.getParagraphs().add(paragraph5);
presentation.save("SetCustomBulletsNumber-slides.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null) presentation.dispose();
}
段落インデントの設定
- Presentation クラスのインスタンスを作成します。
- インデックスで対象スライドへの参照にアクセスします。
- スライドに矩形の autoshape を追加します。
- 矩形の自動シェイプに、3 つの段落を含む TextFrame を追加します。
- 矩形の線を非表示にします。
- 各 Paragraph の BulletOffset プロパティを使用してインデントを設定します。
- 変更されたプレゼンテーションを PPT ファイルとして書き出します。
この Java コードは、段落インデントを設定する方法を示します:
// Presentation クラスのインスタンスを作成
Presentation pres = new Presentation();
try {
// 最初のスライドを取得
ISlide sld = pres.getSlides().get_Item(0);
// 四角形シェイプを追加
IAutoShape rect = sld.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 500, 150);
// 四角形に TextFrame を追加
ITextFrame tf = rect.addTextFrame("This is first line \rThis is second line \rThis is third line");
// テキストをシェイプに合わせて自動調整
tf.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
// 四角形の枠線を非表示にする
rect.getLineFormat().getFillFormat().setFillType(FillType.Solid);
// TextFrame の最初の段落を取得しインデントを設定
IParagraph para1 = tf.getParagraphs().get_Item(0);
// 段落の箇条書きスタイルとシンボルを設定
para1.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para1.getParagraphFormat().getBullet().setChar((char)8226);
para1.getParagraphFormat().setAlignment(TextAlignment.Left);
para1.getParagraphFormat().setDepth((short)2);
para1.getParagraphFormat().setIndent(30);
// TextFrame の2番目の段落を取得しインデントを設定
IParagraph para2 = tf.getParagraphs().get_Item(1);
para2.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para2.getParagraphFormat().getBullet().setChar((char)8226);
para2.getParagraphFormat().setAlignment(TextAlignment.Left);
para2.getParagraphFormat().setDepth((short)2);
para2.getParagraphFormat().setIndent(40);
// TextFrame の3番目の段落を取得しインデントを設定
IParagraph para3 = tf.getParagraphs().get_Item(2);
para3.getParagraphFormat().getBullet().setType(BulletType.Symbol);
para3.getParagraphFormat().getBullet().setChar((char)8226);
para3.getParagraphFormat().setAlignment(TextAlignment.Left);
para3.getParagraphFormat().setDepth((short)2);
para3.getParagraphFormat().setIndent(50);
//Write プレゼンテーションをディスクに保存
pres.save("InOutDent_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
段落のハンギングインデントの設定
この Java コードは、段落のハンギングインデントを設定する方法を示します:
Presentation pres = new Presentation();
try {
IAutoShape autoShape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 50, 250, 550, 150);
Paragraph para1 = new Paragraph();
para1.setText("Example");
Paragraph para2 = new Paragraph();
para2.setText("Set Hanging Indent for Paragraph");
Paragraph para3 = new Paragraph();
para3.setText("This code shows you how to set the hanging indent for a paragraph: ");
para2.getParagraphFormat().setMarginLeft(10f);
para3.getParagraphFormat().setMarginLeft(20f);
autoShape.getTextFrame().getParagraphs().add(para1);
autoShape.getTextFrame().getParagraphs().add(para2);
autoShape.getTextFrame().getParagraphs().add(para3);
pres.save("pres.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
段落の End ラン プロパティの管理
- Presentation クラスのインスタンスを作成します。
- 位置を使用して、段落を含むスライドへの参照を取得します。
- スライドに矩形の autoshape を追加します。
- 矩形に、2 つの段落を含む TextFrame を追加します。
- 段落の
FontHeightとフォントタイプを設定します。 - 段落の End プロパティを設定します。
- 変更されたプレゼンテーションを PPTX ファイルとして書き出します。
この Java コードは、PowerPoint の段落に対して End プロパティを設定する方法を示します:
Presentation pres = new Presentation();
try {
IAutoShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
Paragraph para1 = new Paragraph();
para1.getPortions().add(new Portion("Sample text"));
Paragraph para2 = new Paragraph();
para2.getPortions().add(new Portion("Sample text 2"));
PortionFormat portionFormat = new PortionFormat();
portionFormat.setFontHeight(48);
portionFormat.setLatinFont(new FontData("Times New Roman"));
para2.setEndParagraphPortionFormat(portionFormat);
shape.getTextFrame().getParagraphs().add(para1);
shape.getTextFrame().getParagraphs().add(para2);
pres.save(resourcesOutputPath+"pres.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
HTML テキストを段落にインポート
Aspose.Slides は、HTML テキストを段落にインポートするための高度なサポートを提供します。
- Presentation クラスのインスタンスを作成します。
- インデックスで対象スライドへの参照にアクセスします。
- スライドに autoshape を追加します。
autoshapeの [ITextFrame] を追加し、アクセスします。ITextFrameのデフォルト段落を削除します。- TextReader でソース HTML ファイルを読み取ります。
- Paragraph クラスを使用して最初の段落インスタンスを作成します。
- 読み取った TextReader の HTML ファイル内容を、TextFrame の ParagraphCollection に追加します。
- 変更されたプレゼンテーションを保存します。
この Java コードは、段落に HTML テキストをインポートする手順の実装例です:
// 空のプレゼンテーション インスタンスを作成
Presentation pres = new Presentation();
try {
// プレゼンテーションのデフォルトの最初のスライドにアクセス
ISlide slide = pres.getSlides().get_Item(0);
// HTML コンテンツを収容するために AutoShape を追加
IAutoShape ashape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10,
(float)pres.getSlideSize().getSize().getWidth() - 20, (float)pres.getSlideSize().getSize().getHeight() - 10);
ashape.getFillFormat().setFillType(FillType.NoFill);
// シェイプにテキストフレームを追加
ashape.addTextFrame("");
// 追加したテキストフレームのすべての段落をクリア
ashape.getTextFrame().getParagraphs().clear();
// ストリームリーダーを使用して HTML ファイルを読み込む
TextReader tr = new StreamReader("file.html");
// HTML ストリームリーダーからテキストをテキストフレームに追加
ashape.getTextFrame().getParagraphs().addFromHtml(tr.readToEnd());
// プレゼンテーションを保存
pres.save("output_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
段落テキストを HTML にエクスポート
Aspose.Slides は、段落に含まれるテキストを HTML にエクスポートするための高度なサポートを提供します。
- Presentation クラスのインスタンスを作成し、目的のプレゼンテーションをロードします。
- インデックスで対象スライドへの参照にアクセスします。
- HTML にエクスポートされるテキストを含むシェイプにアクセスします。
- シェイプの TextFrame にアクセスします。
StreamWriterのインスタンスを作成し、新しい HTML ファイルを追加します。- StreamWriter に開始インデックスを設定し、希望する段落をエクスポートします。
この Java コードは、PowerPoint の段落テキストを HTML にエクスポートする方法を示します:
// プレゼンテーションファイルを読み込む
Presentation pres = new Presentation("ExportingHTMLText.pptx");
try {
// プレゼンテーションのデフォルトの最初のスライドにアクセス
ISlide slide = pres.getSlides().get_Item(0);
// 目的のインデックス
int index = 0;
// 追加されたシェイプにアクセス
IAutoShape ashape = (IAutoShape) slide.getShapes().get_Item(index);
// 出力 HTML ファイルを作成
OutputStream os = new FileOutputStream("output.html");
Writer writer = new OutputStreamWriter(os, "UTF-8");
// 最初の段落を HTML として抽出
// 段落の開始インデックスとコピーする段落数を指定して段落データを HTML に書き込む
writer.write(ashape.getTextFrame().getParagraphs().exportToHtml(0, ashape.getTextFrame().getParagraphs().getCount(), null));
writer.close();
} catch (IOException e) {
} finally {
if (pres != null) pres.dispose();
}
段落を画像として保存
このセクションでは、[IParagraph] インターフェイスで表されるテキスト段落を画像として保存する方法を示す 2 つの例を検証します。どちらの例も、[IShape] インターフェイスの getImage メソッドを使用して段落を含むシェイプの画像を取得し、シェイプ内の段落の境界を計算し、ビットマップ画像としてエクスポートする手順を含みます。これらのアプローチにより、PowerPoint プレゼンテーションからテキストの特定部分を抽出し、個別の画像として保存でき、さまざまなシナリオでの活用に役立ちます。
プレゼンテーションファイル sample.pptx があり、1 枚のスライドがあり、最初のシェイプが 3 つの段落を含むテキストボックスであると想定しましょう。

例 1
この例では、2 番目の段落を画像として取得します。
そのために、プレゼンテーションの最初のスライドからシェイプの画像を抽出し、シェイプのテキストフレーム内の 2 番目の段落の境界を計算します。
次に、段落を新しいビットマップ画像に再描画し、PNG 形式で保存します。
この方法は、テキストの正確なサイズと書式設定を保持しながら、特定の段落を個別の画像として保存する必要がある場合に特に有用です。
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape firstShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
// 形状をメモリ内にビットマップとして保存します。
IImage shapeImage = firstShape.getImage();
ByteArrayOutputStream shapeImageStream = new ByteArrayOutputStream();
shapeImage.save(shapeImageStream, ImageFormat.Png);
shapeImage.dispose();
// メモリから形状ビットマップを作成します。
InputStream shapeImageInputStream = new ByteArrayInputStream(shapeImageStream.toByteArray());
BufferedImage shapeBitmap = ImageIO.read(shapeImageInputStream);
// 第2段落の境界を計算します。
IParagraph secondParagraph = firstShape.getTextFrame().getParagraphs().get_Item(1);
Rectangle2D paragraphRectangle = secondParagraph.getRect();
// 出力画像の座標とサイズを計算します(最小サイズ 1x1 ピクセル)。
int imageX = (int) Math.floor(paragraphRectangle.getX());
int imageY = (int) Math.floor(paragraphRectangle.getY());
int imageWidth = Math.max(1, (int) Math.ceil(paragraphRectangle.getWidth()));
int imageHeight = Math.max(1, (int) Math.ceil(paragraphRectangle.getHeight()));
// 形状ビットマップを切り取って段落ビットマップだけを取得します。
BufferedImage paragraphBitmap = shapeBitmap.getSubimage(imageX, imageY, imageWidth, imageHeight);
ImageIO.write(paragraphBitmap, "png", new File("paragraph.png"));
} catch (IOException e) {
} finally {
if (presentation != null) presentation.dispose();
}
結果:

例 2
この例では、段落画像にスケーリング係数を追加して前のアプローチを拡張します。
シェイプはプレゼンテーションから抽出され、スケーリング係数 2 で画像として保存されます。
これにより、段落をエクスポートする際に高解像度の出力が可能になります。
スケールを考慮して段落の境界が計算されます。
スケーリングは、例えば高品質な印刷物での使用など、より詳細な画像が必要な場合に特に有用です。
float imageScaleX = 2f;
float imageScaleY = imageScaleX;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape firstShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
// スケーリング付きで形状をメモリ内にビットマップとして保存します。
IImage shapeImage = firstShape.getImage(ShapeThumbnailBounds.Shape, imageScaleX, imageScaleY);
ByteArrayOutputStream shapeImageStream = new ByteArrayOutputStream();
shapeImage.save(shapeImageStream, ImageFormat.Png);
shapeImage.dispose();
// メモリから形状ビットマップを作成します。
InputStream shapeImageInputStream = new ByteArrayInputStream(shapeImageStream.toByteArray());
BufferedImage shapeBitmap = ImageIO.read(shapeImageInputStream);
// 第2段落の境界を計算します。
IParagraph secondParagraph = firstShape.getTextFrame().getParagraphs().get_Item(1);
Rectangle2D paragraphRectangle = secondParagraph.getRect();
paragraphRectangle.setRect(
paragraphRectangle.getX() * imageScaleX,
paragraphRectangle.getY() * imageScaleY,
paragraphRectangle.getWidth() * imageScaleX,
paragraphRectangle.getHeight() * imageScaleY
);
// 出力画像の座標とサイズを計算します(最小サイズ 1x1 ピクセル)。
int imageX = (int) Math.floor(paragraphRectangle.getX());
int imageY = (int) Math.floor(paragraphRectangle.getY());
int imageWidth = Math.max(1, (int) Math.ceil(paragraphRectangle.getWidth()));
int imageHeight = Math.max(1, (int) Math.ceil(paragraphRectangle.getHeight()));
// 形状ビットマップを切り取って段落ビットマップだけを取得します。
BufferedImage paragraphBitmap = shapeBitmap.getSubimage(imageX, imageY, imageWidth, imageHeight);
ImageIO.write(paragraphBitmap, "png", new File("paragraph.png"));
} catch (IOException e) {
} finally {
if (presentation != null) presentation.dispose();
}
よくある質問
テキストフレーム内で改行を完全に無効にできますか?
はい。テキストフレームの折り返し設定(setWrapText) を使用して折り返しをオフにすれば、フレームの端で行が折り返されなくなります。
特定の段落のスライド上での正確な境界を取得するにはどうすればよいですか?
段落(場合によっては単一の部分)のバウンディング矩形を取得すれば、スライド上での正確な位置とサイズを知ることができます。
段落の配置(左揃え/右揃え/中央揃え/均等割付)はどこで制御されますか?
Alignment は ParagraphFormat の段落レベル設定であり、個々の部分の書式設定に関係なく段落全体に適用されます。
段落の一部(例: 単語)だけにスペルチェックの言語を設定できますか?
はい。言語は PortionFormat.setLanguageId により部分レベルで設定されるため、単一の段落内に複数の言語を共存させることが可能です。