Align Text in PowerPoint Document in C#, VB.NET

2014-04-18 01:23:42 Written by  support iceblue
Rate this item
(0 votes)

PPT text Alignment can be taken as one kind of text format tools. It allows users to align text with the following styles: Left, Right, Center and Justify. By default, text will be aligned right. Also, users can customize alignment. Below demonstrate the effect by a screenshot which shows the text is corresponding to the alignment, for example, text Left is aligned left and Center is aligned center.

Align Text in PPT Document

Spire.Presentation for .NET, a professional .NET PPT component to allow users to manipulate PPT documents without automation. This guide will introduce a method to align text with C#, VB.NET via Spire.Presentation for .NET.

The method is:

First, new a PPT document, and set its slide. Then, traversal all alignment and use the new paragraph to show them. Last set the font and fill style for added paragraphs.

Download and install Spire.Presentation for .NET and use below code to experience this method to align text in PPT document.

The full code:

[C#]
using System;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace Alignment
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();


            //set background Image
            string ImageFile = @"bg.png";
            // set the rectangle size and position
            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);     
    //set the slide's shapes,background image and rectangle .
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            //set the shape's solidFillColor 
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            //append new shape 
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 600, 400));

            shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
            shape.Fill.FillType = FillFormatType.None;
            shape.TextFrame.Text = "Demo about Alignment";

            foreach (TextAlignmentType textAlign in Enum.GetValues(typeof(TextAlignmentType)))
            {
                //create a text range
                TextRange textRange = new TextRange(textAlign.ToString());

                //create a new paragraph
                TextParagraph paragraph = new TextParagraph();

                //apend the text range
                paragraph.TextRanges.Append(textRange);

                //set the alignment
                paragraph.Alignment = textAlign;

                //append to shape

                shape.TextFrame.Paragraphs.Append(paragraph);
            }


            //set the font and fill style
            foreach (TextParagraph paragraph in shape.TextFrame.Paragraphs)
            {
                paragraph.TextRanges[0].LatinFont = new TextFont("Arial Black");
                paragraph.TextRanges[0].Fill.FillType = FillFormatType.Solid;
                paragraph.TextRanges[0].Fill.SolidColor.Color = Color.Black;
            }

            //save the document
            presentation.SaveToFile("alignment.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("alignment.pptx");
        }
    }
}
[VB.NET]
Imports System.Text
Imports System.Drawing
Imports Spire.Presentation
Imports Spire.Presentation.Drawing

Public Class Form1

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click

        'create PPT document
        Dim presentation As New Presentation()

        'set background Image
        Dim ImageFile As String = "bg.png"
        Dim rect As New RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height)
        presentation.Slides(0).Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect)
        presentation.Slides(0).Shapes(0).Line.FillFormat.SolidFillColor.Color = Color.FloralWhite

        'append new shape
        Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 70, 350, 200))
        shape.TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Left
        shape.Fill.FillType = FillFormatType.None
        shape.TextFrame.Text = "Demo about Alignment"

        For Each textAlign As TextAlignmentType In [Enum].GetValues(GetType(TextAlignmentType))
            'create a text range
            Dim textRange As New TextRange(textAlign.ToString())

            'create a new paragraph
            Dim paragraph As New TextParagraph()

            'apend the text range
            paragraph.TextRanges.Append(textRange)

            'set the alignment
            paragraph.Alignment = textAlign

            'append to shape
            shape.TextFrame.Paragraphs.Append(paragraph)
        Next


        'set the font and fill style
        For Each paragraph As TextParagraph In shape.TextFrame.Paragraphs
            paragraph.TextRanges(0).LatinFont = New TextFont("Arial Black")
            paragraph.TextRanges(0).Fill.FillType = FillFormatType.Solid
            paragraph.TextRanges(0).Fill.SolidColor.Color = Color.Black
        Next

        'save the document
        presentation.SaveToFile("alignment.pptx", FileFormat.Pptx2010)
        System.Diagnostics.Process.Start("alignment.pptx")

    End Sub
End Class

Additional Info

  • tutorial_title: Align Text in PowerPoint Document
Last modified on Friday, 24 September 2021 09:17