DataTplUseCases.vb
''
'' This code is part of GrapeCity Documents for Word samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports System.Data
Imports System.Linq
Imports GrapeCity.Documents.Word

'' This data template sample loads a DOCX that was created in MS Word,
'' And contains a table with data template tags. It then creates a DataSet,
'' loads the GcNwind.xml data base into it, And builds a product list
'' from tables in that data set. The product list Is set as the template
'' data source on the document, And the template Is processed to build
'' the final document with actual data.
Public Class DataTplUseCases
    Public Function CreateDocx(ByRef sampleParams As String()) As GcWordDocument
        Dim doc = New GcWordDocument()

        '' Load the template DOCX
        doc.Load(Path.Combine("Resources", "WordDocs", sampleParams(3)))

        Using ds = New DataSet()
            '' Load the data set:
            ds.ReadXml(Path.Combine("Resources", "data", "GcWordTplDataSet.xml"))

            '' Return the unmodified template document for reference
            If sampleParams.Count() >= 6 AndAlso sampleParams(5) = "template" Then
                Return doc
            End If

            '' Add the data source to the data template data sources
            If sampleParams.Count() >= 5 AndAlso Not String.IsNullOrEmpty(sampleParams(4)) Then
                doc.DataTemplate.DataSources.Add("ds", ds.Tables(sampleParams(4)))
            Else
                doc.DataTemplate.DataSources.Add("ds", ds)
            End If

            '' The document already has all the necessary bindings,
            '' so we only need to process the data template:
            doc.DataTemplate.Process(System.Globalization.CultureInfo.GetCultureInfo("en-US"))
        End Using

        '' Done
        Return doc
    End Function

    Public Function CreateDocx(Optional parsIdx As Integer = 0) As GcWordDocument
        Return CreateDocx(GetSampleParamsList()(parsIdx))
    End Function

    Public Shared Function GetSampleParamsList() As List(Of String())
        '' Mandatory: name, description, info
        '' Custom: template DOCX [[, table] , "template"]
        Return New List(Of String()) From
            {
                New String() {"@use-data-tpl/Lease Agreement", "Generate a lease agreement", Nothing,
                    "Lease_Agreement_Template.docx", "LeaseAgreement"},
                New String() {"@use-data-tpl/Consulting Agreement", "Generate a consultancy agreement", Nothing,
                    "Consulting_Agreement_Template.docx", "ConsAgreement"},
                New String() {"@use-data-tpl/Rental Agreement", "Generate a house rental agreement", Nothing,
                    "House_Rental_Template.docx", "HouseRentalAgreement"},
                New String() {"@use-data-tpl/Employment Contract", "Generate an employment contract", Nothing,
                    "Employment_Contract_Template.docx", "EmploymentContract"},
                New String() {"@use-data-tpl-src/Lease Agreement", "Template Lease_Agreement_Template.docx", Nothing,
                    "Lease_Agreement_Template.docx", Nothing, "template"},
                New String() {"@use-data-tpl-src/Consulting Agreement", "Template Consulting_Agreement_Template.docx", Nothing,
                    "Consulting_Agreement_Template.docx", Nothing, "template"},
                New String() {"@use-data-tpl-src/Rental Agreement", "Template House_Rental_Template.docx", Nothing,
                    "House_Rental_Template.docx", Nothing, "template"},
                New String() {"@use-data-tpl-src/Employment Contract", "Template Employment_Contract_Template.docx", Nothing,
                    "Employment_Contract_Template.docx", Nothing, "template"}
            }
    End Function
End Class