.NET でプレゼンテーションテキストをフォーマットする
概要
この記事では、Aspose.Slides for .NET を使用して PowerPoint と OpenDocument のプレゼンテーションでテキストをフォーマットする方法を示します。背景色、透明度、文字間隔、フォントプロパティ、回転、段落間隔、オートフィット動作、テキストのアンカリング、タブストップ、言語設定について説明します。
以下の例では、最初のスライドに単一のテキストボックスが含まれている「sample.pptx」ファイルを使用します。

文字列や正規表現の一致を検索してハイライトする方法については、テキストの検索と置換をご覧ください。
テキストの背景色の設定
段落のデフォルトのハイライト色を設定するには IParagraphFormat.DefaultPortionFormat を使用し、個々のテキスト部分の色を設定するには IBasePortionFormat.HighlightColor を使用します。
以下のコード例は 段落全体 の背景色を設定する方法を示します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// 段落全体のハイライトカラーを設定します。
paragraph.ParagraphFormat.DefaultPortionFormat.HighlightColor.Color = Color.LightGray;
presentation.Save("gray_paragraph.pptx", SaveFormat.Pptx);
}
結果:

以下のコード例は 太字フォントのテキスト部分 の背景色を設定する方法を示します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// テキスト部分のハイライトカラーを設定します。
portion.PortionFormat.HighlightColor.Color = Color.LightGray;
}
}
presentation.Save("gray_text_portions.pptx", SaveFormat.Pptx);
}
結果:

テキスト段落の配置
テキストフレーム内の段落の配置を設定するには IParagraphFormat.Alignment を使用します。値は中央揃え、左揃え、右揃え、両端揃えなどが指定できます。
以下のコード例は段落を 中央 に配置する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// 段落の配置を中央に設定します。
paragraph.ParagraphFormat.Alignment = TextAlignment.Center;
presentation.Save("aligned_paragraph.pptx", SaveFormat.Pptx);
}
結果:

テキストの透明度の設定
テキストの透明度は IBasePortionFormat.FillFormat に割り当てる色のアルファ成分で制御します。以下の例では alpha = 50 は 0〜255 のスケールの ARGB アルファチャネル値であり、透明度パーセンテージではありません。
以下のコード例は 段落全体 に透明度を適用する方法を示します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// テキストの塗りつぶし色を透明に設定します。
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
presentation.Save("transparent_paragraph.pptx", SaveFormat.Pptx);
}
結果:

以下のコード例は 太字フォントのテキスト部分 に透明度を適用する方法を示します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// テキスト部分の透明度を設定します。
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
}
}
presentation.Save("transparent_text_portions.pptx", SaveFormat.Pptx);
}
結果:

テキストの文字間隔の設定
テキストボックス内の文字間隔を拡大または縮小するには IBasePortionFormat.Spacing を使用します。
以下の C# コードは 段落全体 の文字間隔を拡大する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// 注: 文字間隔を圧縮するには負の値を使用します。
paragraph.ParagraphFormat.DefaultPortionFormat.Spacing = 3; // 文字間隔を拡大します。
presentation.Save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
}
結果:

以下のコード例は 太字フォントのテキスト部分 の文字間隔を拡大する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// 注: 文字間隔を圧縮するには負の値を使用します。
portion.PortionFormat.Spacing = 3; // 文字間隔を拡大します。
}
}
presentation.Save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
}
結果:

