PowerPoint paragraph formatting in C# and VB.NET

See how to set the paragraph formatting options in your PowerPoint files in C# and VB.NET using the GemBox.Presentation .NET library.

using GemBox.Presentation;

// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

var presentation = new PresentationDocument();

// Create new slide.
var slide = presentation.Slides.AddNew(SlideLayoutType.Custom);

// Create new text box.
var textBox = slide.Content.AddTextBox(
ShapeGeometryType.RoundedRectangle, 2, 2, 10, 4, LengthUnit.Centimeter);

// Create new paragraph.
var paragraph = textBox.AddParagraph();

// Set paragraph text.
paragraph.AddRun("This paragraph has the following properties: alignment is justify, after spacing is 100% of the text size, before spacing is 250% of the text size, line spacing is 200% of the text size.");

// Set selected paragraph format.
var format = paragraph.Format;
format.Alignment = HorizontalAlignment.Justify;
format.SpacingAfter = TextSpacing.Single;
format.SpacingBefore = TextSpacing.Multiple(2.5);
format.SpacingLine = TextSpacing.Double;

// Create new paragraph.
paragraph = textBox.AddParagraph();

// Set paragraph text.
paragraph.AddRun("This paragraph has the following properties: alignment is left, indentation before text is 15 points and first line indentation is 25 points.");

// Set selected paragraph format.
paragraph.Format.Alignment = HorizontalAlignment.Left;
paragraph.Format.IndentationBeforeText = Length.From(15, LengthUnit.Point);
paragraph.Format.IndentationSpecial = Length.From(25, LengthUnit.Point);

presentation.Save("Paragraph Formatting.pptx");
Imports GemBox.Presentation

Module Program

Sub Main()

' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")

Dim presentation = New PresentationDocument

' Create New slide.
Dim slide = presentation.Slides.AddNew(SlideLayoutType.Custom)

' Create New text box.
Dim textBox = slide.Content.AddTextBox(
ShapeGeometryType.RoundedRectangle, 2, 2, 10, 4, LengthUnit.Centimeter)

' Create New paragraph.
Dim paragraph = textBox.AddParagraph()

' Set paragraph text.
paragraph.AddRun("This paragraph has the following properties: alignment is justify, after spacing is 100% of the text size, before spacing is 250% of the text size, line spacing is 200% of the text size.")

' Set selected paragraph format.
Dim format = paragraph.Format
format.Alignment = HorizontalAlignment.Justify
format.SpacingAfter = TextSpacing.Single
format.SpacingBefore = TextSpacing.Multiple(2.5)
format.SpacingLine = TextSpacing.Double

' Create New paragraph.
paragraph = textBox.AddParagraph()

' Set paragraph text.
paragraph.AddRun("This paragraph has the following properties: alignment is left, indentation before text is 15 points and first line indentation is 25 points.")

' Set selected paragraph format.
paragraph.Format.Alignment = HorizontalAlignment.Left
paragraph.Format.IndentationBeforeText = Length.From(15, LengthUnit.Point)
paragraph.Format.IndentationSpecial = Length.From(25, LengthUnit.Point)

presentation.Save("Paragraph Formatting.pptx")
End Sub
End Module
PowerPoint paragraphs formatted with GemBox.Presentation
Screenshot of PowerPoint paragraphs formatted with GemBox.Presentation

Next steps

GemBox.Presentation is a .NET library that enables you to read, write, edit, convert, and print presentation files from your .NET applications using one simple API.