.NET で PowerPoint のテキスト段落を管理する
概要
Aspose.Slides for .NET はテキストをテキスト フレーム、段落、部分の階層構造で表現します。
- ITextFrame はシェイプ内のテキスト コンテナを表し、段落コレクションへのアクセスを提供します。
- IParagraph はテキスト フレーム内の 1 つの段落を表し、部分と段落レベルの書式設定へのアクセスを提供します。
- IPortion は段落内のテキスト ランを表します。各部分は独自のテキストと文字レベルの書式設定を持つことができます。
したがって、段落は複数の部分を使用することで、フォント、色、サイズ、その他の書式が異なるテキストを含めることができます。
段落の作成と書式設定
複数の部分を持つ段落の作成
次の手順は、3 つの段落を持ち、各段落に 3 つの部分を含むテキスト フレームを作成します。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象スライドの参照にアクセスします。
- スライドに矩形の IAutoShape を追加します。
- シェイプの ITextFrame にアクセスします。
- 既定の段落を使用し、テキスト フレームにさらに 2 つの IParagraph オブジェクトを追加します。
- 各段落が 3 つの部分を含むように十分な数の IPortion オブジェクトを追加します。既定の段落にはすでに空の部分が 1 つ含まれています。
- 各部分のテキストを設定します。
- IPortion.PortionFormat を使用して文字レベルの書式設定を適用します。
- 変更したプレゼンテーションを保存します。
この C# の例は上記の手順を実装しています。
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 で定義します。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象スライドの参照にアクセスします。
- 選択したスライドに IAutoShape を追加します。
- シェイプの ITextFrame にアクセスします。
- テキスト フレームから既定の段落を削除します。
- 記号箇条書き用に Paragraph を作成します。
- IBulletFormat.Type を BulletType.Symbol に設定し、箇条書き文字を指定します。
- 段落テキスト、インデント、箇条書きの色、箇条書きの高さを設定します。
- 段落をテキスト フレームに追加します。
- 2 番目の段落を作成し、IBulletFormat.Type を BulletType.Numbered に設定します。
- 番号付き箇条書きスタイルを構成し、段落をテキスト フレームに追加します。
- プレゼンテーションを保存します。
この C# の例は記号箇条書きと番号付き箇条書きを作成します。
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);
画像箇条書きの使用
画像箇条書きでは、記号や番号の代わりにカスタム画像を使用できます。
- Presentation クラスのインスタンスを作成します。
- インデックスを使用して対象スライドの参照にアクセスします。
- IAutoShape を追加し、その ITextFrame にアクセスします。
- テキスト フレームから既定の段落を削除します。
- 箇条書き画像をロードし、IPPImage としてプレゼンテーションの画像コレクションに追加します。
- Paragraph を作成し、テキストを設定します。
- IBulletFormat.Type を BulletType.Picture に設定します。
- IBulletFormat.Picture で画像を割り当て、箇条書きの高さを設定します。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
この C# の例は画像箇条書きを作成します。
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 です。
- Presentation を作成し、スライドにアクセスします。
- IAutoShape を追加し、そのテキスト フレームから既定の段落をクリアします。
- 4 つの段落を作成し、箇条書き記号を構成します。
- それぞれの IParagraphFormat.Depth を
0、1、2、3に設定します。 - 段落をテキスト フレームに追加し、プレゼンテーションを保存します。
この C# の例は 4 階層の箇条書きリストを作成します。
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 を使用して、番号付き段落の開始番号を指定します。
- Presentation を作成し、スライドに IAutoShape を追加します。
- シェイプのテキスト フレームから既定の段落をクリアします。
- 3 つの番号付き段落を作成します。
- それぞれの段落に対し、IBulletFormat.NumberedBulletStartWith を
2、3、7に設定します。 - 段落をテキスト フレームに追加し、プレゼンテーションを保存します。
この C# の例は各段落にカスタム開始番号を割り当てます。
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);
段落レイアウトおよび終了プロパティの制御
先頭行インデントの設定
IParagraphFormat.Indent プロパティを使用して、段落の先頭行インデントを制御します。このプロパティは段落の左余白に対して最初の行だけを移動させます。正の値は先頭行を右へシフトし、残りの行は段落本文に揃ったままです。
テキスト全体を移動させたい場合は IParagraphFormat.MarginLeft を使用し、先頭行だけを移動させたい場合は IParagraphFormat.Indent を使用します。
以下の例は複数の段落を作成し、異なる IParagraphFormat.Indent 値を適用して、先頭行インデントが段落レイアウトに与える影響を示します。
- Presentation のインスタンスを作成します。
- 対象スライドにアクセスします。
- スライドに矩形の IAutoShape を追加します。
- シェイプの ITextFrame にアクセスし、既定の段落を削除します。
- 複数の段落を作成し、各段落に異なる Indent 値を設定します。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
このコードは段落インデントの設定方法を示しています。
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 値を組み合わせます。
この書式は、文献リスト、参照、用語集エントリなど、折り返し行が段落本文の下に揃える必要がある場合に便利です。
- Presentation のインスタンスを作成します。
- 対象スライドにアクセスします。
- スライドに矩形の IAutoShape を追加します。
- シェイプの ITextFrame にアクセスし、既定の段落を削除します。
- 各段落に対し正の MarginLeft 値を設定します。
- 負の Indent 値を設定してぶら下げインデント効果を作ります。
- 段落をテキスト フレームに追加します。
- 変更したプレゼンテーションを保存します。
このコードは段落にぶら下げインデントを設定する方法を示しています。
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 プロパティは段落終了マークの書式設定を制御します。以下の例は、2 番目の段落の終了マークにフォントサイズとラテン文字フォントを割り当てます。
- Presentation をロードし、スライドにアクセスします。
- IAutoShape を追加し、既定の段落をクリアします。
- 2 つの段落を作成し、テキスト部分を追加します。
- 2 番目の段落の終了マーク用に PortionFormat を作成します。
- IBasePortionFormat.FontHeight と IBasePortionFormat.LatinFont を設定します。
- フォーマットを IParagraph.EndParagraphPortionFormat に割り当て、プレゼンテーションを保存します。
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 つの項目であり、複数の描画行を占有することがあります。段落内の明示的な改行は新しい行を強制しますが、別の段落は作成しません。自動折り返しは利用可能な幅に基づいて行を生成し、テキストに明示的な改行文字を挿入しません。そのため、段落数や改行文字の数を数えても描画行数は得られません。
以下の例はテキスト シェイプを作成し、行数を取得し、シェイプを狭くしてからテキストを短い文字列に置き換える手順を示します。折り返しが有効でオートフィットが無効なため、シェイプの幅が折り返しを制御し、テキストやシェイプの自動縮小は行われません。シェイプのサイズはポイント単位です。最後に、別の段落を追加し、テキスト フレーム全体の行数を合算します。
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}");
このテキストとサイズでは、シェイプを狭くすると行数が増え、短い文字列に置き換えると行数が減ります。フォントの可用性や置換、フォントサイズ、余白、インデント、折り返し、オートフィット設定により正確なカウントは変わる可能性があります。テンプレートを確認する際は、対象環境で使用するフォントとレイアウト設定を使用してください。
行数だけではテキストがコンテナを超えているかは判断できません。利用可能な高さ、行の高さ、段落と行の間隔、オートフィットの動作も重要です。折り返しが無効な場合、単一行でも利用可能な幅を超えることがあります。
段落コンテンツのインポートとエクスポート
HTML テキストを段落にインポート
ParagraphCollection.AddFromHtml を使用すると、HTML マークアップをテキスト フレーム内の段落と部分に変換できます。
- Presentation クラスのインスタンスを作成します。
- スライドにアクセスし、IAutoShape を追加します。
- シェイプの ITextFrame にアクセスし、既定の段落をクリアします。
- ソース HTML ファイルを読み取ります。
- HTML 文字列を ParagraphCollection.AddFromHtml に渡します。
- 変更したプレゼンテーションを保存します。
この C# の例は HTML をテキスト フレームにインポートします。
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 としてエクスポートできます。
- Presentation のインスタンスを作成し、目的のプレゼンテーションをロードします。
- スライドにアクセスし、テキストを含む IAutoShape を見つけます。
- シェイプの ITextFrame にアクセスします。
- 開始段落インデックスとエクスポートする段落数を指定して、ParagraphCollection.ExportToHtml を呼び出します。
- 返された HTML 文字列をファイルに書き込みます。
この C# の例は最初のテキスト シェイプからすべての段落をエクスポートします。
using System;
using System.IO;
using System.Text;
using Aspose.Slides;
using var presentation = new Presentation("ExportingHTMLText.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape && textShape.TextFrame != null)
{
var paragraphs = textShape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, null);
using var writer = new StreamWriter("paragraphs.html", false, Encoding.UTF8);
writer.Write(html);
}
else
{
Console.WriteLine("The first shape is not a text shape.");
}
段落を画像としてレンダリング
IParagraph.GetImage は個々の段落を直接レンダリングし、IImage を返します。結果は IImage.Save でファイルまたはストリームに保存できます。シェイプ全体をレンダリングしたり、ビットマップを手動で切り取る必要はありません。
IParagraph.GetImage は、段落が親コレクションに見つからない、または有効な描画境界がない、あるいはレンダリングできない場合に null を返すことがあります。保存前に結果を確認し、使用後は返された画像を破棄してください。
デフォルト スケールで段落をレンダリング
プレゼンテーション ファイル sample.pptx に 1 枚のスライドがあり、最初のシェイプが 3 段落を含むテキスト ボックスであるとします。

