Java でプレゼンテーションのテキストをフォーマット
概要
この記事では、Aspose.Slides for Java を使用して PowerPoint および OpenDocument プレゼンテーションのテキストをフォーマットする方法を示します。背景色、透明度、文字間隔、フォントプロパティ、回転、段落間隔、オートフィットの動作、テキストのアンカリング、タブストップ、言語設定について説明します。
特に記載がない限り、例では sample.pptx を使用します。最初のスライドの最初のシェイプはテキストボックスで、その最初の段落に以下に示すテキストが含まれています。スライドとシェイプのインデックスはゼロベースです。太字部分を選択する例は、継承された太字書式を含む実効書式を使用します:

リテラルテキストまたは正規表現の一致を検索してハイライトするには、Search and Replace Text を参照してください。
テキストの背景色の設定
IParagraphFormat.getDefaultPortionFormat を使用して段落のデフォルトハイライト色を設定するか、個々のテキスト部分には IBasePortionFormat.getHighlightColor を使用します。
次の例は、最初の段落のデフォルトとして薄いグレーのハイライトを設定します。個別の部分に明示的に設定されたハイライト色はこのデフォルトより優先されます:
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 段落全体のハイライト色を設定します。
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY);
presentation.save("gray_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

以下のコード例は、太字フォントのテキスト部分 の背景色を設定する方法を示しています:
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// テキスト部分のハイライト色を設定します。
portion.getPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY);
}
}
presentation.save("gray_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

テキスト段落の配置
IParagraphFormat.setAlignment を使用してテキストフレーム内の段落の配置を設定します。値は中央揃え、左揃え、右揃え、両端揃えなどがあります。
次のコード例は、段落を 中央 に配置する方法を示します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 段落の配置をセンターに設定します。
paragraph.getParagraphFormat().setAlignment(TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

行内のフォントの配置
IParagraphFormat.setFontAlignment を使用して、行内の異なるフォントサイズのテキスト部分を垂直方向に揃えます。この設定は段落全体に適用され、各行内の配置を制御します。
次の独立した例は、1枚のスライドに4つのラベル付きテキストボックスを作成します。各段落は 18、36、54 ポイントの同じテキストを含み、フォント配置が異なります。Arial を使用し、オートフィットと折り返しを無効にし、テキストフレームは1行分の幅を確保します。
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
int[] alignments = { FontAlignment.Baseline, FontAlignment.Top, FontAlignment.Center, FontAlignment.Bottom };
String[] alignmentNames = { "Baseline", "Top", "Center", "Bottom" };
float[] fontSizes = { 18f, 36f, 54f };
for (int i = 0; i < alignments.length; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 20 + i * 130, 660, 120);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setWrapText(NullableBool.False);
IParagraph label = textFrame.getParagraphs().get_Item(0);
label.setText(alignmentNames[i]);
label.getParagraphFormat().setAlignment(TextAlignment.Left);
label.getParagraphFormat().getDefaultPortionFormat().setFontHeight(14);
label.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().setFontAlignment(alignments[i]);
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
for (float fontSize : fontSizes) {
Portion portion = new Portion("Ag ");
portion.getPortionFormat().setFontHeight(fontSize);
paragraph.getPortions().add(portion);
}
textFrame.getParagraphs().add(paragraph);
}
presentation.save("font_alignment.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

フォント配置はフォントメトリクスに基づくため、個々の文字の見た目の端が必ずしも正確に揃うわけではありません。この例では、大文字とディセンダーの両方を含め、ベースラインと下部配置の違いを示しています。フォントの利用可否や置換、使用する文字、フォントサイズの違いが結果に影響します。また、フレームのサイズ、余白、行間、折り返し、オートフィットもレイアウトに影響するため、モードを比較する際は同じフォントとレイアウト設定を使用してください。
この設定は、水平段落配置を制御する IParagraphFormat.setAlignment や、シェイプ内でテキストブロックを垂直方向に配置する ITextFrameFormat.setAnchoringType とは異なります。IBasePortionFormat.setEscapement による上付き文字や下付き文字の書式設定は、ベースラインに対して個々の部分をシフトさせ、段落の行のフォント配置を設定するものではありません。
テキストの透明度の設定
テキストの透明度は、IBasePortionFormat.getFillFormat に設定された色のアルファ成分で制御します。以下の例では、alpha = 50 は 0〜255 のスケールの ARGB アルファチャネル値であり、透明度のパーセンテージではありません。
以下のコード例は、段落全体 に透明度を適用する方法を示します:
import com.aspose.slides.*;
import java.awt.Color;
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// テキストの塗りつぶし色を透明色に設定します。
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(0, 0, 0, alpha));
presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

次のコード例は、太字フォントのテキスト部分 に透明度を適用する方法を示します:
import com.aspose.slides.*;
import java.awt.Color;
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// テキスト部分の透明度を設定します。
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(0, 0, 0, alpha));
}
}
presentation.save("transparent_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

テキストの文字間隔の設定
IBasePortionFormat.setSpacing を使用してテキストボックス内の文字間の間隔を拡大または縮小します。例では 3 ポイントの間隔を追加しています。負の値はテキストを縮めます。
次の Java コードは、段落全体 の文字間隔を拡大する方法を示します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 注: 文字間隔を圧縮するには負の値を使用します。
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // 文字間隔を拡張します。
presentation.save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

以下のコード例は、太字フォントのテキスト部分 の文字間隔を拡大する方法を示します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// 注: 文字間隔を圧縮するには負の値を使用します。
portion.getPortionFormat().setSpacing(3); // 文字間隔を拡張します。
}
}
presentation.save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

