Error when replacing strings w/ 2.3.6.0

With the 2.3.6.0 release i receive the following exception when replace text with carriage returns within it. : Aspose.PowerPoint.PowerPointException: Can't assign string which contains paragraph break character.

Attached is the project that recreates the problem.

NOTE: It is extermly important to me to be able to replace text that has carriage returns with in it. I am open to any suggestion to work around this problem.

It was never possible to insert text with carriage return. In most cases it generated broken presentation and

we wrote many times about it in the forum. In 2.3.6 and later releases we check it and throw exception.



You should parse your text and insert separate paragraphs for each part of text between carriage returns.

Alexy,

I'm happy that an exception is thrown instead of creating a broken presentation.

However, isn't this an example where Aspose.Powerpoint should break the string apart an insert the extra paragraphs for the application instead of forcing it onto every application?

Thank you for listening,
Randy Stegbauer

Yes, you are right. By the way, 2.3.6 is the first version where added new possibility

to add text with carriage returns to a TextFrame or Paragraph.

All created Portion will have the same default formatting of course.

Attached is search replace algorithm that supports carrigage returns. The code is bit complicated cause we have to support multiple languages and ensure new styles are created when a new paragraph is created. But it should be enough to get you started.

Can you please send the the file you attached? I cannot get it from the forum.

Thank you,

Don

dpruitt@nfocus.com

Here is the code. :

Public Shared Function CloneParagraphStyle(ByVal paragraph As Aspose.Slides.Paragraph) As Aspose.Slides.Paragraph
If paragraph Is Nothing Then
Throw New System.ArgumentNullException("paragraph")
End If

Dim newParagraph As New Aspose.Slides.Paragraph

newParagraph.Alignment = paragraph.Alignment
newParagraph.BulletCharacter = paragraph.BulletCharacter
newParagraph.BulletColor = paragraph.BulletColor
newParagraph.BulletFontIndex = paragraph.BulletFontIndex
newParagraph.BulletHeight = paragraph.BulletHeight
newParagraph.BulletOffset = paragraph.BulletOffset
newParagraph.Depth = paragraph.Depth
newParagraph.HasBullet = paragraph.HasBullet
newParagraph.IsBulletHardFont = paragraph.IsBulletHardFont
newParagraph.NumberedBulletStartWith = paragraph.NumberedBulletStartWith
newParagraph.NumberedBulletStyle = paragraph.NumberedBulletStyle
newParagraph.PictureBulletId = paragraph.PictureBulletId
newParagraph.SpaceAfter = paragraph.SpaceAfter
newParagraph.SpaceBefore = paragraph.SpaceBefore
newParagraph.SpaceWithin = paragraph.SpaceWithin
newParagraph.TextOffset = paragraph.TextOffset

newParagraph.RawAlignment = paragraph.RawAlignment
newParagraph.RawBulletCharacter = paragraph.RawBulletCharacter
newParagraph.RawBulletColor = paragraph.RawBulletColor
newParagraph.RawBulletFontIndex = paragraph.RawBulletFontIndex
newParagraph.RawBulletHeight = paragraph.RawBulletHeight
newParagraph.RawBulletOffset = paragraph.RawBulletOffset
newParagraph.RawHasBullet = paragraph.RawHasBullet
newParagraph.RawIsBulletHardColor = paragraph.RawIsBulletHardColor
newParagraph.RawIsBulletHardFont = paragraph.RawIsBulletHardFont
newParagraph.RawNumberedBulletStartWith = paragraph.RawNumberedBulletStartWith
newParagraph.RawNumberedBulletStyle = paragraph.RawNumberedBulletStyle
newParagraph.RawPictureBulletId = paragraph.RawPictureBulletId
newParagraph.RawSpaceAfter = paragraph.RawSpaceAfter
newParagraph.RawSpaceBefore = paragraph.RawSpaceBefore
newParagraph.RawSpaceWithin = paragraph.RawSpaceWithin
newParagraph.RawTextOffset = paragraph.RawTextOffset

Return newParagraph
End Function

Public Shared Function ClonePortionStyle(ByVal portion As Aspose.Slides.Portion) As Aspose.Slides.Portion
If portion Is Nothing Then
Throw New System.ArgumentNullException("portion")
End If

Dim newPortion As New Aspose.Slides.Portion

newPortion.AsianOrComplexFontIndex = portion.AsianOrComplexFontIndex
newPortion.Escapement = portion.Escapement
newPortion.FontBold = portion.FontBold
newPortion.FontColor = portion.FontColor
newPortion.FontEmbossed = portion.FontEmbossed
newPortion.FontHeight = portion.FontHeight
newPortion.FontIndex = portion.FontIndex
newPortion.FontItalic = portion.FontItalic
newPortion.FontShadow = portion.FontShadow
newPortion.FontUnderline = portion.FontUnderline
newPortion.SymbolFontIndex = portion.SymbolFontIndex

