Paragraph and Text

Paragraph and Text (22)

Friday, 06 December 2019 08:14

New Method to Replace Text in PowerPoint in C#

Written by support iceblue

Spire.Presentation 4.11 and the later versions provide the following methods to replace text in PowerPoint document:

ReplaceFirstText(string matchedString, string newValue, bool caseSensitive): replace the first occurrence of searched text only.

ReplaceAllText(string matchedString, string newValue, bool caseSensitive): replace all the occurrences of searched text.

In the below examples, we will show you how to use the above methods to replace text in a PowerPoint document.

Replace the first occurrence of searched text only

using Spire.Presentation;

namespace ReplaceFirstText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Replace the first occurrence of “Spire.Presentation” with “New Text” by using ReplaceFirstText method
            slide.ReplaceFirstText("Spire.Presentation", "New Text", false);

            //Save the resultant document
            ppt.SaveToFile("RplaceFirstText.pptx", FileFormat.Pptx2013);
        }
    }
}

New Method to Replace Text in PowerPoint in C#

Replace all the occurrences of searched text

using Spire.Presentation;

namespace ReplaceAllText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Replace all the occurrences of “Spire.Presentation” with “New Text” by using ReplaceAllText method
            slide.ReplaceAllText("Spire.Presentation", "New Text", false);

            //Save the resultant document
            ppt.SaveToFile("RplaceAllText.pptx", FileFormat.Pptx2013);
        }
    }
}

New Method to Replace Text in PowerPoint in C#

Wednesday, 04 December 2019 03:42

Highlight Text in PowerPoint in C#

Written by support iceblue

This article demonstrates how to highlight the specific text within a PowerPoint document by using Spire.Presentation for .NET.

using Spire.Presentation;
using System.Drawing;

namespace HighlightTextInPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the sample PowerPoint file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx");

            //Loop through the slides
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                //Get the specific slide
                ISlide slide = presentation.Slides[i];

                //Loop through the shapes
                for (int j = 0; j < slide.Shapes.Count; j++)
                {
                    if (slide.Shapes[j] is IAutoShape)
                    {
                        //Get the specific shape
                        IAutoShape shape = slide.Shapes[j] as IAutoShape;

                        //Initialize an object of TextHighLightingOptions
                        TextHighLightingOptions options = new TextHighLightingOptions();
                        options.CaseSensitive = true;
                        options.WholeWordsOnly = true;

                        //Highligh the specific text within the shape with color
                        shape.TextFrame.HighLightText("Spire.Presentation", Color.LightYellow, options);
                    }
                   
                }
            }

            //Save to file
            presentation.SaveToFile("Highlight Text.pptx", FileFormat.Pptx2013);
        }
    }
}

Highlight Text in PowerPoint in C#

Wednesday, 04 September 2019 07:38

Set 3D effect for the text in PowerPoint in C#

Written by support iceblue

With the help of Spire.Presentation for .NET, we could set 3D effect to the shapes and text in the PowerPoint document to make it attractive. We have already demonstrated how to use Spire.Presentation to set 3D format for shapes. In this article, I will introduce how to create three dimensional effects text in PowerPoint in C#.

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace 3DEffectForText
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new presentation object
            Presentation presentation = new Presentation();

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

            //Append a new shape to slide and set the line color and fill type
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(30, 40, 600, 200));
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

            //Add text to the shape
            shape.AppendTextFrame("This demo shows how to add 3D effect text to Presentation slide");

            //set the color of text in shape
            TextRange textRange = shape.TextFrame.TextRange;
            textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            textRange.Fill.SolidColor.Color = Color.Green;

            //set the Font of text in shape
            textRange.FontHeight = 30;
            textRange.LatinFont = new TextFont("Gulim");


            //Set 3D effect for text
            shape.TextFrame.TextThreeD.ShapeThreeD.PresetMaterial = PresetMaterialType.Matte;
            shape.TextFrame.TextThreeD.LightRig.PresetType = PresetLightRigType.Sunrise;
            shape.TextFrame.TextThreeD.ShapeThreeD.TopBevel.PresetType = BevelPresetType.Circle;
            shape.TextFrame.TextThreeD.ShapeThreeD.ContourColor.Color = Color.Green;
            shape.TextFrame.TextThreeD.ShapeThreeD.ContourWidth = 3;                    
          
            //Save the document to file.
            presentation.SaveToFile("3DEffectForText_result.pptx", FileFormat.Pptx2010);                                                        
        }
    }
}

Effective screenshot for 3-D effect text on Presentation slides:

Set 3D effect for the text in PowerPoint in C#

This article demonstrates how to add a picture as a custom bullet style in a PowerPoint file using Spire.Presentation.

Below is the screenshot of the example PowerPoint file:

Add a Picture as a Custom Bullet Style in PowerPoint in C#

