How to Format Text in .NET
Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat class. A Portion is the smallest independent unit of text; it maps to a single formatting run within a paragraph. This guide shows how to apply bold, italic, font size, and color formatting to text in a presentation.
Step-by-Step Guide
Step 1: Install the Package
dotnet add package Aspose.Slides.FossStep 2: Add a Shape with a Text Frame
Before formatting text, a shape must contain a TextFrame. Use shape.AddTextFrame() to create one.
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);Step 3: Access the TextFrame
shape.AddTextFrame() returns the TextFrame object. You can also retrieve it later via shape.TextFrame.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameA TextFrame contains a list of Paragraph objects (tf.Paragraphs). Each Paragraph contains Portion objects (paragraph.Portions).
Step 4: Apply Bold and Italic Formatting
Use PortionFormat.FontBold and PortionFormat.FontItalic. These properties accept NullableBool.True, NullableBool.False, or NullableBool.NotDefined (inherit from master).
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);Step 5: Set Font Size and Color
Set PortionFormat.FontHeight for size (in points) and use FillFormat for color.
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) accepts values 0-255 for each channel.
Step 6: Multiple Portions in One Paragraph
A single paragraph can contain multiple portions with different formatting. Add a new Portion to a paragraph’s Portions collection:
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);Common Issues and Fixes
Text appears black even after setting color
Ensure FillFormat.FillType = FillType.Solid is set before assigning the color. Without setting the fill type, the color change may have no effect.
NullableBool.True vs true
PortionFormat.FontBold expects NullableBool.True, not the C# true. Assigning C# true will not compile because the types are incompatible.
Font does not appear in the saved file
The LatinFont property sets the Latin font family. If not set, the presentation theme font is used. Custom fonts must be embedded or available on the viewing machine.
Frequently Asked Questions
How do I change the font family?
Set PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData accepts the font family name as a string.
How do I set paragraph alignment?
Use ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;Supported values: Left, Center, Right, Justify.
How do I set line spacing?
Use ParagraphFormat.SpaceBefore (points before paragraph) or ParagraphFormat.SpaceAfter (points after paragraph):
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after