Return newPortion
End Function

Public Shared Sub ReplaceTokensOnSlide(ByVal slide As Aspose.Slides.Slide, ByVal tokenRegularExpression As System.Text.RegularExpressions.Regex, ByVal tokens As System.Collections.Specialized.StringDictionary)
If slide Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "slide")
End If
If tokenRegularExpression Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokenRegularBLOCKED EXPRESSION
End If
If tokens Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokens")
End If

Dim placeHolderShapes As New System.Collections.ArrayList
Dim placeHolderEnumerator As System.Collections.IDictionaryEnumerator = slide.Placeholders.GetEnumerator
While placeHolderEnumerator.MoveNext()
Dim holder As Aspose.Slides.Placeholder = DirectCast(placeHolderEnumerator.Value, Aspose.Slides.Placeholder)
If holder.GetType.Equals(GetType(Aspose.Slides.TextHolder)) Then
Dim textHolder As Aspose.Slides.TextHolder = DirectCast(holder, Aspose.Slides.TextHolder)
placeHolderShapes.Add(textHolder.ShapeRef)
ReplaceTokensInParagraphs(textHolder.Paragraphs, tokenRegularExpression, tokens)
End If
End While

For Each shape As Aspose.Slides.Shape In slide.Shapes
ReplaceTokensInShape(shape, tokenRegularExpression, tokens, placeHolderShapes)
Next

End Sub

Public Shared Sub ReplaceTokensInParagraphs(ByVal paragraphs As Aspose.Slides.Paragraphs, ByVal tokenRegularExpression As System.Text.RegularExpressions.Regex, ByVal tokens As System.Collections.Specialized.StringDictionary)
If paragraphs Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "paragraphs")
End If
If tokenRegularExpression Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokenRegularBLOCKED EXPRESSION
End If
If tokens Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokens")
End If

Dim cariageReturnChar As System.Char = System.Convert.ToChar(13)
Dim lineFeedChar As System.Char = System.Convert.ToChar(10)
'Iterate through paragraphs
Dim paragraphIndex As System.Int32
While paragraphIndex <= paragraphs.Count - 1
Dim paragraph As Aspose.Slides.Paragraph = paragraphs.Item(paragraphIndex)
Dim paragraphText As String = paragraph.Text

'Iterate over matches in Paragraph text- after each loop as a post condition - there are no more instances of a particular token in the text
If paragraphText <> "" Then
Dim expressionMatch As System.Text.RegularExpressions.Match = tokenRegularExpression.Match(paragraphText)
If expressionMatch.Success AndAlso tokens.ContainsKey(expressionMatch.Value) Then
Dim matchFound As Boolean = True
While matchFound

Dim tokenText As String = expressionMatch.Value

'Get character indices of token position
Dim tokenStartIndex As System.Int32 = paragraphText.IndexOf(expressionMatch.Value)
Dim tokenNameLength As System.Int32 = tokenText.Length

'scroll through potions until we hit start of token
Dim portionContainingTokenEndForMultiLineTokens As Aspose.Slides.Portion
Dim tokenStartPortion As Aspose.Slides.Portion
Dim tokenStartPositionInPortion As System.Int32
Dim portion As Aspose.Slides.Portion
Dim charactersEncountered As System.Int32 = 0
Dim portionIndex As System.Int32 = 0
If tokenStartIndex > 0 Then
While charactersEncountered < tokenStartIndex
portion = paragraph.Portions(portionIndex)
If portion.Text.Length + charactersEncountered <= tokenStartIndex Then
portionIndex += 1
charactersEncountered += portion.Text.Length
If charactersEncountered = tokenStartIndex Then
portion = paragraph.Portions(portionIndex)
End If
Else
Exit While
End If
End While
Else
portion = paragraph.Portions(0)
tokenStartPositionInPortion = 0
End If
tokenStartPortion = portion
Dim portionIndexOfToken As System.Int32 = portionIndex

'get the replacement value
Dim replacementText As String = tokens(tokenText)
replacementText = replacementText.Replace(cariageReturnChar + lineFeedChar, cariageReturnChar.ToString)
replacementText = replacementText.Replace(lineFeedChar.ToString, cariageReturnChar.ToString)
Dim textLines As System.String() = replacementText.Split(cariageReturnChar)
Dim tokenValueLineCount As System.Int32 = textLines.Length