Detail steps:

Step 1: Instantiate a Presentation object and load the PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"Input.pptx");

Step 2: Get the first shape on the first slide.

IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;

Step 3: Add a picture as the bullet style of the paragraphs in the shape.

foreach (TextParagraph paragraph in shape.TextFrame.Paragraphs)
{
    paragraph.BulletType = TextBulletType.Picture;
    Image bulletPicture = Image.FromFile(@"timg.jpg");
    paragraph.BulletPicture.EmbedImage = ppt.Images.Append(bulletPicture);
}

Step 4: Save the file.

ppt.SaveToFile(@"Output.pptx", FileFormat.Pptx2010);

Screenshot:

Add a Picture as a Custom Bullet Style in PowerPoint in C#

Full code:

using System.Drawing;
using Spire.Presentation;

namespace Add_Picture_as_Custom_Bullet_Style_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Presentation object
            Presentation ppt = new Presentation();
            //Load the PowerPoint file
            ppt.LoadFromFile(@"Input.pptx");

            //Get the first shape on the first slide
            IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;

            //Traverse through the paragraphs in the shape
            foreach (TextParagraph paragraph in shape.TextFrame.Paragraphs)
            {
                //Set the bullet style of paragraph as picture
                paragraph.BulletType = TextBulletType.Picture;
                //Load a picture
                Image bulletPicture = Image.FromFile(@"timg.jpg");
                //Add the picture as the bullet style of paragraph
                paragraph.BulletPicture.EmbedImage = ppt.Images.Append(bulletPicture);
            }

            //Save the file
            ppt.SaveToFile(@"Output.pptx", FileFormat.Pptx2010);
        }
    }
}

We have already demonstrated how to use Spire.Presentation to insert HTML formatted text to PowerPoint slide. Now from Spire.Presentation 3.4.1, it supports to insert html (including text, image, audio and video) into presentation slides and each html tag will be added to the slide as a separate shape. The following code snippets demonstrate how to.

Step 1: Create an instance of Presentation class.

Presentation ppt = new Presentation();

Step 2: Get the shapes on the first slide.

ShapeList shapes = ppt.Slides[0].Shapes;

Step 3: Add contents to shape from HTML code, which includes text and image.

shapes.AddFromHtml("<html><div><p>First paragraph</p><p><img src='https://cdn.e-iceblue.com/Logo.jpg'/></p><p>Second paragraph </p></html>");

Step 4: Save the document to file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot:

Insert HTML with image into the Presentation slides in C#

Full codes of how to insert HTML into the Presentation slides:

Presentation ppt = new Presentation();

ShapeList shapes = ppt.Slides[0].Shapes;
            
shapes.AddFromHtml("<html><div><p>First paragraph</p><p><img src='https://cdn.e-iceblue.com/Logo.jpg'/></p><p>Second paragraph </p></html>");

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Wednesday, 07 February 2018 09:36

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

Written by support iceblue

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#]
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]
Dim presentation As Presentation =  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)

Spire.Presentation supports to operate the paragraph styles from code. There are two kinds of special indentation styles for the paragraph, first line and hanging. This article will demonstrate how to set the indent style for the paragraph on presentation slide in C#.

Step 1: Create an instance of presentation and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Get the paragraphs from the first slide.

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

Step 3: Set the indentation as first line for the first paragraph.

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

Step 4: Set the indentation as Hanging for the third paragraph.

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

Step 5: Save the document to file.

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot of the presentation with indentation style:

How to set the indent style for the paragraph on presentation slide in C#

Full codes:

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Thursday, 07 December 2017 08:44

Apply Transparency to Text in PowerPoint in C#, VB.NET

Written by jie zou

It is possible to add text transparency to any text shape in PowerPoint. In order to make the text transparent, we’d need to apply the transparency level to the text shape. This article will show you how to set the transparency level of text using Spire.Presentation.

Step 1: Create a Presentation instance.

Presentation ppt = new Presentation();

Step 2: Add a shape to the first slide.

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70,300, 120));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = FillFormatType.None;

Step 3: Add text to shape.

textboxShape.TextFrame.Text = "Text Transparency";

Step 4: Set the fill type of TextRange to solid, and fill the TextRange using the color created with specified alpha value.

textboxShape.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid;
int alpha = 55;
textboxShape.TextFrame.TextRange.Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple);

Step 5:Save the file.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);

Output:

Apply Transparency to Text in PowerPoint in C#, VB.NET

Full Code:

[C#]
//create a PowerPoint document
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;

//add a shape
IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70,300, 120));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = FillFormatType.None;

//remove default blank paragraphs
textboxShape.TextFrame.Paragraphs.Clear();

