.NETでテキストをフォーマットする方法
Aspose.Slides FOSS for .NET は、PortionFormat クラスを通じて細かいテキスト書式設定を提供します。Portion はテキストの最小の独立単位であり、段落内の単一の書式ランに対応します。このガイドでは、プレゼンテーション内のテキストに太字、斜体、フォントサイズ、カラーの書式設定を適用する方法を示します。
ステップバイステップ ガイド
ステップ 1: パッケージをインストール
dotnet add package Aspose.Slides.Fossステップ 2: テキストフレーム付きのシェイプを追加
テキストをフォーマットする前に、シェイプはTextFrameを含んでいる必要があります。shape.AddTextFrame()を使用して作成してください。
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Default text: will be formatted");
prs.Save("output.pptx", SaveFormat.Pptx);ステップ 3: TextFrame にアクセス
shape.AddTextFrame() は TextFrame オブジェクトを返します。shape.TextFrame を使用して後で取得することもできます。
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameTextFrame は Paragraph オブジェクト(tf.Paragraphs)のリストを含んでいます。 各ParagraphはPortionオブジェクト(paragraph.Portions)を含んでいます。
ステップ 4: 太字と斜体の書式を適用する
PortionFormat.FontBold と PortionFormat.FontItalic を使用してください。これらのプロパティは NullableBool.True、NullableBool.False、または NullableBool.NotDefined を受け入れます(マスターから継承)。
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Bold and italic text");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FontItalic = NullableBool.True;
prs.Save("bold-italic.pptx", SaveFormat.Pptx);ステップ5: フォントサイズと色を設定
サイズ(ポイント)には PortionFormat.FontHeight を設定し、色には FillFormat を使用してください。
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Large corporate-blue heading");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32; // 32pt font
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);
prs.Save("colored-text.pptx", SaveFormat.Pptx);Color.FromArgb(alpha, red, green, blue) は各チャンネルに対して 0〜255 の値を受け入れます。
ステップ 6: 1つの段落に複数の部分
単一の段落には、異なる書式設定を持つ複数の部分を含めることができます。段落のPortionsコレクションに新しいPortionを追加します:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 600, 100);
var tf = shape.AddTextFrame(""); // start with empty frame
var paragraph = tf.Paragraphs[0];
// First portion: normal text
var portion1 = paragraph.Portions[0];
portion1.Text = "Normal text followed by ";
portion1.PortionFormat.FontHeight = 20;
// Second portion: bold red text
var portion2 = new Portion();
portion2.Text = "bold red text";
portion2.PortionFormat.FontHeight = 20;
portion2.PortionFormat.FontBold = NullableBool.True;
portion2.PortionFormat.FillFormat.FillType = FillType.Solid;
portion2.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);
paragraph.Portions.Add(portion2);
prs.Save("mixed-format.pptx", SaveFormat.Pptx);一般的な問題と対策
色を設定してもテキストが黒のままです
色を割り当てる前に FillFormat.FillType = FillType.Solid が設定されていることを確認してください。塗りつぶしタイプを設定しないと、色の変更が効果を持たない可能性があります。
NullableBool.True 対 true
PortionFormat.FontBold は NullableBool.True を期待しており、C# の true ではありません。C# の true を代入すると、型が互換性がないためコンパイルできません。
保存したファイルにフォントが表示されません
LatinFont プロパティはラテンフォントファミリーを設定します。設定されていない場合、プレゼンテーションテーマのフォントが使用されます。カスタムフォントは埋め込むか、閲覧マシンにインストールされている必要があります。
よくある質問
フォント ファミリーを変更するにはどうすればよいですか?
PortionFormat.LatinFont を設定:
fmt.LatinFont = new FontData("Arial");FontData はフォントファミリ名を文字列として受け取ります。
段落の配置を設定するにはどうすればよいですか?
ParagraphFormat.Alignment を使用してください:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;サポートされている値: Left, Center, Right, Justify。
行間を設定するにはどうすればよいですか?
ParagraphFormat.SpaceBefore(段落の前にポイント)またはParagraphFormat.SpaceAfter(段落の後にポイント)を使用してください:
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after