Thursday, 27 July 2017 09:30

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

Written by  support iceblue
Rate this item
(0 votes)

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#]
using Spire.Presentation;
using System.Drawing;
namespace AppendHTMLString
{
    class Program
    {
        static void Main(string[] args)
        {

            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 = "

First paragraph

Second paragraph

"; shape.TextFrame.Paragraphs.AddFromHtml(htmlText); ppt.SaveToFile("output.pptx", FileFormat.Pptx2013); } } }
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing
Namespace AppendHTMLString
	Class Program
		Private Shared Sub Main(args As String())

			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 = "

First paragraph

Second paragraph

" shape.TextFrame.Paragraphs.AddFromHtml(htmlText) ppt.SaveToFile("output.pptx", FileFormat.Pptx2013) End Sub End Class End Namespace

Additional Info

  • tutorial_title:
Last modified on Monday, 16 November 2020 06:14