'Replace the token name (or portion of it) with the token value
Dim tokenContainedInPortionText As Boolean
If Not portion.Text.IndexOf(tokenText) >= 0 Then
'Portion does not contain entire token text, need to remove portions that contain rest of token
tokenStartPositionInPortion = tokenStartIndex - charactersEncountered
tokenContainedInPortionText = False
Dim charactersToRemove As System.Int32 = tokenStartIndex + tokenNameLength - charactersEncountered - tokenStartPortion.Text.Length
While charactersToRemove > 0
portion = paragraph.Portions(portionIndexOfToken + 1)
If portion.Text.Length < charactersToRemove Then
charactersToRemove -= portion.Text.Length
paragraph.Portions.RemoveAt(portionIndexOfToken + 1)
Else
If tokenValueLineCount > 1 Then
portionContainingTokenEndForMultiLineTokens = ClonePortionStyle(portion)
portionContainingTokenEndForMultiLineTokens.Text = portion.Text.Substring(charactersToRemove)
paragraph.Portions.RemoveAt(portionIndexOfToken + 1)
Else
portionContainingTokenEndForMultiLineTokens = Nothing
portion.Text = portion.Text.Substring(charactersToRemove)
End If
Exit While
End If
End While
Else
tokenContainedInPortionText = True
End If

Dim endPortionOfText As String
For textLineIndex As System.Int32 = 0 To textLines.Length - 1
If textLineIndex = 0 Then
Dim replacementTextForPortion As String
If tokenContainedInPortionText Then
If tokenValueLineCount > 1 Then
Dim tokenStartIndexInPortion As System.Int32 = tokenStartPortion.Text.IndexOf(tokenText)
endPortionOfText = tokenStartPortion.Text.Substring(tokenStartIndexInPortion + tokenNameLength)
replacementTextForPortion = tokenStartPortion.Text.Substring(0, tokenStartIndexInPortion) + textLines(0)
Else
replacementTextForPortion = tokenStartPortion.Text.Replace(tokenText, textLines(0))
endPortionOfText = Nothing
End If
Else
replacementTextForPortion = tokenStartPortion.Text.Substring(0, tokenStartPositionInPortion) + textLines(0)
End If
tokenStartPortion.Text = replacementTextForPortion
Else
Dim newParagraph As Aspose.Slides.Paragraph = CloneParagraphStyle(paragraph)
Dim newPortion As Aspose.Slides.Portion
newPortion = ClonePortionStyle(tokenStartPortion)
newPortion.Text = textLines(textLineIndex)
If textLineIndex = tokenValueLineCount - 1 AndAlso Not endPortionOfText Is Nothing Then
newPortion.Text += endPortionOfText
newParagraph.Portions.Add(newPortion)
ElseIf textLineIndex = tokenValueLineCount - 1 AndAlso Not portionContainingTokenEndForMultiLineTokens Is Nothing Then
newParagraph.Portions.Add(newPortion)
newParagraph.Portions.Add(portionContainingTokenEndForMultiLineTokens)
Else
newParagraph.Portions.Add(newPortion)
End If
paragraphs.Insert(textLineIndex + paragraphIndex, newParagraph)
End If
Next
If textLines.Length > 1 Then
paragraphIndex += textLines.Length - 1
End If

paragraphText = paragraph.Text
expressionMatch = tokenRegularExpression.Match(paragraphText)
If expressionMatch.Success AndAlso tokens.ContainsKey(expressionMatch.Value) Then
matchFound = True
Else
matchFound = False
End If
End While
End If
End If
paragraphIndex += 1
End While
End Sub

Public Shared Sub ReplaceTokensInShape(ByVal shape As Aspose.Slides.Shape, ByVal tokenRegularExpression As System.Text.RegularExpressions.Regex, ByVal tokens As System.Collections.Specialized.StringDictionary, ByVal placeHolderShapes As System.Collections.ArrayList)
If shape Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "shape")
End If
If tokenRegularExpression Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokenRegularBLOCKED EXPRESSION
End If
If tokens Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "tokens")
End If
If placeHolderShapes Is Nothing Then
Throw New System.ArgumentException("Argument cannot be nothing", "placeHolderShapes")
End If

If shape.GetType.Equals(GetType(Aspose.Slides.GroupShape)) Then
Dim groupShape As Aspose.Slides.GroupShape = DirectCast(shape, Aspose.Slides.GroupShape)
For Each shapeInGroup As Aspose.Slides.Shape In groupShape.Shapes
ReplaceTokensInShape(shapeInGroup, tokenRegularExpression, tokens, placeHolderShapes)
Next
ElseIf shape.GetType.Equals(GetType(Aspose.Slides.Table)) Then
Dim tableShape As Aspose.Slides.Table = DirectCast(shape, Aspose.Slides.Table)
For Each shapeInGroup As Aspose.Slides.Shape In tableShape.Shapes
ReplaceTokensInShape(shapeInGroup, tokenRegularExpression, tokens, placeHolderShapes)
Next
ElseIf Not shape.IsTextHolder AndAlso Not shape.TextFrame Is Nothing Then
ReplaceTokensInParagraphs(shape.TextFrame.Paragraphs, tokenRegularExpression, tokens)
ElseIf shape.IsTextHolder AndAlso Not placeHolderShapes.Contains(shape) AndAlso Not shape.TextFrame Is Nothing Then
ReplaceTokensInParagraphs(shape.TextFrame.Paragraphs, tokenRegularExpression, tokens)
End If
End Sub