We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Replacing a string with a paragraph

I need to find a string in my document and replace that string with a paragraph.  The paragraph also contains html.

here is the code I am using,  the replaced item in the document only includes the first line from the paragraph.

--start of code (vb.net)---------------------------------------------------
 Dim s As New WSection(document)
 Dim p As WParagraph = s.AddParagraph
 document.XHTMLValidateOption = XHTMLValidationType.Transitional
 p.AppendHTML(myHTMLstring)
 Dim tbpHTML As TextBodyPart
 tbpHTML = New TextBodyPart(document)
 tbpHTML.BodyItems.Add(p)
document.Replace("~html~", tbpHTML, True, True)
--end of code-------------------------------------------------------------

the field myHTMLstring contains this html string....

<b>Some line as a heading</b><br />
<ul>
 <li>the first bulleted item</li>
 <li>the second bulleted item</li>
 <li>the last bulleted item</li>
</ul>
-----------------------------------------------------------------
The replaced string in the document only contains:  
Some line as a heading

the rest of the html string is missing....how can I achieve this?

Thank you

5 Replies

RM Ramkumar M Syncfusion Team September 20, 2012 08:39 AM UTC

Hi Todd,

Thank you for your interest in Syncfusion products.

Appending html with list tag to a paragraph will create new paragraphs for those lists. So that we have to replace string by all those paragraphs. For your reference we have modified your code snippets. Please try this code and let us if this helps you.

  Dim s As New WSection(document)

  Dim p As WParagraph = s.AddParagraph

  document.XHTMLValidateOption = XHTMLValidationType.Transitional

  p.AppendHTML(myHTMLstring)

  Dim tbpHTML As TextBodyPart

  tbpHTML = New TextBodyPart(document)

  For index As Integer = 0 To s.Body.ChildEntities.Count - 1

      tbpHTML.BodyItems.Add(s.Body.ChildEntities(0))

  Next

  document.Replace("~html~", tbpHTML, True, True)

 

Please let us know if you have any questions.

Regards

Ramkumar



TJ Todd J September 20, 2012 02:32 PM UTC

that works great...thank you very much!

Are they other tags besides the list tag that I will have to perform special processing for?


RM Ramkumar M Syncfusion Team September 26, 2012 10:18 AM UTC

Hi Todd,

Thank you for your updated.

Regarding your question about processing other tags:

No need to process each tag individually, the code snippets that I have provided in my last update will process all kinds of tags. For your reference, please find the code snippets below.

Code snippets:

  Dim s As New WSection(document)

  Dim p As WParagraph = s.AddParagraph

  document.XHTMLValidateOption = XHTMLValidationType.Transitional

  p.AppendHTML(myHTMLstring)

  Dim tbpHTML As TextBodyPart

  tbpHTML = New TextBodyPart(document)

  For index As Integer = 0 To s.Body.ChildEntities.Count - 1

      tbpHTML.BodyItems.Add(s.Body.ChildEntities(0))

  Next

  document.Replace("~html~", tbpHTML, True, True)

 

Please let us know if you have any questions.

 

Regards

Ramkumar



TJ Todd J October 16, 2012 09:36 PM UTC

OK, now I have another challenge with this...It does work for me based on your code change.
But I am now trying to appendhtml into a table cell.  
Here is my code snippet: 

myHtmlString looks like this
<b>Item Highlights</b>
<ul>
<li>Clear Case</li>
<li>Blank Sheet</li>
<li>Heavy Duty</li>
</ul>

here is a scaled down version of the sub routine that I call to append the html.

private sub FormatHtml(byval myHtmlString as string, byRef cell As WTableCell)
    cell.Document.XHTMLValidateOption = XHTMLValidationType.Transitional
    cell.AddParagraph.AppendHTML(myHtmlString)
end sub

this does work but the spacing is too big, see attached example output file.

The spacing before and after the bullets is too big.  any way to reduce this?



append html in subtable_d0e4891f.zip


RM Ramkumar M Syncfusion Team October 19, 2012 11:58 AM UTC

Hi Todd,
Thank you for your update.
Please use the below code snippets to remove the before and after spacing of paragraphs present in the cell and let us know if this helps you.
Code snippets:
        Private Sub FormatHtml(ByVal myHtmlString As String, ByRef cell As WTableCell)
            cell.Document.XHTMLValidateOption = XHTMLValidationType.Transitional
            cell.AddParagraph.AppendHTML(myHtmlString)
            RemoveParagraphSpacing(cell)
        End Sub
 
        Private Sub RemoveParagraphSpacing(cell As WTableCell)
            For Each item As Entity In cell.ChildEntities
                Dim para As WParagraph = DirectCast(item, WParagraph)
                para.ParagraphFormat.SpaceAfterAuto = False
                para.ParagraphFormat.SpaceBeforeAuto = False
                para.ParagraphFormat.BeforeSpacing = 0
                para.ParagraphFormat.AfterSpacing = 0
            Next
        End Sub
 
Please let us know if you have any questions
Regards
Ramkumar

Loader.
Live Chat Icon For mobile
Up arrow icon