Home > Code > C# > How to Import & Export HTML Text in a PPTX Presentation inside .NET Apps

How to Import & Export HTML Text in a PPTX Presentation inside .NET Apps

by sher azam   on May 13, 2015   Category: C#   |  Views: 1061    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can import and export HTML text in PPTX presentation files inside their .NET applications. This topic is also part of a series of topics about managing text paragraphs. Aspose.Slides for .NET has enhanced support for adding HTML text or saving paragraphs text to HTML. This article shows how to manage paragraphs to use HTML data and shows how developers can use this small yet powerful feature.

Importing HTML Text in Paragraphs

To manage paragraph bullets using Aspose.Slides for .NET:

• Create an instance of the lPresentation class.
• Access the desired slide in slide collection using the ISlide object.
• Add an autoshape to the selected slide.
• Add and access the ITextFrame of the added shape.
• Remove the default paragraph in the ITextFrame.
• Read the source HTML file in a TextReader.
• Create the first paragraph instance using the Paragraph class.
• Add the HTML file content in the read TextReader to the TextFrame's ParagraphCollection.
• Save the presentation.

// HTML text addition to paragraphs

//[C# Code Sample]

//Create Empty presentation instance//Create Empty presentation instance
using (Presentation pres = new Presentation())
{
//Acesss the default first slide of presentation

ISlide slide = pres.Slides[0];

//Adding the AutoShape to accomodate 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;

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

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

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

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

//Saving Presentation
pres.Save("output.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

}

//[VB.NET Code Sample]

'Create Empty presentation instance
Using pres As New Presentation()
'Acesss the default first slide of presentation

Dim slide As ISlide = pres.Slides(0)

'Adding the AutoShape to accomodate the HTML content
Dim ashape As IAutoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, pres.SlideSize.Size.Width - 20, pres.SlideSize.Size.Height - 10)

ashape.FillFormat.FillType = FillType.NoFill

'Adding text frame to the shape
ashape.AddTextFrame("")

'Clearing all paragraphs in added text frame
ashape.TextFrame.Paragraphs.Clear()

'Loading the HTML file using stream reader
Dim tr As TextReader = New StreamReader("file.html")

'Adding text from HTML stream reader in text frame
ashape.TextFrame.Paragraphs.AddFromHtml(tr.ReadToEnd())

'Saving Presentation
pres.Save("output.pptx", Aspose.Slides.Export.SaveFormat.Pptx)

End Using

// Exporting Paragraphs Text to HTML

//[C# Code Sample]


//Load the presentation file
using (Presentation pres = new Presentation("demo.pptx"))
{

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

//Desired index
int index = 0;

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

// Extracting first paragraph as HTML
StreamWriter sw = new StreamWriter("output.html", false, Encoding.UTF8);

//Writing Paragraphs data to HTML by providing paragraph starting index, total paragraphs to be copied
sw.Write(ashape.TextFrame.Paragraphs.ExportToHtml(0, ashape.TextFrame.Paragraphs.Count, null));

sw.Close();
}

//[VB.NET Code Sample]

'Load the presentation file
Using pres As New Presentation("demo.pptx")

'Acesss the default first slide of presentation
Dim slide As ISlide = pres.Slides(0)

'Desired index
Dim index As Integer = 0

'Accessing the added shape
Dim ashape As IAutoShape = CType(slide.Shapes(index), IAutoShape)

' Extracting first paragraph as HTML
Dim sw As New StreamWriter("output.html", False, Encoding.UTF8)

'Writing Paragraphs data to HTML by providing paragraph starting index, total paragraphs to be copied
sw.Write(ashape.TextFrame.Paragraphs.ExportToHtml(0, ashape.TextFrame.Paragraphs.Count, Nothing))

sw.Close()
End Using



Overview: Aspose.Slides for .NET

Aspose.Slides is a .NET component to read, write and modify a PowerPoint document without using MS PowerPoint. PowerPoint versions from 97-2007 and all three PowerPoint formats: PPT, POT, PPS are also supported. Now you can create, access, copy, clone, edit and delete slides in your presentations. Other features include PPT/PPTX printing, PPT to XPS format, saving PowerPoint slides into PDF, adding & modifying audio & video frames, using shapes like rectangles or ellipses and saving presentations in SVG format, streams or images.

- Homepage of Aspose.Slides for .NET: http://www.aspose.com/.net/powerpoint-component.aspx

- Download Aspose.Slides for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.slides-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts