Manage PowerPoint Paragraph in C#

Aspose.Slides provides all the interfaces and classes you need to work with PowerPoint texts, paragraphs, and portions in C#.

  • Aspose.Slides provides the ITextFrame interface to allow you to add objects that represent a paragraph. An ITextFame object can have one or multiple paragraphs (each paragraph is created through a carriage return).
  • Aspose.Slides provides the IParagraph interface to allow you to add objects that represent portions. An IParagraph object can have one or multiple portions (collection of iPortions objects).
  • Aspose.Slides provides IPortion interface to allow you to add objects that represent texts and their formatting properties.

An IParagraph object is capable of handling texts with different formatting properties through its underlying IPortion objects.

Add Multiple Paragraph Containing Multiple Portions

These steps show you how to add a text frame containing 3 paragraphs and each paragraph containing 3 portions:

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add a Rectangle IAutoShape to the slide.
  4. Get the ITextFrame associated with the IAutoShape.
  5. Create two IParagraph objects and add them to the IParagraphs collection of the ITextFrame.
  6. Create three IPortion objects for each new IParagraph (two Portion objects for default Paragraph) and add each IPortion object to the IPortion collection of each IParagraph.
  7. Set some text for each portion.
  8. Apply your preferred formatting features to each portion using the formatting properties exposed by the IPortion object.
  9. Save the modified presentation.

This C# code is an implementation of the steps for adding paragraphs containing portions:

// Instantiates a Presentation class that represents a PPTX file
using (Presentation pres = new Presentation())
{
    // Accesses the first slide
    ISlide slide = pres.Slides[0];

    // Adds a Rectangle IAutoShape
    IAutoShape ashp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);

    // Accesses the AutoShape TextFrame
    ITextFrame tf = ashp.TextFrame;

    // Creates Paragraphs and Portions with different text formats
    IParagraph para0 = tf.Paragraphs[0];
    IPortion port01 = new Portion();
    IPortion port02 = new Portion();
    para0.Portions.Add(port01);
    para0.Portions.Add(port02);

    IParagraph para1 = new Paragraph();
    tf.Paragraphs.Add(para1);
    IPortion port10 = new Portion();
    IPortion port11 = new Portion();
    IPortion port12 = new Portion();
    para1.Portions.Add(port10);
    para1.Portions.Add(port11);
    para1.Portions.Add(port12);

    IParagraph para2 = new Paragraph();
    tf.Paragraphs.Add(para2);
    IPortion port20 = new Portion();
    IPortion port21 = new Portion();
    IPortion port22 = new Portion();
    para2.Portions.Add(port20);
    para2.Portions.Add(port21);
    para2.Portions.Add(port22);

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            tf.Paragraphs[i].Portions[j].Text = "Portion0" + j.ToString();
            if (j == 0)
            {
                tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
                tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
                tf.Paragraphs[i].Portions[j].PortionFormat.FontBold = NullableBool.True;
                tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 15;
            }
            else if (j == 1)
            {
                tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
                tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
                tf.Paragraphs[i].Portions[j].PortionFormat.FontItalic = NullableBool.True;
                tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 18;
            }
        }
    // Saves the modified presentation
    pres.Save("multiParaPort_out.pptx", SaveFormat.Pptx);

Manage Paragraph Bullets

Bullet lists help you to organize and present information quickly and efficiently. Bulleted paragraphs are always easier to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the selected slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance using the Paragraph class.
  7. Set the bullet Type for the paragraph to Symbol and set the bullet character.
  8. Set the paragraph Text.
  9. Set the paragraph Indent for the bullet.
  10. Set a color for the bullet.
  11. Set a height of the bullet.
  12. Add the new paragraph to the TextFrame paragraph collection.
  13. Add the second paragraph and repeat the process given in steps 7 to 13.
  14. Save the presentation.

This C# code shows you how to add a paragraph bullet:

// Instantiates a Presentation class that represents a PPTX file
using (Presentation pres = new Presentation())
{

    // Accesses the first slide
    ISlide slide = pres.Slides[0];


    // Adds and accesses Autoshape
    IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);

    // Accesses the autoshape text frame
    ITextFrame txtFrm = aShp.TextFrame;

    // Removes the default paragraph
    txtFrm.Paragraphs.RemoveAt(0);

    // Creates a paragraph
    Paragraph para = new Paragraph();

    // Sets a paragraph bullet style and symbol
    para.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    para.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);

    // Sets a paragraph text
    para.Text = "Welcome to Aspose.Slides";

    // Sets bullet indent
    para.ParagraphFormat.Indent = 25;

    // Sets bullet color
    para.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
    para.ParagraphFormat.Bullet.Color.Color = Color.Black;
    para.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // set IsBulletHardColor to true to use own bullet color

    // Sets Bullet Height
    para.ParagraphFormat.Bullet.Height = 100;

    // Adds Paragraph to text frame
    txtFrm.Paragraphs.Add(para);

    // Creates second paragraph
    Paragraph para2 = new Paragraph();

    // Sets paragraph bullet type and style
    para2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
    para2.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;

    // Adds paragraph text
    para2.Text = "This is numbered bullet";

    // Sets bullet indent
    para2.ParagraphFormat.Indent = 25;

    para2.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
    para2.ParagraphFormat.Bullet.Color.Color = Color.Black;
    para2.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // set IsBulletHardColor to true to use own bullet color

    // Sets Bullet Height
    para2.ParagraphFormat.Bullet.Height = 100;

    // Adds Paragraph to text frame
    txtFrm.Paragraphs.Add(para2);


    // Saves the modified presentation
    pres.Save("Bullet_out.pptx", SaveFormat.Pptx);

}

Manage Picture Bullets

Bullet lists help you to organize and present information quickly and efficiently. Picture paragraphs are easy to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance using the Paragraph class.
  7. Load the image in IPPImage.
  8. Set the bullet type to Picture and set the image.
  9. Set the Paragraph Text.
  10. Set the Paragraph Indent for the bullet.
  11. Set a color for the bullet.
  12. Set a height for the bullet.
  13. Add the new paragraph to the TextFrame paragraph collection.
  14. Add the second paragraph and repeat the process based on the previous steps.
  15. Save the modified presentation.

This C# code shows you how to add and manage picture bullets:

// Instantiates a Presentation class that represents a PPTX file
Presentation presentation = new Presentation();

// Accesses the first slide
ISlide slide = presentation.Slides[0];

// Instantiates the image for bullets
Image image = new Bitmap("bullets.png");
IPPImage ippxImage = presentation.Images.AddImage(image);

// Adds and accesses Autoshape
IAutoShape autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);

// Accesses the autoshape textframe
ITextFrame textFrame = autoShape.TextFrame;

// Removes the default paragraph
textFrame.Paragraphs.RemoveAt(0);

// Creates a new paragraph
Paragraph paragraph = new Paragraph();
paragraph.Text = "Welcome to Aspose.Slides";

// Sets paragraph bullet style and image
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = ippxImage;

// Sets bullet Height
paragraph.ParagraphFormat.Bullet.Height = 100;

// Adds paragraph to text frame
textFrame.Paragraphs.Add(paragraph);

// Writes the presentation as a PPTX file
presentation.Save("ParagraphPictureBulletsPPTX_out.pptx", SaveFormat.Pptx);

// Writes the presentation as a PPT file
presentation.Save("ParagraphPictureBulletsPPT_out.ppt", SaveFormat.Ppt);

Manage Multilevel Bullets

Bullet lists help you to organize and present information quickly and efficiently. Multilevel bullets are easy to read and understand.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape in the new slide.
  4. Access the autoshape’s TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance through the Paragraph class and set the depth to 0.
  7. Create the second paragraph instance through the Paragraph class and set the depth set to 1.
  8. Create the third paragraph instance through the Paragraph class and set the depth set to 2.
  9. Create the fourth paragraph instance through the Paragraph class and set the depth set to 3.
  10. Add the new paragraphs to the TextFrame paragraph collection.
  11. Save the modified presentation.

This C# code shows you how to add and manage multilevel bullets:

// Instantiates a Presentation class that represents a PPTX file
using (Presentation pres = new Presentation())
{

    // Accesses the first slide
    ISlide slide = pres.Slides[0];
    
    // Adds and accesses Autoshape
    IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);

    // Accesses the text frame of created autoshape
    ITextFrame text = aShp.AddTextFrame("");
    
    // Clears the default paragraph
    text.Paragraphs.Clear();

    // Adds the first paragraph
    IParagraph para1 = new Paragraph();
    para1.Text = "Content";
    para1.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    para1.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
    para1.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    para1.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
    // Sets the bullet level
    para1.ParagraphFormat.Depth = 0;

    // Adds the second paragraph
    IParagraph para2 = new Paragraph();
    para2.Text = "Second Level";
    para2.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    para2.ParagraphFormat.Bullet.Char = '-';
    para2.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    para2.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
    // Sets the bullet level
    para2.ParagraphFormat.Depth = 1;

    // Adds the third paragraph
    IParagraph para3 = new Paragraph();
    para3.Text = "Third Level";
    para3.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    para3.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
    para3.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    para3.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
    // Sets the bullet level
    para3.ParagraphFormat.Depth = 2;

    // Adds the fourth paragraph
    IParagraph para4 = new Paragraph();
    para4.Text = "Fourth Level";
    para4.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    para4.ParagraphFormat.Bullet.Char = '-';
    para4.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    para4.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
    // Sets the bullet level
    para4.ParagraphFormat.Depth = 3;

    // Adds paragraphs to collection
    text.Paragraphs.Add(para1);
    text.Paragraphs.Add(para2);
    text.Paragraphs.Add(para3);
    text.Paragraphs.Add(para4);

    // Writes the presentation as a PPTX file
    pres.Save("MultilevelBullet.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

Manage Paragraph with Custom Numbered List

The IBulletFormat interface provides the NumberedBulletStartWith property and others that allow you to manage paragraphs with custom numbering or formatting.

  1. Create an instance of the Presentation class.
  2. Access the slide containing the paragraph.
  3. Add an autoshape to the slide.
  4. Access the autoshape TextFrame.
  5. Remove the default paragraph in the TextFrame.
  6. Create the first paragraph instance through the Paragraph class and set NumberedBulletStartWith to 2.
  7. Create the second paragraph instance through the Paragraph class and set NumberedBulletStartWith to 3.
  8. Create the third paragraph instance through the Paragraph class and set NumberedBulletStartWith to 7.
  9. Add the new paragraphs to the TextFrame paragraph collection.
  10. Save the modified presentation.

This C# code shows you how to add and manage paragraphs with custom numbering or formatting:

using (var presentation = new Presentation())
{
	var shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);

	// Accesses the text frame of created autoshape
	ITextFrame textFrame = shape.TextFrame;

	// Removes the default exisiting paragraph
	textFrame.Paragraphs.RemoveAt(0);

	// First list
	var paragraph1 = new Paragraph { Text = "bullet 2" };
	paragraph1.ParagraphFormat.Depth = 4; 
	paragraph1.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
	paragraph1.ParagraphFormat.Bullet.Type = BulletType.Numbered;
	textFrame.Paragraphs.Add(paragraph1);

	var paragraph2 = new Paragraph { Text = "bullet 3" };
	paragraph2.ParagraphFormat.Depth = 4;
	paragraph2.ParagraphFormat.Bullet.NumberedBulletStartWith = 3; 
	paragraph2.ParagraphFormat.Bullet.Type = BulletType.Numbered;  
	textFrame.Paragraphs.Add(paragraph2);

	
	var paragraph5 = new Paragraph { Text = "bullet 7" };
	paragraph5.ParagraphFormat.Depth = 4;
	paragraph5.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
	paragraph5.ParagraphFormat.Bullet.Type = BulletType.Numbered;
	textFrame.Paragraphs.Add(paragraph5);

	presentation.Save("SetCustomBulletsNumber-slides.pptx", SaveFormat.Pptx);
}

Set Paragraph Indent

  1. Create an instance of Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add a rectangle autoshape to the slide.
  4. Add a TextFrame with three paragraphs to the rectangle autoshape.
  5. Hide the rectangle lines.
  6. Set the indent for each Paragraph through their BulletOffset property.
  7. Write the modified presentation as a PPT file.

This C# code shows you how to set a paragraph indent:

// Instantiate Presentation Class
Presentation pres = new Presentation();

// Gets the first slide
ISlide sld = pres.Slides[0];

// Adds a Rectangle Shape
IAutoShape rect = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 500, 150);

// Adds TextFrame to the Rectangle
ITextFrame tf = rect.AddTextFrame("This is first line \rThis is second line \rThis is third line");

// Sets the text to fit the shape
tf.TextFrameFormat.AutofitType = TextAutofitType.Shape;

// Hides the lines of the Rectangle
rect.LineFormat.FillFormat.FillType = FillType.Solid;

// Gets the first Paragraph in the TextFrame and set its Indent
IParagraph para1 = tf.Paragraphs[0];

// Sets paragraph bullet style and symbol
para1.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para1.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para1.ParagraphFormat.Alignment = TextAlignment.Left;

para1.ParagraphFormat.Depth = 2;
para1.ParagraphFormat.Indent = 30;

// Gets second Paragraph in the TextFrame and set its Indent
IParagraph para2 = tf.Paragraphs[1];
para2.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para2.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para2.ParagraphFormat.Alignment = TextAlignment.Left;
para2.ParagraphFormat.Depth = 2;
para2.ParagraphFormat.Indent = 40;

// Gets third Paragraph in the TextFrame and sets its Indent
IParagraph para3 = tf.Paragraphs[2];
para3.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para3.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para3.ParagraphFormat.Alignment = TextAlignment.Left;
para3.ParagraphFormat.Depth = 2;
para3.ParagraphFormat.Indent = 50;

// Writes the Presentation to disk
pres.Save("InOutDent_out.pptx", SaveFormat.Pptx);

Set Hanging Indent for Paragraph

This C# code shows you how to set the hanging indent for a paragraph:

using (Presentation pres = new Presentation())
{
    var autoShape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 50, 250, 550, 150);

    Paragraph para1 = new Paragraph
    {
        Text = "Example"
    };
    Paragraph para2 = new Paragraph
    {
        Text = "Set Hanging Indent for Paragraph"
    };
    Paragraph para3 = new Paragraph
    {
        Text = "This C# code shows you how to set the hanging indent for a paragraph: "
    };

    para2.ParagraphFormat.MarginLeft = 10f;
    para3.ParagraphFormat.MarginLeft = 20f;
    
    autoShape.TextFrame.Paragraphs.Add(para1);
    autoShape.TextFrame.Paragraphs.Add(para2);
    autoShape.TextFrame.Paragraphs.Add(para3);
    
    pres.Save("pres.pptx", SaveFormat.Pptx);
}

Manage End Paragraph Run Properties for Paragraph

  1. Create an instance of Presentation class.
  2. Get the reference for the slide containing the paragraph through its position.
  3. Add a rectangle autoshape to the slide.
  4. Add a TextFrame with two paragraphs to the Rectangle.
  5. Set the FontHeight and Font type for the paragraphs.
  6. Set the End properties for the paragraphs.
  7. Write the modified presentation as a PPTX file.

This C# code shows you how to set the End properties for paragraphs in PowerPoint:

using (Presentation pres = new Presentation("Test.pptx"))
{
	IAutoShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);

	Paragraph para1 = new Paragraph();
	para1.Portions.Add(new Portion("Sample text"));

	Paragraph para2 = new Paragraph();
	para2.Portions.Add(new Portion("Sample text 2"));
	PortionFormat endParagraphPortionFormat = new PortionFormat();
	endParagraphPortionFormat.FontHeight = 48;
	endParagraphPortionFormat.LatinFont = new FontData("Times New Roman");
	para2.EndParagraphPortionFormat = endParagraphPortionFormat;

	shape.TextFrame.Paragraphs.Add(para1);
	shape.TextFrame.Paragraphs.Add(para2);

	pres.Save("pres.pptx", SaveFormat.Pptx);
}

Import HTML Text into Paragraphs

Aspose.Slides provides enhanced support for importing HTML text into paragraphs.

  1. Create an instance of the Presentation class.
  2. Access the relevant slide’s reference through its index.
  3. Add an autoshape to the slide.
  4. Add and access autoshape ITextFrame.
  5. Remove the default paragraph in the ITextFrame.
  6. Read the source HTML file in a TextReader.
  7. Create the first paragraph instance through the Paragraph class.
  8. Add the HTML file content in the read TextReader to the TextFrame’s ParagraphCollection.
  9. Save the modified presentation.

This C# code is an implementation of the steps for importing HTML texts in paragraphs:

// Creates Empty presentation instance
using (Presentation pres = new Presentation())
{
    // Acessses the default first slide of presentation
    ISlide slide = pres.Slides[0];

    // Adds the AutoShape to house the HTML content
    IAutoShape ashape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, pres.SlideSize.Size.Width - 20, pres.SlideSize.Size.Height - 10);

    ashape.FillFormat.FillType = FillType.NoFill;

    // Adds text frame to the shape
    ashape.AddTextFrame("");

    // Clears all paragraphs in the added text frame
    ashape.TextFrame.Paragraphs.Clear();

    // Loads the HTML file using stream reader
    TextReader tr = new StreamReader("file.html");

    // Adds the text from HTML stream reader in text frame
    ashape.TextFrame.Paragraphs.AddFromHtml(tr.ReadToEnd());

    // Saves Presentation
    pres.Save("output_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

Export Paragraphs Text to HTML

Aspose.Slides provides enhanced support for exporting texts (contained in paragraphs) to HTML.

  1. Create an instance of the Presentation class and load the desired presentation.
  2. Access the relevant slide’s reference through its index.
  3. Access the shape containing the text that will be exported to HTML.
  4. Access the shape TextFrame.
  5. Create an instance of StreamWriter and add the new HTML file.
  6. Provide a starting index to StreamWriter and export your preferred paragraphs.

This C# code shows you how to export PowerPoint paragraph texts to HTML:

// Loads the presentation file
using (Presentation pres = new Presentation("ExportingHTMLText.pptx"))
{

    // Acessses the default first slide of presentation
    ISlide slide = pres.Slides[0];

    // Accesses the required  index
    int index = 0;

    // Accesses the added shape
    IAutoShape ashape = (IAutoShape)slide.Shapes[index];

    StreamWriter sw = new StreamWriter("output_out.html", false, Encoding.UTF8);

    // Writes paragraphs data to HTML by specifying paragraph starting index and number of paragraphs to be copied
    sw.Write(ashape.TextFrame.Paragraphs.ExportToHtml(0, ashape.TextFrame.Paragraphs.Count, null));

    sw.Close();
}