//add three paragraphs, apply color with different alpha values to text
int alpha = 55;
for (int i = 0; i < 3; i++)
{
    textboxShape.TextFrame.Paragraphs.Append(new TextParagraph());
    textboxShape.TextFrame.Paragraphs[i].TextRanges.Append(new TextRange("Text Transparency"));
    textboxShape.TextFrame.Paragraphs[i].TextRanges[0].Fill.FillType = FillFormatType.Solid;
    textboxShape.TextFrame.Paragraphs[i].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple);
    alpha += 100;
}

//save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013); 
[VB.NET]
'create a PowerPoint document
Dim ppt As Presentation = New Presentation()
ppt.SlideSize.Type = SlideSizeType.Screen16x9
 
'add a shape
Dim textboxShape As IAutoShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 70, 300, 120))
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
textboxShape.Fill.FillType = FillFormatType.None
 
'remove default blank paragraphs
textboxShape.TextFrame.Paragraphs.Clear()
 
'add three paragraphs, apply color with different alpha values to text
Dim alpha As Integer = 55
Dim i As Integer
For i = 0 To  3- 1  Step i + 1
    textboxShape.TextFrame.Paragraphs.Append(New TextParagraph())
    textboxShape.TextFrame.Paragraphs(i).TextRanges.Append(New TextRange("Text Transparency"))
    textboxShape.TextFrame.Paragraphs(i).TextRanges(0).Fill.FillType = FillFormatType.Solid
    textboxShape.TextFrame.Paragraphs(i).TextRanges(0).Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple)
    alpha += 100
Next
 
'save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)

You may sometimes want your text to display vertically (letters stacked one on top of the other), horizontally, or rotated facing the right margin or the left margin. This tutorial shows you how to change the text direction using VerticalTextType property in Spire.Presentation.

Step 1: Initialize an instance of Prensentation class.

Presentation ppt = new Presentation();

Step 2: Append a shape with text to the first slide.

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.OrangeRed;
textboxShape.TextFrame.Text = "You Are Welcome Here";

Step 3: Set the text direction to vertical.

textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical;

Step 4: Append another shape with text to the slide.

textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(150, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.Orange;
textboxShape.TextFrame.Text = "欢迎光临";

Step 5: For asian characters, you can set the VerticalTextType as EastAsianVertical to aviod rotating text 90 degrees.

textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical;

Step 6: Save the file.

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

Output:

How to Change Text Direction in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.OrangeRed;
textboxShape.TextFrame.Text = "You Are Welcome Here";
textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical;

textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(150, 70, 100, 400));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textboxShape.Fill.SolidColor.Color = Color.Orange;
textboxShape.TextFrame.Text = "欢迎光临";
textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical;

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);
[VB.NET]
Dim ppt As New Presentation()

Dim textboxShape As IAutoShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 70, 100, 400))
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
textboxShape.Fill.SolidColor.Color = Color.OrangeRed
textboxShape.TextFrame.Text = "You Are Welcome Here"
textboxShape.TextFrame.VerticalTextType = VerticalTextType.Vertical

textboxShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(150, 70, 100, 400))
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
textboxShape.Fill.SolidColor.Color = Color.Orange
textboxShape.TextFrame.Text = "欢迎光临"
textboxShape.TextFrame.VerticalTextType = VerticalTextType.EastAsianVertical

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013)

Spire.Presentation supports to insert HTML formatted text to PowerPoint slide. The following code snippets demonstrate how to.

Step 1: Create an instance of Presentation class.

Presentation ppt = new Presentation();

Step 2: Insert an autoshape (rectangle) in slide.

IAutoShape shape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 400, 100));

Step 3: Clear default paragraphs in the shape.

shape.TextFrame.Paragraphs.Clear();

Step 4: Add paragraphs to shape from HTML code. Make sure your HTML segments are written between <html><body> and </body></html> tags, otherwise, AddFromHtml method will fail to work.

string htmlText= "<html><body><p>First paragraph</p><p>Second paragraph</p></body></html>";
shape.TextFrame.Paragraphs.AddFromHtml(htmlText);

Step 5: Save to file.

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

Output:

Append HTML String to PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();
IAutoShape shape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 400, 100));
shape.TextFrame.Paragraphs.Clear();

string htmlText= "<html><body><p>First paragraph</p><p>Second paragraph</p></body></html>";
shape.TextFrame.Paragraphs.AddFromHtml(htmlText);

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);
[VB.NET]
Dim ppt As New Presentation()
Dim shape As IAutoShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 50, 400, 100))
shape.TextFrame.Paragraphs.Clear()

Dim htmlText As String = "<html><body><p>First paragraph</p><p>Second paragraph</p></body></html>";
shape.TextFrame.Paragraphs.AddFromHtml(htmlText)

ppt.SaveToFile("output.pptx", FileFormat.Pptx2013)
Page 1 of 2