以下の例は、通常のテキスト シェイプ内の 2 番目の段落をデフォルト スケールでレンダリングし、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](https://reference.aspose.com/slides/ja/net/aspose.slides/itextframeformat/wraptext/) を設定すると、テキスト フレームの端で行が折り返されなくなります。
特定の段落のスライド上の正確な境界を取得するにはどうすればよいですか?
[IParagraph.GetRect](https://reference.aspose.com/slides/ja/net/aspose.slides/iparagraph/getrect/) を使用して段落のバウンディング矩形を取得できます。[IPortion.GetRect](https://reference.aspose.com/slides/ja/net/aspose.slides/iportion/getrect/) は個々の部分の境界を提供します。
段落の行揃え(左、右、中央、両端揃え)はどこで制御しますか?
[IParagraphFormat.Alignment](https://reference.aspose.com/slides/ja/net/aspose.slides/iparagraphformat/alignment/) は段落レベルの設定であり、個々の部分の書式設定に関係なく段落全体に適用されます。
段落の一部に対して校正言語を設定できますか?
はい。[IBasePortionFormat.LanguageId](https://reference.aspose.com/slides/ja/net/aspose.slides/ibaseportionformat/languageid/) を個々の部分に設定すれば、1 つの段落内に複数の言語のテキストを含めることができます。