.NET で PowerPoint のテキスト段落を管理
概要
Aspose.Slides for .NET は、テキストをテキストフレーム、段落、ポーションの階層として表します。
- ITextFrame は、シェイプ内のテキスト コンテナを表し、その段落コレクションへのアクセスを提供します。
- IParagraph は、テキストフレーム内の 1 つの段落を表し、ポーションと段落レベルの書式設定へのアクセスを提供します。
- IPortion は、段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。
したがって、段落は複数のポーションを使用することで、フォント、色、サイズ、その他の書式設定が異なるテキストを含めることができます。
段落の作成と書式設定
シェイプのテキストフレーム内に段落を作成し、ポーションを使用して個々のテキスト ランに書式を設定します。このセクションでは、各段落内で複数のポーションを組み合わせ、異なる色、フォント サイズ、スタイルを適用する方法を示します。
複数ポーションで段落を作成
以下の C# サンプルは、IParagraph.Portions コレクションに 3 つのテキスト ランを含む 3 つの段落を作成します。既定の段落と空のポーションを再利用し、残りの段落とポーションを追加します。書式設定は IPortion.PortionFormat を通じて適用されます。各段落の最初のポーションは赤、太字、15 ポイント、2 番目は青、斜体、18 ポイント、3 番目は既定の書式を保持します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
箇条書きと番号付きリストの作成
シンボル、番号、またはカスタム画像を使用して段落テキストをリストに整理します。このセクションの例では、箇条書きの書式設定、多層リスト、カスタム開始番号を扱います。
箇条書きまたは番号付きリストの作成
箇条書きと番号付けは、関連項目を見やすくします。Aspose.Slides では、リスト設定は IBulletFormat で定義されます。
この C# サンプルは、同じテキストフレームにシンボル箇条書きと番号付き項目を追加します。IBulletFormat.Type で箇条書きタイプを選択し、IBulletFormat.Char でシンボルを指定、IBulletFormat.NumberedBulletStyle で番号スタイルを選択します。両方の項目は黒色の箇条書きを使用し、高さは 100% で段落インデントは 25 ポイントです。
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
画像箇条書きの使用
画像箇条書きを使用すると、シンボルや番号の代わりにカスタム画像を使用できます。
この C# サンプルは、プレゼンテーションの画像コレクションに画像を IPPImage として追加し、段落箇条書きとして使用します。IBulletFormat.Type を画像箇条書きに設定し、画像は IBulletFormat.Picture で割り当てます。IBulletFormat.Height は 100% に設定し、段落テキストに対する相対サイズにします。
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
多層リストの作成
IParagraphFormat.Depth を設定して、リストの異なるレベルに段落を配置します。最上位レベルの深さは 0 です。
この C# サンプルは、4 つの段落に対して IParagraphFormat.Depth を 0 から 3 まで割り当て、1 つのテキストフレーム内で階層を作成します。各段落はシンボル箇条書きを使用し、IBulletFormat.Char が丸い箇条書きとハイフンを交互に使用して隣接レベルを区別します。
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
番号付きリスト項目の開始番号をカスタム値に設定
IBulletFormat.NumberedBulletStartWith を使用して、番号付き段落に表示される開始番号を設定します。
この C# サンプルは、3 つの段落で番号付けを使用するように IBulletFormat.Type を設定し、IBulletFormat.NumberedBulletStartWith に 2、3、7 の値を割り当てます。開始値は各段落ごとに個別に設定され、表示番号が飛ぶようになります。
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
段落のレイアウトと終端プロパティの制御
段落の余白とインデントを調整して、1 行目と折り返し行の位置を制御します。表示テキストのポーションとは別に段落終端マークを書式設定することも可能です。
行頭インデントの設定
IParagraphFormat.Indent プロパティを使用して段落の行頭インデントを制御します。このプロパティは段落の左余白に対して最初の行だけを移動させます。正の値は最初の行を右にシフトし、残りの行は段落本体に揃ったままです。
全体の段落を移動したい場合は IParagraphFormat.MarginLeft を使用し、最初の行だけを移動したい場合は IParagraphFormat.Indent を使用します。
以下の例は、3 つの段落に同じ IParagraphFormat.MarginLeft 20 ポイントを設定し、IParagraphFormat.Indent にそれぞれ 0、20、40 ポイントの値を付与します。最初の行は段階的に大きなオフセットで開始し、折り返し行は同じ左余白を保持します。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
結果:

