Free Support Forum - aspose.com

Hello get all text from slide and do word count

hi i want to get all text from the slide and do word count my code is following anyone please see that and please help me

Try
Dim lic As Aspose.Slides.License = New Aspose.Slides.License
lic.SetLicense(HttpContext.Current.Server.MapPath(“bin”).ToString + “\” + “Aspose.Custom.lic”)
Dim srcPres As Presentation = New Presentation(root)
Dim totalWords As Int64 = 0



For i As Integer = 1 To srcPres.Slides.LastSlidePosition

Dim srcSld As Slide = srcPres.GetSlideByPosition(i)

Dim thldText1 As String = srcSld.HeaderFooter.HeaderText.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)

Dim splitParams1() As Char = {” "c}

Dim wordsInThisTextHolder1 As Integer = thldText1.Split(splitParams1, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder1


Dim thldText2 As String = srcSld.HeaderFooter.FooterText.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)

Dim splitParams2() As Char = {” "c}

Dim wordsInThisTextHolder2 As Integer = thldText2.Split(splitParams2, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder2


If Not srcSld.Notes Is Nothing Then
Dim thldText3 As String = srcSld.Notes.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)

Dim splitParams3() As Char = {” "c}

Dim wordsInThisTextHolder3 As Integer = thldText2.Split(splitParams3, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder3
End If

For Each shp As Shape In srcSld.Shapes


If Not shp.Placeholder Is Nothing Then

If TypeOf shp.Placeholder Is TextHolder Then

Dim thld As TextHolder = shp.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder

Continue For

End If

End If

If TypeOf shp Is Aspose.Slides.Table Then
Dim dt As Aspose.Slides.Table = shp

For Each tShape As Aspose.Slides.Shape In dt.Shapes
If tShape.TextFrame IsNot Nothing Then
Dim tfText As String = tShape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextFrame
End If
Next
Continue For
End If

If TypeOf shp Is Aspose.Slides.Rectangle Then
Dim rect As Aspose.Slides.Rectangle = shp

If rect.TextFrame IsNot Nothing Then
Dim tfText As String = rect.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextFrame
End If

Continue For
End If

If TypeOf shp Is Aspose.Slides.GroupShape Then
Try

Dim shp1 As Aspose.Slides.Shape = shp

Dim dt As Aspose.Slides.GroupShape = shp1
totalWords += Group(dt)
Catch ex As Exception

End Try

Continue For

‘’ ‘’ ''Catch ex As Exception

‘’ ‘’ ''End Try

End If

If TypeOf shp Is Aspose.Slides.Polyline Then
Dim Eshape As Aspose.Slides.Polyline = shp
If Not Eshape Is Nothing Then
If Eshape.TextFrame IsNot Nothing Then
Dim tfText As String = Eshape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextFrame
Continue For
End If
If Eshape.Placeholder IsNot Nothing Then
If TypeOf Eshape.Placeholder Is TextHolder Then

Dim thld As TextHolder = Eshape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder

Continue For
End If
End If
End If
End If

If TypeOf shp Is Aspose.Slides.PictureFrame Then
Dim Eshape As Aspose.Slides.PictureFrame = shp
If Not Eshape Is Nothing Then
If Eshape.TextFrame IsNot Nothing Then
Dim tfText As String = Eshape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextFrame
Continue For
End If
If Eshape.Placeholder IsNot Nothing Then
If TypeOf Eshape.Placeholder Is TextHolder Then

Dim thld As TextHolder = Eshape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextHolder

Continue For
End If
End If
End If
End If
If Not shp.TextFrame Is Nothing Then

Dim tf As TextFrame = shp.TextFrame

Dim tfText As String = tf.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)

Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

totalWords += wordsInThisTextFrame

End If

Next

Next

Response.Write("Word Count = " + totalWords.ToString)



and Function is following for group shape


Function Group(ByVal shp1 As Aspose.Slides.GroupShape)
Dim WordC As Integer = 0
Dim dt As Aspose.Slides.GroupShape = shp1
For Each tShape As Aspose.Slides.Shape In dt.Shapes
If TypeOf tShape Is Aspose.Slides.GroupShape Then
WordC += Group(tShape)
Else
If TypeOf tShape Is Aspose.Slides.Rectangle Then

If Not tShape.TextFrame Is Nothing Then

Dim tfText As String = tShape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
If tfText = " " Then
Continue For
End If
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame
End If
Continue For
End If
If tShape.TextFrame IsNot Nothing Then
Dim tfText As String = tShape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
If tfText = " " Then
Continue For
End If
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame

Continue For
End If

If Not tShape.Placeholder Is Nothing Then

If TypeOf tShape.Placeholder Is TextHolder Then

Dim thld As TextHolder = tShape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextHolder

Continue For
End If
End If

If TypeOf tShape Is Aspose.Slides.Table Then
Dim dt1 As Aspose.Slides.Table = tShape

For Each tShape1 As Aspose.Slides.Shape In dt.Shapes
If tShape.TextFrame IsNot Nothing Then
Dim tfText As String = tShape1.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame
End If
Next
Continue For
End If

If TypeOf tShape Is Aspose.Slides.Ellipse Then
Dim Eshape As Aspose.Slides.Ellipse = tShape
If Not Eshape Is Nothing Then
If Eshape.TextFrame IsNot Nothing Then
Dim tfText As String = Eshape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame
Continue For
End If
If Eshape.Placeholder IsNot Nothing Then
If TypeOf Eshape.Placeholder Is TextHolder Then

Dim thld As TextHolder = Eshape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextHolder

Continue For
End If
End If
End If
End If


If TypeOf tShape Is Aspose.Slides.PictureFrame Then
Dim Eshape As Aspose.Slides.PictureFrame = tShape
If Not Eshape Is Nothing Then
If Eshape.TextFrame IsNot Nothing Then
Dim tfText As String = Eshape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame
Continue For
End If
If Eshape.Placeholder IsNot Nothing Then
If TypeOf Eshape.Placeholder Is TextHolder Then

Dim thld As TextHolder = Eshape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)

Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextHolder

Continue For
End If
End If
End If
End If

If TypeOf tShape Is Aspose.Slides.Polyline Then
Dim Eshape As Aspose.Slides.Polyline = tShape
If Not Eshape Is Nothing Then
If Eshape.TextFrame IsNot Nothing Then
Dim tfText As String = Eshape.TextFrame.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)
Dim splitParams() As Char = {” "c}

Dim wordsInThisTextFrame As Integer = tfText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextFrame
Continue For
End If
If Eshape.Placeholder IsNot Nothing Then
If TypeOf Eshape.Placeholder Is TextHolder Then

Dim thld As TextHolder = Eshape.Placeholder

Dim thldText As String = thld.Text.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " “)


Dim splitParams() As Char = {” "c}

Dim wordsInThisTextHolder As Integer = thldText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

WordC += wordsInThisTextHolder

Continue For
End If
End If
End If
End If
End If
Next
Return WordC
End Function

You should skip all words which contain only signs or punctuation marks like: “/”, “-”, “(+)” etc.

my code count that character if it is seprate then count as a single words so it is not single but in other word then count whole word as a single word so i think it is not problem.

Please check this code, I think, it covers all the text and count the words properly from all shapes including group shapes except it needs a check mentioned by Alexey earlier. It is very simple to do so, just modify the CountWordsInString function

************************************************

Private Function CountWordsInPresentation() As Integer

Dim strPath As String = "c:\"

Dim srcPres As Presentation = New Presentation(strPath & "-ressources_obligatoires.ppt")

Dim totalWords As Integer = 0

For i As Integer = 1 To srcPres.Slides.LastSlidePosition

Dim srcSld As Slide = srcPres.GetSlideByPosition(i)

'Get all the words from the slids

totalWords += CountWordsInShapes(srcSld.Shapes)

'Get the words from the slide notes

If Not srcSld.Notes Is Nothing Then

totalWords += CountWordsInString(srcSld.Notes.Text)

End If

Next 'For each

CountWordsInPresentation = totalWords

End Function

Private Function CountWordsInShapes(ByVal allShapes As Shapes) As Integer

Dim totalWords As Integer = 0

For Each shp As Shape In allShapes

If Not shp.Placeholder Is Nothing Then

If TypeOf shp.Placeholder Is TextHolder Then

Dim thld As TextHolder = shp.Placeholder

totalWords += CountWordsInString(thld.Text)

Continue For

End If

End If

If Not shp.TextFrame Is Nothing Then

Dim tf As TextFrame = shp.TextFrame

totalWords += CountWordsInString(tf.Text)

Continue For

End If

'Count words in groupshapes

If TypeOf shp Is GroupShape Then

Dim grpShape As GroupShape = shp

totalWords += CountWordsInShapes(grpShape.Shapes)

End If

Next

CountWordsInShapes = totalWords

End Function

Private Function CountWordsInString(ByVal strText As String) As Integer

strText = strText.Replace(vbCr, " ").Replace(vbLf, " ").Replace(vbVerticalTab, " ")

Dim splitParams() As Char = {" "c}

CountWordsInString = strText.Split(splitParams, StringSplitOptions.RemoveEmptyEntries).Length

End Function