チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
マイクロソフト株式会社によって開発された PowerPoint プレゼンテーションは、視覚的に魅力的でインタラクティブなコンテンツを作成するために使用される多目的なファイル形式です。テキストや画像などの豊富な機能と複数の要素を含んでおり、ビジネスの紹介や学術的なスピーチなど、さまざまなシナリオで強力なツールとなっています。PowerPoint のテキストを編集や操作する必要がある場合、プログラムでテキストを抽出し、新しいファイルに保存することは良い選択です。この記事では、Spire.Presentation for Java を使用して PowerPoint からテキストを抽出する方法を示します。
まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>8.7.3</version>
</dependency>
</dependencies>
Spire.Presentation for Java では、ParagraphEx.getText() メソッドを使用して、すべてのスライドをループし、各スライドの段落からテキストを抽出することができます。詳細な手順は次のとおりです。
import com.spire.presentation.*;
import java.io.*;
public class ExtractText {
public static void main(String[] args) throws Exception {
//Presentationクラスのオブジェクトを作成する
Presentation presentation = new Presentation();
//PowerPointプレゼンテーションをロードする
presentation.loadFromFile("sample.pptx");
//StringBuilderオブジェクトを作成する
StringBuilder buffer = new StringBuilder();
//各スライドをループしてテキストを抽出する
for (Object slide : presentation.getSlides()) {
for (Object shape : ((ISlide) slide).getShapes()) {
if (shape instanceof IAutoShape) {
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText()+"\n");
}
}
}
}
//抽出したテキストを新しい.txtファイルに書き込む
FileWriter writer = new FileWriter("output/ExtractAllText.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
presentation.dispose();
}
}
Spire.Presentation for Java では、特定のスライドからテキストを抽出することもサポートしています。テキストを抽出する前に、Presentation.getSlides().get() メソッドを呼び出して、必要なスライドを取得してください。以下に詳細な手順を示します。
import com.spire.presentation.*;
import java.io.*;
public class ExtractText {
public static void main(String[] args) throws Exception {
//Presentationクラスのオブジェクトを作成する
Presentation presentation = new Presentation();
//PowerPointプレゼンテーションをロードする
presentation.loadFromFile("sample.pptx");
//StringBuilderオブジェクトを作成する
StringBuilder buffer = new StringBuilder();
//最初のスライドを取得する
ISlide Slide = presentation.getSlides().get(0);
//各形状の段落をループしてテキストを抽出する
for (Object shape : Slide.getShapes()) {
if (shape instanceof IAutoShape) {
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText()+"\n");
}
}
}
//抽出したテキストを新しい.txtファイルに書き込む
FileWriter writer = new FileWriter("output/ExtractSlideText.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
presentation.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
PowerPoint プレゼンテーションで重要な情報を強調したい場合は、明るい色でハイライトすることができます。これは、視聴者が重要な情報を迅速に取得するのにも役立ちます。この記事では、Spire.Presentation for Java を使用して PowerPoint 内のテキストをハイライトする方法を紹介します。
まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>8.1.2</version>
</dependency>
</dependencies>
Presentation のテキストをハイライトするには、まずスライドと各スライドの図形をループします。IAutoShape.getTextFrame().highLightText() メソッドを使用して、図形内の指定されたテキストを他の色でハイライトすることができます。以下に詳細な操作手順を示します。
import com.spire.presentation.*;
import java.awt.*;
public class HighlightTextInPPT {
public static void main(String []args) throws Exception {
//Presentationクラスのインスタンスを作成する
Presentation presentation = new Presentation();
//PowerPointファイルをロードする
presentation.loadFromFile("Input.pptx");
//すべてのスライドをループする
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
//現在のスライドを取得する
ISlide slide = presentation.getSlides().get(i);
//スライドの図形をループする
for (int j = 0; j < slide.getShapes().getCount(); j++)
{
//現在の図形がIAutoShapeタイプであることを確認する
if (slide.getShapes().get(j) instanceof IAutoShape)
{
//図形をIAutoShapeに変換する
IAutoShape shape = (IAutoShape)slide.getShapes().get(j);
//TextHighLightingOptionsクラスのインスタンスを作成する
TextHighLightingOptions options = new TextHighLightingOptions();
//テキストのハイライトオプションを設定する
options.setCaseSensitive(true);
options.setWholeWordsOnly(true);
//図形内の特定のテキストをハイライトする
shape.getTextFrame().highLightText("Spire.Presentation", Color.YELLOW, options);
}
}
}
//結果ファイルを保存する
presentation.saveToFile("HighlightText.pptx", FileFormat.PPTX_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
PowerPoint プレゼンテーションを作成する際には、いかにスライドを読みやすくするかは非常に重要です。最も一般的な方法の1つは、テキストを番号付き、箇条書きリストとしてフォーマットすることです。この方法は、簡潔で明瞭な方法で情報を表現することができ、読者の注意を引くことができます。この記事では、Spire.Presentation for Java を使用して PowerPoint で番号付きと箇条書きリストを作成する方法を紹介します。
まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>8.1.2</version>
</dependency>
</dependencies>
Spire.Presentation では、番号付き、箇条書きリストを作成するために段落の前に数字や行頭記号を追加できます。ParagraphEx.setBulletType() メソッドを使用して、行頭文字のタイプを指定できます。以下は具体的な操作手順です。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class CreateNumberedList {
public static void main(String[] args) throws Exception {
//Presentationオブジェクトを作成する
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//最初のスライドを取得する
ISlide slide = presentation.getSlides().get(0);
//スライドに図形を追加し、図形のスタイルを設定する
Rectangle2D rect = new Rectangle2D.Double(50, 50, 300, 200);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
shape.getLine().setFillType(FillFormatType.NONE);
shape.getFill().setFillType(FillFormatType.NONE);
//デフォルト段落にテキストを追加する
ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
titleParagraph.setText("スキル:");
titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
titleParagraph.setAlignment(TextAlignmentType.LEFT);
//リスト内容を指定する
String[] listContent = new String[] {
" Command-line Unix",
" Vim",
" HTML",
" CSS",
" Python",
" JavaScript",
" SQL"
};
//番号付きリストを作成する
for(int i = 0; i < listContent.length; i ++)
{
ParagraphEx paragraph = new ParagraphEx();
shape.getTextFrame().getParagraphs().append(paragraph);
paragraph.setText(listContent[i]);
paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
paragraph.setBulletType(TextBulletType.NUMBERED);
paragraph.setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
}
//ドキュメントをPowerPointに保存する
presentation.saveToFile("NumberedList.pptx", FileFormat.PPTX_2013);
}
}
記号の箇条書きリストを作成する方法は、番号付きリストを作成する方法とよく似ています。行頭文字のタイプを SYMBOL に設定するだけです。以下に詳細な操作手順を示します。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class CreateBulletedListWithSymbol {
public static void main(String[] args) throws Exception {
//Presentationオブジェクトを作成する
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//最初のスライドを取得する
ISlide slide = presentation.getSlides().get(0);
//スライドに図形を追加し、図形のスタイルを設定する
Rectangle2D rect = new Rectangle2D.Double(50, 50, 350, 200);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
shape.getLine().setFillType(FillFormatType.NONE);
shape.getFill().setFillType(FillFormatType.NONE);
//デフォルト段落にテキストを追加する
ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
titleParagraph.setText("スキル:");
titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
titleParagraph.setAlignment(TextAlignmentType.LEFT);
//リスト内容を指定する
String[] listContent = new String[] {
" Command-line Unix",
" Vim",
" HTML",
" CSS",
" Python",
" JavaScript",
" SQL"
};
//記号の箇条書きリストを作成する
for(int i = 0; i < listContent.length; i ++)
{
ParagraphEx paragraph = new ParagraphEx();
shape.getTextFrame().getParagraphs().append(paragraph);
paragraph.setText(listContent[i]);
paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
paragraph.setBulletType(TextBulletType.SYMBOL);
}
//ドキュメントをPowerPointに保存する
presentation.saveToFile("SymbolBullets.pptx", FileFormat.PPTX_2013);
}
}
画像を行頭文字として使用するには、行頭文字のタイプを PICTURE に設定し、画像を BulletPicture オブジェクトに指定します。以下に詳細な操作手順を示します。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
public class CreateBulletedListWithImage {
public static void main(String[] args) throws Exception {
//Presentationオブジェクトを作成する
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//最初のスライドを取得する
ISlide slide = presentation.getSlides().get(0);
//スライドに図形を追加し、図形のスタイルを設定する
Rectangle2D rect = new Rectangle2D.Double(50, 50, 400, 180);
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
shape.getLine().setFillType(FillFormatType.NONE);
shape.getFill().setFillType(FillFormatType.NONE);
//デフォルト段落にテキストを追加する
ParagraphEx titleParagraph = shape.getTextFrame().getParagraphs().get(0);
titleParagraph.setText("To-Do List:");
titleParagraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
titleParagraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
titleParagraph.setAlignment(TextAlignmentType.LEFT);
//リスト内容を指定する
String[] listContent = new String[] {
" プロジェクトとタスクの定義",
" タスクに人を割り当てる",
" タスクの優先度の定義",
" タスクの進捗状況の追跡",
" 仕事のまとめ"
};
//画像の箇条書きリストを作成する
BufferedImage image = ImageIO.read(new File("image.jpg"));
for(int i = 0; i < listContent.length; i ++)
{
ParagraphEx paragraph = new ParagraphEx();
shape.getTextFrame().getParagraphs().append(paragraph);
paragraph.setText(listContent[i]);
paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
paragraph.setBulletType(TextBulletType.PICTURE);
paragraph.getBulletPicture().setEmbedImage(presentation.getImages().append(image));
}
//ドキュメントをPowerPointに保存する
presentation.saveToFile("ImageBullets.pptx", FileFormat.PPTX_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
プレゼンテーションに商標、著作権、その他の記号を追加するときに、その記号を特定のテキストより少し上または下にしたい場合があります。Microsoft PowerPoint では、記号に上付き文字または下付き文字のフォーマットを適用することで、この効果を実現することができます。この記事では、Spire.Presentation for Java を使用して PowerPoint に上付き文字と下付き文字を追加する方法を紹介します。
まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>8.1.2</version>
</dependency>
</dependencies>
Spire.Presentation for Java が提供する PortionEx.getFormat().setScriptDistance(float value) メソッドは、テキストに上付き文字と下付き文字のフォーマットを適用することがサポートします。この値は正または負の値に設定できます。正の値が大きいほど、上付き文字がテキストの上に表示されます。負の値が小さいほど、テキストの下に下付き文字が表示されます。以下に詳細な操作手順を示します。
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class AddSuperscriptAndSubscript {
public static void main(String []args) throws Exception {
//PowerPointドキュメントをロードする
Presentation presentation = new Presentation();
presentation.loadFromFile("sample.pptx");
//最初のスライドを取得する
ISlide slide = presentation.getSlides().get(0);
//スライドに図形を追加する
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(150, 100, 200, 50));
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
//図形のテキストフレームにアクセスする
ITextFrameProperties textFrame = shape.getTextFrame();
//テキストフレームのデフォルト段落を取り除きする
textFrame.getParagraphs().clear();
//標準テキストの段落を作成する
ParagraphEx para = new ParagraphEx();
para.setText("E=mc");
//上付きテキストのポーションを作成する
PortionEx tr = new PortionEx("2");
tr.getFormat().setScriptDistance(40);
//そのポーションを段落に付加する
para.getTextRanges().append(tr);
para.getTextRanges().append(new PortionEx("\n"));
//標準テキストの色、フォント、フォントサイズを設定する
tr = para.getTextRanges().get(0);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(new Color(128,0,128));
tr.setFontHeight(20);
tr.setLatinFont(new TextFont("Arial"));
//上付きテキストの色とフォントを設定する
tr = para.getTextRanges().get(1);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(Color.BLUE);
tr.setLatinFont(new TextFont("Arial"));
//図形のテキストフレームに段落を付加する
textFrame.getParagraphs().append(para);
//標準テキストの段落を作成する
para = new ParagraphEx();
para.setText("X");
//下付きテキストのポーションを作成する
tr = new PortionEx("100");
tr.getFormat().setScriptDistance(-25);
//そのポーションを段落に付加する
para.getTextRanges().append(tr);
//標準テキストの色、フォント、フォントサイズを設定する
tr = para.getTextRanges().get(0);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(new Color(128,0,128));
tr.setFontHeight(20);
tr.setLatinFont(new TextFont("Arial"));
//下付きテキストの色とフォントを設定する
tr = para.getTextRanges().get(1);
tr.getFill().setFillType(FillFormatType.SOLID);
tr.getFill().getSolidColor().setColor(Color.BLUE);
tr.setLatinFont(new TextFont("Arial"));
//図形のテキストフレームに段落をアタッチする
textFrame.getParagraphs().append(para);
//結果ドキュメントを保存する
presentation.saveToFile("AddSuperscriptAndSubscript.pptx", FileFormat.PPTX_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。