Auto-Fit Text or Shape in PowerPoint in C#, VB.NET

With auto-fit feature, you can automatically reduce the font size of text to fit a shape, or you can shrink or enlarge shape to fit the exact size of your text. This article will show you how to auto-fit text or shape using Spire.Presentation with C#, VB.NET.

Step 1: Create an instance of Presentation class.

Presentation presentation = new Presentation();

Step 2: Insert a shape, and set the AutofitType property to Normal, which means the text automatically shrinks to fit shape.

IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

Step 3: Insert a shape, and set the AutofitType property to Shape, which means the shape size automatically decreases or increases to fit text.

IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
textShape2.TextFrame.Text = "Resize shape to fit text.";
textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

Step 4: Save the file.

presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output:

Auto-Fit Text or Shape in PowerPoint in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using System.Drawing;
namespace AutofitinPPT
{
    class Program
    {
        static void Main(string[] args)
        {

            Presentation presentation = new Presentation();

            IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
            textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
            textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

            IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
            textShape2.TextFrame.Text = "Resize shape to fit text.";
            textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

            presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing
Namespace AutofitinPPT
	Class Program
		Private Shared Sub Main(args As String())

			Dim presentation As New Presentation()

			Dim textShape1 As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 50, 150, 80))
			textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape."
			textShape1.TextFrame.AutofitType = TextAutofitType.Normal

			Dim textShape2 As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(350, 50, 150, 80))
			textShape2.TextFrame.Text = "Resize shape to fit text."
			textShape2.TextFrame.AutofitType = TextAutofitType.Shape

			presentation.SaveToFile("output.pptx", FileFormat.Pptx2013)
		End Sub
	End Class
End Namespace