Androidのプレゼンテーションから高度なテキスト抽出
スライドからテキストを抽出
Aspose.Slides for Android via Java は SlideUtil クラスを提供します。このクラスは、プレゼンテーションまたはスライド全体のテキストを抽出するための多数のオーバーロードされた静的メソッドを公開しています。PPTX プレゼンテーションのスライドからテキストを抽出するには、SlideUtil クラスが提供する getAllTextBoxes のオーバーロードされた静的メソッドを使用します。このメソッドは Slide オブジェクトをパラメーターとして受け取ります。 実行すると、Slide メソッドはパラメーターとして渡されたスライド全体のテキストをスキャンし、TextFrame オブジェクトの配列を返します。これにより、テキストに関連付けられたすべての書式情報が利用可能になります。以下のコードは、プレゼンテーションの最初のスライド上のすべてのテキストを抽出します。
//PPTX ファイルを表す Presentation クラスのインスタンスを作成します
Presentation pres = new Presentation("demo.pptx");
try {
for (ISlide slide : pres.getSlides())
{
//PPTX のすべてのスライドから ITextFrame オブジェクトの配列を取得します
ITextFrame[] textFramesPPTX = SlideUtil.getAllTextBoxes(slide);
//TextFrame の配列をループします
for (int i = 0; i < textFramesPPTX.length; i++) {
//現在の ITextFrame の段落をループします
for (IParagraph para : textFramesPPTX[i].getParagraphs()) {
//現在の IParagraph の部分をループします
for (IPortion port : para.getPortions()) {
//現在の部分のテキストを表示します
System.out.println(port.getText());
//テキストのフォント高さを表示します
System.out.println(port.getPortionFormat().getFontHeight());
//テキストのフォント名を表示します
if (port.getPortionFormat().getLatinFont() != null)
System.out.println(port.getPortionFormat().getLatinFont().getFontName());
}
}
}
}
} finally {
pres.dispose();
}
プレゼンテーションからテキストを抽出
プレゼンテーション全体のテキストをスキャンするには、SlideUtil クラスが提供する getAllTextFrames 静的メソッドを使用します。このメソッドは 2 つのパラメーターを受け取ります。
- 最初に、テキストを抽出する対象となるプレゼンテーションを表す Presentation オブジェクト。
- 次に、プレゼンテーションからテキストをスキャンする際にマスタースライドを含めるかどうかを決定するブール値。
このメソッドは、テキスト書式情報を含む TextFrame オブジェクトの配列を返します。以下のコードは、マスタースライドを含めてプレゼンテーションのテキストと書式情報をスキャンします。
//PPTX ファイルを表す Presentation クラスのインスタンスを作成します
Presentation pres = new Presentation("demo.pptx");
try {
//PPTX のすべてのスライドから ITextFrame オブジェクトの配列を取得します
ITextFrame[] textFramesPPTX = SlideUtil.getAllTextFrames(pres, true);
//TextFrame 配列をループします
for (int i = 0; i < textFramesPPTX.length; i++)
{
//現在の ITextFrame の段落をループします
for (IParagraph para : textFramesPPTX[i].getParagraphs())
{
//現在の IParagraph の部分をループします
for (IPortion port : para.getPortions())
{
//現在の部分のテキストを表示します
System.out.println(port.getText());
//テキストのフォント高さを表示します
System.out.println(port.getPortionFormat().getFontHeight());
//テキストのフォント名を表示します
if (port.getPortionFormat().getLatinFont() != null)
System.out.println(port.getPortionFormat().getLatinFont().getFontName());
}
}
}
} finally {
pres.dispose();
}
カテゴリ別かつ高速なテキスト抽出
Presentation クラスに新しい静的メソッド getPresentationText が追加されました。このメソッドには 3 つのオーバーロードがあります。
public IPresentationText getPresentationText(String file, int mode);
public IPresentationText getPresentationText(InputStream stream, int mode);
public IPresentationText getPresentationText(InputStream stream, int mode, ILoadOptions options);
The TextExtractionArrangingMode enum argument indicates the mode to organize the output of text result and can be set to the following values:
- Unarranged - The raw text with no respect to position on the slide
- Arranged - The text is positioned in the same order as on the slide
Unarranged mode can be used when speed is critical, it’s faster than Arranged mode.
IPresentationText represents the raw text extracted from the presentation. It contains a getSlidesText method which returns an array of ISlideText objects. Every object represent the text on the corresponding slide. ISlideText object have the following methods:
- ISlideText.getText - The text on the slide’s shapes
- ISlideText.getMasterText - The text on the master page’s shapes for this slide
- ISlideText.getLayoutText - The text on the layout page’s shapes for this slide
- ISlideText.getNotesText - The text on the notes page’s shapes for this slide
There is also a SlideText class which implements the ISlideText interface.
The new API can be used like this:
IPresentationText text1 = PresentationFactory.getInstance().getPresentationText("presentation.pptx", TextExtractionArrangingMode.Unarranged);
System.out.println(text1.getSlidesText()[0].getText());
System.out.println(text1.getSlidesText()[0].getLayoutText());
System.out.println(text1.getSlidesText()[0].getMasterText());
System.out.println(text1.getSlidesText()[0].getNotesText());
FAQ
Aspose.Slides はテキスト抽出時に大規模なプレゼンテーションをどの程度高速に処理できますか?
Aspose.Slides は高性能に最適化されており、大規模プレゼンテーション でも効率的に処理できるため、リアルタイムやバルク処理シナリオに適しています。
Aspose.Slides はプレゼンテーション内の表やチャートからテキストを抽出できますか?
はい、Aspose.Slides は表、チャート、その他の複雑なスライド要素からのテキスト抽出を完全にサポートしており、すべてのテキストコンテンツに簡単にアクセスし分析できます。
プレゼンテーションからテキストを抽出するために特別な Aspose.Slides ライセンスが必要ですか?
無料体験版の Aspose.Slides でもテキスト抽出は可能ですが、スライド数に制限があるなどの制約があります。制限なく使用し、より大きなプレゼンテーションを処理するには、正規ライセンスの購入が推奨されます。