ぶら下げインデントの設定
ぶら下げインデントは、最初の行が残りの行より左側で開始する段落レイアウトです。Aspose.Slides では IParagraphFormat.Indent プロパティでこの効果を作ります。Indent を負の値に設定すると、段落本体に対して最初の行が左に移動します。
実際には、IParagraphFormat.MarginLeft が段落本体の左位置を定義し、IParagraphFormat.Indent がその余白に対する最初の行の位置を定義します。ぶら下げインデントを作成するには、正の MarginLeft 値と負の Indent 値を設定します。
この書式は、書誌、参照、用語集エントリなど、折り返し行が段落本体の下に揃う必要がある段落で便利です。
以下の C# サンプルは、IParagraphFormat.MarginLeft に 40 と 60 ポイント、IParagraphFormat.Indent に -20 と -30 ポイントの組み合わせを適用します。各段落で最初の行は折り返し行の左側で開始し、2 番目の段落はより深いぶら下げインデントになります。
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
結果:

段落終端ランのプロパティ設定
IParagraph.EndParagraphPortionFormat プロパティは段落終端マークの書式を制御します。以下の例は、PortionFormat を作成し、IBasePortionFormat.FontHeight を 48 ポイント、IBasePortionFormat.LatinFont を Times New Roman に設定します。この書式を 2 番目の段落の終端マークに割り当て、表示テキストのポーションは変更しません。
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
描画された行数のカウント
行末の自動折り返しや句読点に影響する段落ルールについては、Control Line Breaking と Control Hanging Punctuation を参照してください。
IParagraph.GetLinesCount を使用すると、テキスト配置後の段落が占める行数(自動折り返しを含む)を取得できます。プレゼンテーションテンプレートでテキスト長やレイアウトを確認する際に便利です。
段落は ITextFrame.Paragraphs の 1 項目であり、複数の描画行を占めることがあります。段落内の明示的な改行は新しい段落を作成せずに新しい行を強制します。自動折り返しは、テキストに明示的な改行文字を挿入せず、利用可能な幅に基づいて行を生成します。そのため、段落数や改行文字のカウントだけでは描画行数は得られません。
以下の例はテキストシェイプを作成し、行数をカウントしてシェイプを狭め、さらにテキストを短い文字列に置き換えます。例では ITextFrameFormat.WrapText を有効にし、ITextFrameFormat.AutofitType を無効にして、シェイプ幅が折り返しを制御し、テキストやシェイプの自動縮小は行わないようにしています。各測定は IParagraph.GetLinesCount を使用します。シェイプの寸法はポイント単位です。最後に別の段落を追加し、テキストフレーム全体の行数を合計します。
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
このテキストと寸法でシェイプを狭めると行数が増加し、短い文字列に置き換えると行数が減少します。正確なカウントはフォントの可用性・代替、フォントサイズ、余白、インデント、折り返し、 Autofit 設定により変動します。テンプレートをチェックする際は、対象環境で使用するフォントとレイアウト設定を使用してください。
行数だけではテキストがコンテナを超えているかは判断できません。利用可能な高さ、行の高さ、段落・行間隔、 Autofit 動作も重要です。折り返しが無効の場合、1 行だけでも利用可能な幅を超えることがあります。
段落コンテンツのインポートとエクスポート
HTML を使用してテキストをプレゼンテーションに取り込んだり、既存の段落を外部で利用できるようにエクスポートしたりします。このセクションでは、テーブルセル内の段落を含む個々の段落を画像としてレンダリングする方法も示します。
HTMLテキストを段落にインポート
ParagraphCollection.AddFromHtml を使用して、HTML マークアップをテキストフレーム内の段落とポーションに変換します。
この C# サンプルはファイルから HTML を読み込み、マークアップを ParagraphCollection.AddFromHtml に渡します。テキストフレームの既定段落をクリアして、フレームにインポートされたコンテンツのみが含まれるようにします。矩形シェイプはスライドの各側面に 10 ポイントの余白を残します。
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
段落テキストをHTMLにエクスポート
ParagraphCollection.ExportToHtml を使用して、選択された段落範囲を HTML としてエクスポートします。プレゼンテーションのテキストを Web サイトで再利用する場合、フォント、サイズ、テキストボックスのスタイルを保持せずにローカルの強調表示を保持したいことがあります。TextToHtmlConversionOptions.TextInheritanceLimit を設定して、エクスポーターが書式継承をどこまでたどるかを制御します。
text-inheritance.pptx サンプルプレゼンテーションをダウンロードし、アプリケーションの作業ディレクトリに配置します。最初のスライドにはテキスト「Inherited text; explicit red 18pt」を含むテキストシェイプが 1 つあります。入力は意図的に複数の書式レベルを使用しています。
- プレゼンテーションの既定テキストスタイルは Calibri と 28 pt を供給します。
- テキストフレームの第一レベルテキストスタイルは青色テキストと斜体を供給します。
- 段落は太字テキストを明示的に設定します。
- 第二ポーション「explicit red 18pt」は赤色テキストと 18 pt のサイズを明示的に設定します。
以下の完全なサンプルは同じ段落を 3 回エクスポートします。TextToHtmlConversionOptions をエクスポート メソッドに渡し、各結果を別々の UTF-8 HTML ファイルとして書き出します。
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
生成された HTML には、最初のポーション「Inherited text; 」に対する以下の CSS が含まれます。表は関連する宣言を一覧表示し、他の生成プロパティは簡潔さのために省略しています。
| 出力 | 最初のポーションの関連 CSS | 表示 |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
青色、太字、斜体のテキスト、28 pt、Calibri フォント(利用可能な場合) |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
青色、太字、斜体のテキスト;周囲のページまたはブラウザーがフォントファミリとサイズを提供 |
ParagraphOnly.html |
font-weight:bold |
太字のテキスト;周囲のページまたはブラウザーがフォントファミリ、サイズ、色、斜体スタイルを提供 |
第二ポーションはすべての出力で color:#ff0000、font-size:18pt、font-weight:bold を保持します。All と TextBox では斜体も保持されますが、ParagraphOnly では保持されません。継承を制限しても、段落やポーションに明示的に割り当てられた書式は削除されません。
同じブラウザーでファイルを開いて比較してください。制限モードの 2 種類では、省略された CSS プロパティが宛先ページのスタイルまたはブラウザーのデフォルトで解決されるため、最終的な表示は HTML が使用される場所に依存します。
エクスポートされた段落をUTF-8 HTMLとして保存
ITextToHtmlConversionOptions.EncodingName は、ParagraphCollection.ExportToHtml が返す HTML 文字列に宣言される charset を設定します。その文字列を保存する際に使用するエンコーディングが File.WriteAllText に渡されるものと一致すれば、実際のバイト列も一致します。両方で同じエンコーディングを使用すると、ブラウザーが多言語テキストを正しくデコードできます。
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
shape.TextFrame.Text = "Café | Ωω | 日本語 | 😀";
var options = new TextToHtmlConversionOptions
{
EncodingName = "utf-8",
AddClipboardFragmentHeader = false
};
var paragraphs = shape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText("paragraphs.html", html, new UTF8Encoding(false));
保存されたファイルは charset=utf-8 を宣言し、UTF-8 バイトを含みます。paragraphs.html をブラウザーで開くとテキスト “Café | Ωω | 日本語 | 😀” が保持されます。
HTMLクリップボードフラグメントの生成
AddClipboardFragmentHeader を false に設定すると通常の HTML、true に設定するとクリップボードヘッダーが含まれます。どちらの呼び出しも文字列を返します。ヘッダーは、周囲の HTML の UTF-8 バイトオフセット(StartHTML と EndHTML)と貼り付けるコンテンツのオフセット(StartFragment と EndFragment)を提供します。
この例は、2 つの書式付き段落を通常の HTML(html)と HTML クリップボードフラグメント(clipboardHtml)としてエクスポートします。フラグメントをシステムクリップボードに書き込むのは別途アプリケーションで行う必要があります。RAW HTML クリップボードバイトを受け取るクリップボード API は、BOM のない UTF-8 を要求します。
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
段落を画像としてレンダリング
IParagraph.GetImage は個々の段落を直接レンダリングし、IImage を返します。結果は IImage.Save でファイルまたはストリームに保存できます。包含シェイプをレンダリングしたり、ビットマップを手動で切り取る必要はありません。
IParagraph.GetImage は、段落が親コレクションに見つからない、または有効な描画境界がない、またはレンダリングできない場合に null を返すことがあります。保存する前に結果を確認し、使用後は返された画像を必ず破棄してください。
デフォルトスケールで段落をレンダリング
この例は、1 枚のスライドを持つプレゼンテーションを使用し、最初のシェイプが 3 つの段落を含むテキストボックスです。

