Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq
Imports System.Windows.Forms
Imports System.Windows.Media
Imports Telerik.Windows.Documents.Flow.FormatProviders.Docx
Imports Telerik.Windows.Documents.Flow.Model
Imports Telerik.Windows.Documents.Flow.Model.Editing
Imports Telerik.Windows.Documents.Flow.Model.Styles
Imports Telerik.Windows.Documents.Spreadsheet.Model

Namespace WordsProcessingBlogPart2
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles radButton1.Click
            '			#Region "Import and Clone"

            Dim source As RadFlowDocument
            Using fs As FileStream = New FileStream("..\..\output.docx", FileMode.Open)
                Dim provider As DocxFormatProvider = New DocxFormatProvider()
                source = provider.Import(fs)
            End Using

            Dim document As RadFlowDocument = New RadFlowDocument()
            document = source.Clone()

            Dim editor As RadFlowDocumentEditor = New RadFlowDocumentEditor(document)

            '			#End Region

            '			#Region "Styles"

            Dim paragaphStyle As Style = New Style("CompetenciesRequired", StyleType.Paragraph)
            paragaphStyle.Name = "CompetenciesRequiredStyle"

            paragaphStyle.CharacterProperties.FontFamily.LocalValue = New ThemableFontFamily("Constantia")
            paragaphStyle.CharacterProperties.FontSize.LocalValue = 22

            paragaphStyle.CharacterProperties.ForegroundColor.LocalValue = New ThemableColor(Colors.DarkSlateBlue)

            document.StyleRepository.Add(paragaphStyle)

            For Each paragraph As Paragraph In document.Sections(0).EnumerateChildrenOfType(Of Paragraph)()
                If paragraph.ListLevel < 3 Then
                    paragraph.StyleId = paragaphStyle.Id
                End If
            Next paragraph

            Dim tableStyle As Style = New Style("TableStyle", StyleType.Table)
            tableStyle.Name = "Table Style"
            Dim tableBorder As Border = New Border(2, Telerik.Windows.Documents.Flow.Model.Styles.BorderStyle.Wave, New ThemableColor(Colors.DarkSlateGray))
            tableStyle.TableProperties.Borders.LocalValue = New TableBorders(tableBorder)

            document.StyleRepository.Add(tableStyle)

            For Each table As Table In document.Sections(0).EnumerateChildrenOfType(Of Table)()
                table.StyleId = tableStyle.Id
            Next table

            '			#End Region          

            '			#Region "Comment and Bookmark"

            For Each item As DocumentElementBase In document.Sections(0).Blocks
                Dim paragraph = TryCast(item, Paragraph)
                If Not paragraph Is Nothing AndAlso paragraph.Inlines.Count > 0 Then
                    If TypeOf paragraph.Inlines(0) Is Run Then
                        If (CType(paragraph.Inlines(0), Run)).Text.Contains("ASP.NET") AndAlso paragraph.ListLevel = 4 Then
                            editor.MoveToParagraphStart(paragraph)
                            editor.MoveToInlineEnd(paragraph.Inlines(0))
                            editor.InsertComment("At least 2 years of experience is required", paragraph.Inlines(0), paragraph.Inlines(0))
                        ElseIf (CType(paragraph.Inlines(0), Run)).Text.Contains("HTML") AndAlso paragraph.ListLevel = 4 Then
                            editor.MoveToParagraphStart(paragraph)

                            Dim bookmark As Bookmark = New Bookmark(document, "HTML")

                            paragraph.Inlines.Insert(0, bookmark.BookmarkRangeStart)
                            paragraph.Inlines.Add(bookmark.BookmarkRangeEnd)
                        End If
                    End If
                End If
            Next item

            '			#End Region

            Using output As Stream = New FileStream("..\..\editedDoc.docx", FileMode.Create)
                Dim provider As DocxFormatProvider = New DocxFormatProvider()
                provider.Export(document, output)
            End Using

            Process.Start("..\..\editedDoc.docx")
            radLabel1.Text = "File saved to: " & Path.GetFullPath("..\..\editedDoc.docx")
        End Sub
    End Class
End Namespace