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

Need help with pasting HTML to Document

HI,
i'm trying to put some HTML part from a rte editor to my generated word document.

Most of the document ist pasted using MailMerge. At one Point i want to add html data so i got a placeholder there: <<TempPlaceHolderForHTML>>

this is getting my placeholdertext:

Dim selection As TextSelection = doc.Find(New System.Text.RegularExpressions.Regex("<<TempPlaceHolderForHTML>>"))

with this im pasting my html to another document. i thought this would be the best to add the parts to the document:

Private Function HTMLtoRTF(htmltext As String) As EntityCollection
            Dim document As WordDocument = New WordDocument()
            Dim section As IWSection = document.AddSection()
            Dim para As IWParagraph = section.AddParagraph()

            document.XHTMLValidateOption = XHTMLValidationType.Transitional
            section.Body.InsertXHTML(htmltext)
            Return section.ChildEntities
End Function

unfortunately this is not working: 

Dim foo2 As New TextBodyPart(selection)
                Dim blubb = HTMLtoRTF(currentUser.ProfilInteressen.Text)

                For Each fob In blubb
                    foo2.PasteAtEnd(fob)
                Next

Does anyone know hot to continue at this point or is there maybe an easy way im overseeing?

Kind regards,
lc

4 Replies

LC lc August 27, 2015 11:04 AM UTC

i found a solution, but there should be some easier approach to this problem:

Private Function FindCell(doc As WordDocument, searchString As String) As WTableCell
            For Each it As IWSection In doc.Sections
                For Each blu As WTable In it.Tables
                    For Each blu2 As WTableRow In blu.Rows
                        For Each cl As WTableCell In blu2.Cells
                            Dim temp = blu2.Cells.Count
                            For Each pg As WParagraph In cl.Paragraphs
                                If pg.Text.Contains(searchString) Then
                                    Return cl
                                End If
                            Next
                        Next
                    Next
                Next
            Next
            Return Nothing
        End Function


Dim cell = FindCell(doc, "<<TempPlaceHolderForHTML>>")
                If Not IsNothing(cell) Then
                    Dim pgCount = cell.Paragraphs.Count()
                    For i As Integer = pgCount - 1 To 0 Step -1
                        cell.Paragraphs.RemoveAt(i)
                    Next
                    Dim tempParagraph = cell.AddParagraph()
                    tempParagraph.AppendHTML(htmlString)
                End If


DV Divyalakshmi Vasudevan Syncfusion Team August 28, 2015 07:34 AM UTC

Hi IC,

Thank you for your interest in Syncfusion products.


Kindly make use of below code snippet to meet your requirement by using Find and Replace functionality of DocIO .



Example:


'Loads the Word Document

            Dim document As New WordDocument(filename)


            'Find the <<TempPlaceHolderForHTML>> text

            Dim selections As TextSelection() = document.FindAll(New System.Text.RegularExpressions.Regex("<<TempPlaceHolderForHTML>>"))


            For selectionIndex As Integer = 0 To selections.Length - 1

                Dim selection As TextSelection = selections(selectionIndex)

                Dim OwnerParagraph As WParagraph = selection.GetAsOneRange().OwnerParagraph

                'Retrieve the Owner cell

                If TypeOf OwnerParagraph.OwnerTextBody Is WTableCell Then

                    Dim cell As WTableCell = TryCast(OwnerParagraph.OwnerTextBody, WTableCell)

                    'Clear the paragraphs inside the cell

                    cell.ChildEntities.Clear()

                    Dim paragraph As IWParagraph = cell.AddParagraph()

                    'Append the HTML string to the paragraph

                    paragraph.AppendHTML(htmlstring)

                End If

            Next

            'Saves the Word document to disk

            document.Save("Sample.docx")


Kindly refer the below UG documentation link to get more information about Find and Replace:


UGLink:


http://help.syncfusion.com/ug/aspnetmvc/Documents/findandreplace.htm



Please let us know if you have any other questions.


Regards,
Divyalakshmi V



LC lc August 28, 2015 11:49 AM UTC

Hi Divyalakshmi,
thanks this looks much easier than my code. selection.GetAsOneRange().OwnerParagraph was the connection i could not find in the samples.

Regards,
LC


SV Sivasubramani V Syncfusion Team August 31, 2015 07:22 AM UTC

Hi IC,

Thank you for your update.

We have prepared a sample to find and replace the particular content with html string using find and replace functionality of DocIO. Please find the sample from below link

Sample link:

http://www.syncfusion.com/downloads/support/forum/120062/ze/Sample-1088296552

Please let us know if you have any other questions.

Regards,
Siva.



Loader.
Live Chat Icon For mobile
Up arrow icon