以下の例はインデックス 1 の位置にある 2 番目の段落を選択し、スケールパラメータなしで IParagraph.GetImage を呼び出します。シェイプが必要な段落を含んでいることと、レンダリングが画像を返すことを確認した上で、IImage.Save を使用して PNG 形式で保存します。using 宣言により、使用後に画像が破棄されます。
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
結果:

テーブルセル内の段落をスケーリングしてレンダリング
float scaleX と float scaleY パラメータを受け取るオーバーロードの IParagraph.GetImage を使用して、水平および垂直のスケール係数を設定します。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅と高さの 2 倍でレンダリングし、PNG 画像として保存します。
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
スケール係数 1 はその軸をデフォルトのピクセルサイズに保ちます。たとえば、両方の係数を 2 にすると、幅と高さが約 2 倍になり、ピクセル数は 4 倍になります。大きな係数はズームや高解像度出力でテキストをより鮮明にしますが、メモリ使用量とファイルサイズも増加します。係数が 1 未満の場合は詳細が減少した小さな画像が生成されます。段落のアスペクト比を保つには等しい係数を使用し、異なる水平・垂直係数は出力を個別に伸縮させます。
シェイプ全体を IShape.GetImage でレンダリングすることは、シェイプの塗りつぶし、枠線、その他の視覚的コンテキストを含める必要がある場合に有用です。段落のみの画像の場合は、IParagraph.GetImage を使用してください。
FAQ
テキストフレーム内で行の折り返しを完全に無効にできますか?
はい。ITextFrameFormat.WrapText を設定して折り返しを無効にすれば、行はテキストフレームの端で折れません。
特定の段落のスライド上での正確な境界を取得するにはどうすればよいですか?
IParagraph.GetRect を使用して段落の境界矩形を取得します。IPortion.GetRect は個々のポーションの境界を提供します。
段落の配置(左寄せ、右寄せ、中央揃え、両端揃え)はどこで制御されますか?
IParagraphFormat.Alignment は段落レベルの設定であり、個々のポーションの書式設定に関係なく段落全体に適用されます。
各行内で異なるフォントサイズのポーションを垂直に揃える方法については、Align Fonts Within a Line を参照してください。
段落の一部に校正言語を設定できますか?
はい。個々のポーションに対して IBasePortionFormat.LanguageId を設定すれば、1 つの段落で複数の言語のテキストを扱うことができます。