.NET で PowerPoint テキスト段落を管理する

概要

Aspose.Slides for .NET はテキストをテキスト フレーム、段落、ポーションの階層として表現します:

  • ITextFrame はシェイプ内のテキスト コンテナを表し、段落コレクションへのアクセスを提供します。
  • IParagraph はテキスト フレーム内の段落を表し、ポーションと段落レベルの書式設定へのアクセスを提供します。
  • IPortion は段落内のテキスト ランを表します。各ポーションは独自のテキストと文字レベルの書式設定を持つことができます。

このため、段落は複数のポーションを使用して、フォント、色、サイズ、その他の書式が異なるテキストを含めることができます。

段落の作成と書式設定

�数ポーションで段落を作成する

以下の手順で、各段落に 3 つのポーションが含まれるテキスト フレームを作成します:

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象スライドの参照にアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスします。
  5. デフォルトの段落を使用し、テキスト フレームにさらに 2 つの IParagraph オブジェクトを追加します。
  6. 各段落が 3 つのポーションを含むように十分な数の IPortion オブジェクトを追加します。デフォルトの段落にはすでに空のポーションが 1 つ含まれています。
  7. 各ポーションのテキストを設定します。
  8. IPortion.PortionFormat を使用して文字レベルの書式設定を適用します。
  9. 変更されたプレゼンテーションを保存します。

この 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 を使用して定義されます。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象スライドの参照にアクセスします。
  3. 選択したスライドに IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスします。
  5. テキスト フレームからデフォルトの段落を削除します。
  6. 記号箇条書き用に Paragraph を作成します。
  7. IBulletFormat.Type を BulletType.Symbol に設定し、箇条書き文字を指定します。
  8. 段落テキスト、インデント、箇条書きの色、箇条書きの高さを設定します。
  9. 段落をテキスト フレームに追加します。
  10. 2 番目の段落を作成し、IBulletFormat.Type を BulletType.Numbered に設定します。
  11. 番号付き箇条書きスタイルを構成し、段落をテキスト フレームに追加します。
  12. プレゼンテーションを保存します。

この 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);

画像箇条書きの使用

画像箇条書きでは、記号や番号の代わりにカスタム画像を使用できます。

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用して対象スライドの参照にアクセスします。
  3. IAutoShape を追加し、その ITextFrame にアクセスします。
  4. テキスト フレームからデフォルトの段落を削除します。
  5. 箇条書き画像をロードし、プレゼンテーションの画像コレクションに IPPImage として追加します。
  6. Paragraph を作成し、テキストを設定します。
  7. IBulletFormat.Type を BulletType.Picture に設定します。
  8. IBulletFormat.Picture で画像を割り当て、箇条書きの高さを設定します。
  9. 段落をテキスト フレームに追加します。
  10. 変更されたプレゼンテーションを保存します。

この 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 です。

  1. Presentation を作成し、スライドにアクセスします。
  2. IAutoShape を追加し、テキスト フレームからデフォルトの段落をクリアします。
  3. 4 つの段落を作成し、箇条書き記号を構成します。
  4. それらの IParagraphFormat.Depth 値を 0、1、2、3 に設定します。
  5. 段落をテキスト フレームに追加し、プレゼンテーションを保存します。

この 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 を使用して、番号付き段落の開始番号を設定できます。

  1. Presentation を作成し、スライドに IAutoShape を追加します。
  2. シェイプのテキスト フレームからデフォルトの段落をクリアします。
  3. 3 つの番号付き段落を作成します。
  4. 各段落に対して IBulletFormat.NumberedBulletStartWith をそれぞれ 2、3、7 に設定します。
  5. 段落をテキスト フレームに追加し、プレゼンテーションを保存します。

この 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 値を適用して、先頭行インデントが段落レイアウトに与える影響を示します。

  1. Presentation クラスのインスタンスを作成します。
  2. 対象スライドにアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスし、デフォルトの段落を削除します。
  5. 複数の段落を作成し、各段落に異なる Indent 値を設定します。
  6. 段落をテキスト フレームに追加します。
  7. 変更されたプレゼンテーションを保存します。

このコードは段落インデントの設定方法を示します:

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 値を設定します。

この書式設定は、参考文献、引用、用語集エントリなど、折り返し行が段落本文の下に揃える必要がある場合に便利です。

  1. Presentation クラスのインスタンスを作成します。
  2. 対象スライドにアクセスします。
  3. スライドに矩形の IAutoShape を追加します。
  4. シェイプの ITextFrame にアクセスし、デフォルトの段落を削除します。
  5. 各段落に対して正の MarginLeft 値を設定します。
  6. ぶら下げインデント効果を作成するために負の Indent 値を設定します。
  7. 段落をテキスト フレームに追加します。
  8. 変更されたプレゼンテーションを保存します。

このコードは段落のぶら下げインデント設定方法を示します:

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 番目の段落の終了マークにフォント サイズとラテン文字フォントを割り当てます:

  1. Presentation を読み込み、スライドにアクセスします。
  2. IAutoShape を追加し、デフォルトの段落をクリアします。
  3. 2 つの段落を作成し、テキスト ポーションを追加します。
  4. 2 番目の段落の終了マーク用に PortionFormat を作成します。
  5. IBasePortionFormat.FontHeight と IBasePortionFormat.LatinFont を設定します。
  6. フォーマットを 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 マークアップをテキスト フレーム内の段落とポーションに変換できます。

  1. Presentation クラスのインスタンスを作成します。
  2. スライドにアクセスし、IAutoShape を追加します。
  3. シェイプの ITextFrame にアクセスし、デフォルトの段落をクリアします。
  4. ソース HTML ファイルを読み取ります。
  5. HTML 文字列を ParagraphCollection.AddFromHtml に渡します。
  6. 変更されたプレゼンテーションを保存します。

この 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 としてエクスポートできます。

  1. Presentation のインスタンスを作成し、目的のプレゼンテーションを読み込みます。
  2. スライドにアクセスし、テキストを含む IAutoShape を見つけます。
  3. シェイプの ITextFrame にアクセスします。
  4. 開始段落インデックスとエクスポートする段落数を指定して、ParagraphCollection.ExportToHtml を呼び出します。
  5. 返された 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 段落を含むテキスト ボックスであると仮定します。

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.");
}

結果:

段落画像

テーブルセル内の段落をスケーリングしてレンダリングする

IParagraph.GetImage のオーバーロードで float scaleX と float scaleY パラメータを受け取り、横方向と縦方向のスケール係数を設定できます。以下の例はテーブルを作成し、最初のセル内の段落をデフォルト幅と高さの 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 段落内で複数の言語を含めることができます。