'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.DataImportsSystem.GlobalizationImportsSystem.IOImportsSystem.LinqImportsGrapeCity.Documents.Word '' This data template sample prints an invoice containing the list'' of products in the purchase order.'' The invoice data is generated for a randomly selected'' order from the sample DsNWind database.PublicClassDataTplOrderInvoiceFunctionCreateDocx(Optional idx AsInteger = 0) AsGcWordDocumentUsing ds = NewDataSet()'' Load the sample database, fetch a random supplier'' and a random order from that supplier: ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml")) '' Database tables used by the invoice:Dim dtSuppliers = ds.Tables("Suppliers")Dim dtOrders = ds.Tables("OrdersCustomersEmployees")Dim dtOrdersDetails = ds.Tables("EmployeesProductsOrders") '' Collect order data:Dim random = Util.NewRandom() Dim fetchedIndex = random.Next(dtSuppliers.Select().Count())Dim supplier = dtSuppliers.Select() _ .Skip(fetchedIndex).Take(1) _ .Select(Function(it) NewWith { .SupplierID = Convert.ToInt32(it("SupplierID")), .CompanyName = it("CompanyName").ToString(), .ContactName = it("ContactName").ToString(), .ContactTitle = it("ContactTitle").ToString(), .Address = it("Address").ToString(), .City = it("City").ToString(), .Region = it("Region").ToString(), .PostalCode = it("PostalCode").ToString(), .Country = it("Country").ToString(), .Phone = it("Phone").ToString(), .Fax = it("Fax").ToString(), .HomePage = it("HomePage").ToString() }).FirstOrDefault() fetchedIndex = random.Next(dtOrders.Select().Count())Dim order = dtOrders.Select() _ .Skip(fetchedIndex).Take(1) _ .Select(Function(it) NewWith { .OrderDate = it("OrderDate"), .OrderID = Convert.ToInt32(it("OrderID")), .CompanyName = it("CompanyName").ToString(), .Name = $"{it("FirstName")}{it("LastName")}", .Address = $"{it("ShipAddress")}," & vbLf & $"{it("ShipCity")}{it("ShipRegion")}{it("ShipPostalCode")}{it("ShipCountry")}", .Email = GetEmail(it("FirstName").ToString(), it("LastName").ToString(), it("CompanyName").ToString()) }).FirstOrDefault() Dim orderDetails = dtOrdersDetails.Select() _ .Select(Function(it) NewWith { .OrderID = Convert.ToInt32(it("OrderID")), .ProductName = it("ProductName").ToString(), .UnitPrice = Convert.ToDecimal(it("UnitPrice")), .Quantity = Convert.ToDecimal(it("Quantity")), .Total = Convert.ToDecimal(it("UnitPrice")) * Convert.ToDecimal(it("Quantity")) }) _ .Where(Function(it) it.OrderID = order.OrderID) _ .OrderBy(Function(it) it.ProductName).ToList() '' Finally, prep the integrated data source for the template:Dim data = NewWith { .o = order, .ps = orderDetails, .total = orderDetails.Sum(Function(od_) od_.Total) } '' Load the template DOCX, add the data source and process the template:Dim doc = NewGcWordDocument() doc.Load(Path.Combine("Resources", "WordDocs", "InvoiceTemplate.docx")) doc.DataTemplate.DataSources.Add("ds", data) doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US")) '' Done:Return docEndUsingEndFunction '' Generate a sample email address from other data:PrivateFunctionGetEmail(firstName AsString, lastName AsString, companyName AsString) AsStringDim x = NewString(companyName.ToLower().Where(Function(c_) Char.IsLetterOrDigit(c_)).ToArray())Return$"{firstName.ToLower()}.{Char.ToLower(lastName(0))}@{x}.com"EndFunction PublicSharedFunctionGetSampleParamsList() As List(OfString())ReturnNew List(OfString())() From {NewString() {"@data-templates/Order Invoice", "Generate the invoice for a random order fetched from a database", Nothing},NewString() {"@use-data-tpl/Order Invoice", "Generate the invoice for a random order fetched from a database", Nothing} }EndFunctionEndClass