特定フォントのカーニングを無効にする
場合によっては、Aspose.Slides でレンダリングされたテキストが PowerPoint の表示よりもわずかに詰まって見えることがあります。これは PowerPoint が特定フォントのカーニング情報を無視するために起こります。このような場合、該当フォントを使用するテキスト部分のカーニングを無効にして、PowerPoint に近い表示にできます。 IBasePortionFormat.KerningMinimalSize に実際のフォントサイズより大幅に大きい値を設定します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var targetFont = "Roboto";
foreach (var paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (var portion in paragraph.Portions)
{
if ((portion.PortionFormat.LatinFont != null &&
portion.PortionFormat.LatinFont.FontName == targetFont) ||
(portion.PortionFormat.EastAsianFont != null &&
portion.PortionFormat.EastAsianFont.FontName == targetFont) ||
(portion.PortionFormat.ComplexScriptFont != null &&
portion.PortionFormat.ComplexScriptFont.FontName == targetFont))
{
portion.PortionFormat.KerningMinimalSize = 100;
}
}
}
presentation.Save("output.pptx", SaveFormat.Pptx);
}
この設定により該当テキスト部分にカーニングが適用されず、PowerPoint 特有の挙動による差異を抑えることができます。
テキストのフォントプロパティの管理
フォントプロパティは IParagraphFormat.DefaultPortionFormat を使用して段落レベルで、または個々の部分に対しては IPortionFormat を使用して設定できます。
以下のコードは段落全体のフォントとテキストスタイルを設定します。フォントサイズ、太字、斜体、点線下線、Times New Roman フォントがすべての部分に適用されます。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// 段落のフォントプロパティを設定します。
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 12;
paragraph.ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontItalic = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontUnderline = TextUnderlineType.Dotted;
paragraph.ParagraphFormat.DefaultPortionFormat.LatinFont = new FontData("Times New Roman");
presentation.Save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
}
結果:

以下のコード例は 太字フォントのテキスト部分 に同様のプロパティを適用します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// テキスト部分のフォントプロパティを設定します。
portion.PortionFormat.FontHeight = 13;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontUnderline = TextUnderlineType.Dotted;
portion.PortionFormat.LatinFont = new FontData("Times New Roman");
}
}
presentation.Save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
}
結果:

テキストの回転の設定
テキストの向きを事前定義されたものに設定するには ITextFrameFormat.TextVerticalType を使用します。
以下のコード例はシェイプ内のテキスト向きを Vertical270 に設定し、テキストを 反時計回りに 90 度 回転させます。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.TextVerticalType = TextVerticalType.Vertical270;
presentation.Save("text_rotation.pptx", SaveFormat.Pptx);
}
結果:

テキストフレームのカスタム回転の設定
ITextFrameFormat.RotationAngle を使用して、ITextFrame のカスタム回転角度を設定できます。
以下のコード例はシェイプ内のテキストフレームを時計回りに 3 度回転させます。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.RotationAngle = 3;
presentation.Save("custom_text_rotation.pptx", SaveFormat.Pptx);
}
結果:

段落の行間の設定
Aspose.Slides は IParagraphFormat.SpaceAfter、IParagraphFormat.SpaceBefore、IParagraphFormat.SpaceWithin を提供し、段落間隔を制御します。使用方法は次のとおりです。
- 正の値を使用すると、行間が行の高さのパーセンテージとして指定されます。
- 負の値を使用すると、行間がポイントで指定されます。
以下のコード例は段落内の行間を指定する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.SpaceWithin = 200;
presentation.Save("line_spacing.pptx", SaveFormat.Pptx);
}
結果:

テキストフレームのオートフィットタイプの設定
ITextFrameFormat.AutofitType は、テキストがコンテナの境界を超えたときの動作を決定します。テキストを縮小するか、はみ出すか、シェイプを自動的にリサイズするかを制御できます。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
presentation.Save("autofit_type.pptx", SaveFormat.Pptx);
}
自動折り返し後の行数をカウントし、テキストやシェイプ幅の変化を見るには、レンダリングされた行数のカウントをご参照ください。行数だけではテキストがコンテナからはみ出しているかは判断できません。
テキストフレームのアンカーの設定
ITextFrameFormat.AnchoringType は、シェイプ内でテキストが垂直方向に配置される位置(上部、中央、下部など)を定義します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Bottom;
presentation.Save("text_anchor.pptx", SaveFormat.Pptx);
}
テキストのタブ設定
段落のタブ位置は IParagraphFormat.DefaultTabSize と IParagraphFormat.Tabs を使用して構成できます。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultTabSize = 100;
paragraph.ParagraphFormat.Tabs.Add(30, TabAlignment.Left);
presentation.Save("paragraph_tabs.pptx", SaveFormat.Pptx);
}
結果:

校正言語の設定
Aspose.Slides は IBasePortionFormat.LanguageId を提供し、テキスト部分の校正言語を設定できます。校正言語は PowerPoint のスペルチェックおよび文法チェックに使用される言語を決定します。
以下のコード例はテキスト部分の校正言語を設定する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.Portions.Clear();
var font = new FontData("SimSun");
var textPortion = new Portion();
textPortion.PortionFormat.ComplexScriptFont = font;
textPortion.PortionFormat.EastAsianFont = font;
textPortion.PortionFormat.LatinFont = font;
// 校正言語の ID を設定します。
textPortion.PortionFormat.LanguageId = "zh-CN";
textPortion.Text = "1。";
paragraph.Portions.Add(textPortion);
presentation.Save("proofing_language.pptx", SaveFormat.Pptx);
}
デフォルト言語の設定
LoadOptions.DefaultTextLanguage を使用して、プレゼンテーションの読み込みまたは作成時に作成されるテキストのデフォルト言語を定義します。
using Aspose.Slides;
var loadOptions = new LoadOptions();
loadOptions.DefaultTextLanguage = "en-US";
using (var presentation = new Presentation(loadOptions))
{
var slide = presentation.Slides[0];
// テキスト付きの新しい矩形シェイプを追加します。
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.TextFrame.Text = "Sample text";
// 最初のポーションの言語を確認します。
var portion = shape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine(portion.PortionFormat.LanguageId);
}
デフォルトテキストスタイルの設定
プレゼンテーションレベルでデフォルトのテキスト書式を適用するには IPresentation.DefaultTextStyle を使用します。
以下のコード例は新しいプレゼンテーションのすべてのスライドで、フォントサイズ 14 pt の太字フォントをデフォルトテキストスタイルとして設定する方法を示します。
using Aspose.Slides;
using Aspose.Slides.Export;
using (var presentation = new Presentation())
{
// 上位レベルの段落書式を取得します。
var paragraphFormat = presentation.DefaultTextStyle.GetLevel(0);
if (paragraphFormat != null)
{
paragraphFormat.DefaultPortionFormat.FontHeight = 14;
paragraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
}
presentation.Save("default_text_style.pptx", SaveFormat.Pptx);
}
All-Caps 効果付きテキストの抽出
PowerPoint では All Caps フォント効果を適用すると、スライド上では大文字で表示されますが、元の入力は小文字のままです。Aspose.Slides でそのテキスト部分を取得すると、入力されたままの文字列が返されます。表示されたテキストと一致させるには、TextCapType を確認し、値が All の場合は返された文字列を大文字に変換します。
サンプル2.pptx ファイルの最初のスライドにあるテキストボックスを例にします。

以下のコード例は All Caps 効果が適用されたテキストを抽出する方法を示します。
using Aspose.Slides;
using (var presentation = new Presentation("sample2.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var textPortion = autoShape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine($"Original text: {textPortion.Text}");
var textFormat = textPortion.PortionFormat.GetEffective();
if (textFormat.TextCapType == TextCapType.All)
{
var text = textPortion.Text.ToUpper();
Console.WriteLine($"All-Caps effect: {text}");
}
}
出力:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
FAQ
スライド上のテーブルのテキストを変更するにはどうすればよいですか?
テーブル内のテキストを変更するには ITable を使用します。セルを反復処理し、各セルを ICell.TextFrame と IParagraph.ParagraphFormat で更新します。
PowerPoint スライドのテキストにグラデーションカラーを適用するにはどうすればよいですか?
グラデーションカラーを適用するには IBasePortionFormat.FillFormat を使用します。 IFillFormat.FillType を FillType.Gradient に設定し、グラデーションのストップ、方向、透明度を構成します。