特定フォントのカーニングを無効にする
場合によっては、Aspose.Slides がレンダリングしたテキストが PowerPoint の同じテキストよりやや詰まって見えることがあります。これは、PowerPoint が特定のフォントのカーニングデータを無視するためで、フォントに有効なカーニング情報があり、PowerPoint の設定でカーニングが有効になっていても起こります。
このような場合にレンダリング結果を PowerPoint に近づけるには、影響を受けたフォントを使用するテキスト部分のカーニングを無効にできます。IBasePortionFormat.setKerningMinimalSize に実際のフォントサイズより大きな値を設定します。この例では、最初のスライドの最初のシェイプがテキストボックスである “presentation.pptx” が必要です。効果的なフォント名(継承フォントを含む)を確認し、Roboto を使用する部分に対して 100 ポイントの閾値を設定します。これにより、フォントサイズが 100 ポイント未満の該当部分のカーニングが無効になります:
import com.aspose.slides.*;
Presentation presentation = new Presentation("presentation.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
String targetFont = "Roboto";
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
IPortionFormatEffectiveData portionFormat = portion.getPortionFormat().getEffective();
if ((portionFormat.getLatinFont() != null &&
portionFormat.getLatinFont().getFontName().equals(targetFont)) ||
(portionFormat.getEastAsianFont() != null &&
portionFormat.getEastAsianFont().getFontName().equals(targetFont)) ||
(portionFormat.getComplexScriptFont() != null &&
portionFormat.getComplexScriptFont().getFontName().equals(targetFont))) {
portion.getPortionFormat().setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
閾値以下の該当テキストに対して、この設定はカーニングを防止し、PowerPoint 固有の動作の影響を受けるフォントの視覚的出力を Aspose.Slides のレンダリングと合わせるのに役立ちます。
テキストフォントプロパティの管理
フォントプロパティは、段落レベルでは IParagraphFormat.getDefaultPortionFormat を、個々の部分では IPortionFormat を使用して設定できます。
次の例は、最初の段落のデフォルトフォントを 12 ポイントの Times New Roman に設定し、太字、イタリック、点線下線の書式を適用します。個々の部分に対する明示的な書式設定は、これらのデフォルトより優先されます。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// 段落のフォントプロパティを設定します。
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

次の例は、実効書式が太字の部分に対して、13 ポイントの Times New Roman、イタリック書式、および点線下線を適用します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// テキスト部分のフォントプロパティを設定します。
portion.getPortionFormat().setFontHeight(13);
portion.getPortionFormat().setFontItalic(NullableBool.True);
portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
portion.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

テキスト高さ正規化の制御
IBasePortionFormat.setNormaliseHeight は、テンプレートのタイポグラフィを再現する際や文字高さの差異を調査する際に使用します。PowerPoint では、正規化を有効にすると、テキスト部分内の文字の可視高さを均一にするために個々の文字を垂直方向に伸縮させます。ただし、フォント間で同一のテキスト高さが保証されるわけではありません。
プロパティを NullableBool.True または False に設定すると、正規化を明示的に有効または無効にできます。NullableBool.NotDefined はローカル設定を未定義のままにし、たとえば段落の getDefaultPortionFormat から値を継承させます。これは False を意味するものではありません。IPortionFormat.getEffective を使用し、IBasePortionFormatEffectiveData.getNormaliseHeight を読み取って、評価されたブール値を取得してください。
次の独立した例は Arial のインストールが必要です。54 ポイントで同じ大文字、小文字、ディセンダー、アクセント文字を含む 2 つのテキストブロックを作成します。両方の段落はデフォルトで正規化を有効にしています。上部の部分は明示的に無効にし、下部の部分は継承します。フレームサイズ、余白、段落間隔は同一で、比較を一貫させるために折り返しとオートフィットは無効にしています。例は False: false と NotDefined: true を出力し、“text_height_normalization.pptx” に保存します。
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
byte[] settings = { NullableBool.False, NullableBool.NotDefined };
String[] settingNames = { "False", "NotDefined" };
for (int i = 0; i < settings.length; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 60 + i * 200, 660, 150);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.setText("Hxgjpq ÅÉ 0123");
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setWrapText(NullableBool.False);
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getTextFrameFormat().setMarginTop(0);
textFrame.getTextFrameFormat().setMarginLeft(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceBefore(0);
paragraph.getParagraphFormat().setSpaceAfter(0);
paragraph.getParagraphFormat().setSpaceWithin(100);
paragraph.getParagraphFormat().getDefaultPortionFormat().setNormaliseHeight(NullableBool.True);
IPortion portion = paragraph.getPortions().get_Item(0);
FontData font = new FontData("Arial");
portion.getPortionFormat().setLatinFont(font);
portion.getPortionFormat().setFontHeight(54);
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
portion.getPortionFormat().setNormaliseHeight(settings[i]);
System.out.println(settingNames[i] + ": " + portion.getPortionFormat().getEffective().getNormaliseHeight());
}
presentation.save("text_height_normalization.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
以下の比較は保存されたプレゼンテーションからエクスポートしたものです。上段は正規化が無効 (False) で、下段は有効な設定を継承しています (NotDefined → True)。この外観を再現するには Arial をインストールしたままにしてください。

正規化は、ポイントで指定されたフォントサイズを示す getFontHeight の値を変更せずに文字の高さを変えます。段落の setSpaceWithin は行間を制御し、テキストフレームの setAutofitType はテキストまたはそのコンテナのフィット方法を制御します。これらの設定は目的が異なり、全体のレイアウトに独立して影響を与える可能性があります。段落の書式設定と継承の詳細については、Manage Paragraph を参照してください。
取り消し線の適用と除去
IBasePortionFormat.setStrikethroughType をテキスト部分に使用すると、元のテキストを削除せずに訂正や取り消し語、以前の価格を表示できます。テキストに 1 本または 2 本の線を引くには、TextStrikethroughType.Single または Double を設定します。
取り消し線を除去するには、TextStrikethroughType.None を指定します。これにより、周囲の書式設定で取り消し線が指定されていても効果が明示的に無効化されます。対照的に、TextStrikethroughType.NotDefined はローカル設定を未定義のままにし、段落などから書式を継承できるようにします。取り消し線が必ず除去されるわけではありません。結果の書式設定(継承された値を含む)を取得するには、IPortionFormat.getEffective を使用してください。
次の独立した例は、3 つの価格比較を作成します。各段落はラベル、以前の価格、現在の価格の各部分を分けているため、影響を受けるのは以前の価格だけです。最初の行は取り消し線を適用し、次に除去します。残りの行は単一と二重の取り消し線を保持します。この設定を変更しても、テキストと他の書式設定(太字の以前の価格を含む)は保持されます。
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, 620, 220);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getTextFrame().getParagraphs().clear();
String[] labels = { "None: ", "Single: ", "Double: " };
for (String label : labels) {
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceAfter(20);
IPortionFormat format = paragraph.getParagraphFormat().getDefaultPortionFormat();
format.setFontHeight(28);
FontData font = new FontData("Arial");
format.setLatinFont(font);
format.getFillFormat().setFillType(FillType.Solid);
format.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
Portion labelPortion = new Portion(label);
paragraph.getPortions().add(labelPortion);
Portion previousPrice = new Portion("$100");
previousPrice.getPortionFormat().setFontBold(NullableBool.True);
paragraph.getPortions().add(previousPrice);
Portion currentPrice = new Portion(" → $80");
paragraph.getPortions().add(currentPrice);
shape.getTextFrame().getParagraphs().add(paragraph);
}
IParagraphCollection paragraphs = shape.getTextFrame().getParagraphs();
// 取り消し線を適用し、最初の以前の価格から明示的に取り消し線を除去します。
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.None);
paragraphs.get_Item(1).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(2).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Double);
presentation.save("strikethrough.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果(上から順に):取り消し線なし、単一取り消し線、二重取り消し線。

テキストの回転設定
ITextFrameFormat.setTextVerticalType を使用して、シェイプ内のテキストの事前定義された向きを設定します。
次のコード例は、シェイプ内のテキスト向きを TextVerticalType.Vertical270 に設定し、テキストを 90 度反時計回り に回転させます:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

テキストフレームのカスタム回転設定
ITextFrameFormat.setRotationAngle を使用して、ITextFrame のカスタム回転角度を設定します。
以下のコード例は、シェイプ内でテキストフレームを時計回りに 3 度回転させます:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

段落の行間設定
Aspose.Slides は、段落間隔を制御するために IParagraphFormat.setSpaceAfter, IParagraphFormat.setSpaceBefore, および IParagraphFormat.setSpaceWithin を提供します。これらのプロパティは次のように使用します:
- 正の値を使用すると、行間を行の高さのパーセンテージで指定します。
- 負の値を使用すると、行間をポイントで指定します。
次の例は、最初の段落の行間を行の高さの 200%(二倍行間)に設定します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

改行の制御
段落の改行ルールは、狭いテキストブロックやラテン文字と東アジア文字が混在するプレゼンテーションで便利です。以下のメソッドは IParagraphFormat に属し、段落全体に適用されます:
- setLatinLineBreak はラテン文字の改行規則を制御します。混在テキストでは、これを変更することで隣接する東アジア文字や句読点の折り返し位置も変わることがあります。
- setEastAsianLineBreak は東アジア文字の改行規則を制御し、行頭・行末の文字に関する制限を含みます。
これらの規則は、テキストフレーム内で自動折り返しを有効にする ITextFrameFormat.setWrapText の代わりになるものではありません。折り返しが発生したときのレイアウトに影響を与えますが、改行文字を挿入するわけではありません。明示的な改行は、利用可能な幅に関係なく段落内で新しい行を強制します。
次の独立した例は、中国語とラテン文字を含む狭いテキストブロックを作成します。両方の改行オプションを明示的に設定し、“line_breaking.pptx” に保存します。いずれかの規則を試すには、もう一方の設定を固定したまま対応する値を変更します。例では 24 ポイントの Arial と SimSun を使用し、フレーム幅を 160 ポイント、テキストフレームの水平余白をゼロに設定しています。ITextFrameFormat.setAutofitType は TextAutofitType.None で呼び出され、テキストサイズとフレームサイズが固定されます。
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, 160, 300);
shape.getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setMarginLeft(0);
textFrame.getTextFrameFormat().setMarginRight(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.setText("中文排版测试,PowerPoint 中文演示。");
IParagraphFormat format = paragraph.getParagraphFormat();
format.setAlignment(TextAlignment.Left);
format.getDefaultPortionFormat().setFontHeight(24);
FontData latinFont = new FontData("Arial");
format.getDefaultPortionFormat().setLatinFont(latinFont);
FontData eastAsianFont = new FontData("SimSun");
format.getDefaultPortionFormat().setEastAsianFont(eastAsianFont);
format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
format.setLatinLineBreak(NullableBool.False);
format.setEastAsianLineBreak(NullableBool.True);
presentation.save("line_breaking.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
ハンギング句読点の制御
IParagraphFormat.setHangingPunctuation は、対象となる句読点が次の行に入る代わりにテキスト行の右端を超えて伸びることを可能にします。段落全体に適用され、ハンギングインデントとは異なります。
次の独立した例は、幅 100 ポイントのテキストフレームでハンギング句読点を有効にし、“hanging_punctuation.pptx” に保存します。24 ポイントの Arial と水平余白ゼロの設定で、最後の句点は “sentence” の後に残り、テキストの右端を超えて伸びます。比較のためにプロパティを NullableBool.False に設定すると、句点が別行に配置されます。折り返しは有効、オートフィットは無効にして利用可能幅を固定しています。
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, 100, 200);
shape.getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setMarginLeft(0);
textFrame.getTextFrameFormat().setMarginRight(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.setText("Simple text, next sentence.");
IParagraphFormat format = paragraph.getParagraphFormat();
format.setAlignment(TextAlignment.Left);
format.getDefaultPortionFormat().setFontHeight(24);
FontData latinFont = new FontData("Arial");
format.getDefaultPortionFormat().setLatinFont(latinFont);
format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
format.setHangingPunctuation(NullableBool.True);
presentation.save("hanging_punctuation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
すべての句読点がハンギングできるわけではありません。上記の フォントとレイアウト条件 もこの比較に適用され、フォント、利用可能幅、余白、オートフィット設定を変更すると見た目の差がなくなることがあります。
テキストフレームのオートフィットタイプの設定
ITextFrameFormat.setAutofitType は、テキストがコンテナの境界を超えたときの動作を決定します。テキストを縮小、はみ出し、またはシェイプを自動的にリサイズさせるかを制御できます。次の例は、シェイプをテキストに合わせてリサイズするよう設定し、結果を “autofit_type.pptx” に保存します。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
自動折り返し後の行数を数え、テキストまたはシェイプ幅の変更が結果に与える影響を確認するには、Count Rendered Lines を参照してください。行数だけではテキストがコンテナからはみ出しているかどうかは判断できません。
テキストフレームのアンカー設定
ITextFrameFormat.setAnchoringType は、シェイプ内でテキストを上下方向にどこに配置するか(上部、中央、下部など)を定義します。次の例は、最初のシェイプの下部にテキストをアンカーし、結果を “text_anchor.pptx” に保存します。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
テキストのタブ設定
IParagraphFormat.setDefaultTabSize と IParagraphFormat.getTabs を使用して段落のタブストップを設定します。次の例は、デフォルトタブ間隔を 100 ポイントに設定し、30 ポイントで左揃えタブストップを追加します。これらの設定は、タブ文字を含むテキストに影響します。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left);
presentation.save("paragraph_tabs.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

校正言語の設定
Aspose.Slides は IBasePortionFormat.setLanguageId を提供し、テキスト部分の校正言語を設定できます。校正言語は、PowerPoint でのスペルチェックや文法チェックに使用される言語を決定します。
次の例は、最初のスライドの最初のシェイプがテキストボックスで、少なくとも 1 つの段落がある “presentation.pptx” が必要です。最初の段落の内容を “1。” に置き換え、フォントを SimSun に設定し、簡体字中国語の校正言語 (zh-CN) を割り当てます。結果は “proofing_language.pptx” に保存されます:
import com.aspose.slides.*;
Presentation presentation = new Presentation("presentation.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
FontData font = new FontData("SimSun");
Portion textPortion = new Portion();
textPortion.getPortionFormat().setComplexScriptFont(font);
textPortion.getPortionFormat().setEastAsianFont(font);
textPortion.getPortionFormat().setLatinFont(font);
// 校正言語の ID を設定します。
textPortion.getPortionFormat().setLanguageId("zh-CN");
textPortion.setText("1。");
paragraph.getPortions().add(textPortion);
presentation.save("proofing_language.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
デフォルト言語の設定
LoadOptions.setDefaultTextLanguage を使用して、プレゼンテーションの読み込みまたは作成時に生成されるテキストのデフォルト言語を定義します。次の例は、デフォルトテキスト言語を米国英語に設定したプレゼンテーションを作成し、テキストボックスを追加して、最初のテキスト部分の言語として en-US を出力します。
import com.aspose.slides.*;
LoadOptions loadOptions = new LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
Presentation presentation = new Presentation(loadOptions);
try {
ISlide slide = presentation.getSlides().get_Item(0);
// テキスト付きの新しい長方形シェイプを追加します。
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// 最初の部分の言語を確認します。
IPortion portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
デフォルトテキストスタイルの設定
プレゼンテーションレベルでデフォルトのテキスト書式設定を適用するには、IPresentation.getDefaultTextStyle を使用します。
次の例は、新しいプレゼンテーションのトップレベル段落のデフォルトとして 14 ポイントの太字フォントを設定し、“default_text_style.pptx” に保存します。テキストは、より具体的な書式設定が上書きしない限り、これらのデフォルトを継承できます。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
// 上位レベルの段落書式を取得します。
IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat != null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
}
presentation.save("default_text_style.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
All-Caps 効果でテキストを抽出
PowerPoint では、All Caps フォント効果を適用すると、元が小文字で入力されていてもスライド上で大文字として表示されます。Aspose.Slides でそのようなテキスト部分を取得すると、ライブラリは入力されたままのテキストを返します。表示されたテキストと合わせるには、TextCapType を確認し、値が All の場合は返された文字列を大文字に変換します。
この例は、最初のスライドの最初のシェイプがテキストボックスである “sample2.pptx” が必要です。最初の段落の最初の部分に All Caps 効果が適用された “Hello, Aspose!” が含まれ、以下のように表示されます。

以下のコード例は、All Caps 効果が適用されたテキストを抽出する方法を示します:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample2.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IPortion textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println("Original text: " + textPortion.getText());
IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() == TextCapType.All) {
String text = textPortion.getText().toUpperCase();
System.out.println("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
FAQ
スライド上のテーブルのテキストを変更するにはどうすればよいですか?
スライド上のテーブルのテキストを変更するには、ITable を使用します。セルを反復処理し、各セルを ICell.getTextFrame で取得し、IParagraph.getParagraphFormat を介して段落書式を更新します。
PowerPoint のスライド上のテキストにグラデーションカラーを適用するにはどうすればよいですか?
テキストにグラデーションカラーを適用するには、IBasePortionFormat.getFillFormat を使用します。IFillFormat.setFillType を FillType.Gradient に設定し、グラデーションストップ、方向、透明度を構成します。
関連機能: スライド内テキストの書式設定.