DataTplFixPbbNotInCell.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Globalization
Imports GrapeCity.Documents.Word

'' This example shows how to deal with the
'' pbb (paragraph-block-behavior) template formatter
'' should start and end in the same cell error.
Public Class DataTplFixPbbNotInCell
    '' Code demonstrating the problem:
    Private Function Problem() As GcWordDocument
        Using oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"))
            Dim doc = New GcWordDocument()
            doc.DataTemplate.DataSources.Add("ds", oceans)
            '' Define a 2x1 table:
            Dim table = doc.Body.Tables.Add(2, 1, doc.Styles(BuiltInStyleId.GridTable1Light))
            Dim cell_00 = table(0, 0)
            Dim cell_01 = table(0, 1)
            '' Put the start of the pbb range in the cell [0,0]:
            cell_00.GetRange().Paragraphs.Add("{{#ds.seas}:pbb()}{{ds.seas.name}} ")
            '' Put the end of the pbb range in the cell [1,1]:
            cell_01.GetRange().Paragraphs.Add("{{/ds.seas}}")
            '' Incorrect: a pbb range starts in one cell and ends in another:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
            Return doc
        End Using
    End Function

    '' Code demonstrating the fix:
    Private Function Fix() As GcWordDocument
        Using oceans = File.OpenRead(Path.Combine("Resources", "data", "oceans.json"))
            Dim doc = New GcWordDocument()
            doc.DataTemplate.DataSources.Add("ds", oceans)
            '' Define a 2x1 table:
            Dim table = doc.Body.Tables.Add(2, 1, doc.Styles(BuiltInStyleId.GridTable1Light))
            Dim cell_00 = table(0, 0)
            '' Put the start of the pbb range in the cell [0,0]:
            cell_00.GetRange().Paragraphs.Add("{{#ds.seas}:pbb()}{{ds.seas.name}} ")
            '' Put the end of the pbb range in the same cell [0,0]:
            cell_00.GetRange().Paragraphs.Add("{{/ds.seas}}")
            '' Correct: a pbb range starts and ends in the same table cell:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
            Return doc
        End Using
    End Function

    Function CreateDocx() As GcWordDocument
        Dim doc As GcWordDocument
        Try
            '' This fails:
            doc = Problem()
        Catch ex As Exception
            '' This works:
            doc = Fix()
            '' Insert a brief explanation of the problem and the fix into the generated document:
            doc.Body.Paragraphs.Insert(
                $"The error ""{ex.Message}"" occurred because a pbb (paragraph-block-behavior) formatter " +
                $"started in one table cell and ended in another. A pbb formatter must start and end in the same table cell.",
                doc.Styles(BuiltInStyleId.BlockText),
                InsertLocation.Start)
        End Try
        Return doc
    End Function
End Class