如何在 .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 frame一个 TextFrame 包含一个 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:一个段落中的多个部分
一个段落可以包含具有不同格式的多个部分。向段落的 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