How to set margins for text inside shapes in C#

Margin is the space between the text and the edge of paper or shape. MS office sets default margins for users but usually we need to reset the margins to best fit our formatting needs. In Presentation, text is often inside a shape or textbox, and there is an option to set the margins for text inside shapes. It’s worthy of mentioning that Spire.Presentation provides the APIs to help users set margins for text inside shapes. This article is going to introduce the method to set the internal margins of shapes in C# using Spire.Presentation.

Note: before start, please download the latest version of Spire.Presentation and use the .dll in the bin folder as the reference of Visual Studio.

Step 1: Initial a new Presentation and insert a sample shape.

Presentation presentation = new Presentation();
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));

Step 2: Add sample text into the shape and format the text.

            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.DarkGreen;
            shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;
            shape.TextFrame.Text = "Using Spire.Presentation, developers will find an easy and effective method to create, read, write, modify, convert and print PowerPoint files on any .Net platform. It's worthwhile for you to try this amazing product.";
            shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold");
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

Step 3: Set the margins for the text frame.

            shape.TextFrame.MarginTop = 10;
            shape.TextFrame.MarginBottom = 35;
            shape.TextFrame.MarginLeft = 15;
            shape.TextFrame.MarginRight = 30;

Step 4: Save the document and launch to see effects.

            presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");

Effects:

How to set margins for text inside shapes in C#

Full Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));
       
            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.DarkGreen;
            shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;
            shape.TextFrame.Text = "Using Spire.Presentation, developers will find an easy and effective method to create, read, write, modify, convert and print PowerPoint files on any .Net platform. It's worthwhile for you to try this amazing product.";
            shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold");
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
   
            shape.TextFrame.MarginTop = 10;
            shape.TextFrame.MarginBottom = 35;
            shape.TextFrame.MarginLeft = 15;
            shape.TextFrame.MarginRight = 30;

            presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");

        }
    }
}