DataTplImage.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports System.Data
  9. Imports System.Linq
  10. Imports System.Globalization
  11. Imports GrapeCity.Documents.Word
  12.  
  13. '' This sample shows how to use the image formatter in a data template.
  14. Public Class DataTplImage
  15. Function CreateDocx() As GcWordDocument
  16. '' The data source.
  17. ''
  18. '' The 'imagePath' yields the absolute path of an image,
  19. '' which Is one of accepted value types for the image formatter.
  20. ''
  21. '' For reference, the following value types are supported by the image formatter:
  22. '' - A byte array containing the image data
  23. '' - A System.IO.Stream object that can be used to read the image data
  24. '' - A System.Drawing.Image object
  25. '' - A string from which an absolute file URI of the image can be created, Or Base64-encoded image data.
  26. Dim data =
  27. {
  28. New With {.name = "Minerva", .imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "minerva.jpg"))},
  29. New With {.name = "Colosseum", .imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "colosseum.jpg"))},
  30. New With {.name = "Venus Felix", .imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "lady.jpg"))}
  31. }
  32.  
  33. Dim doc = New GcWordDocument()
  34.  
  35. '' Add the data source to the data template data sources
  36. '' (note that in this release, only one data source can be added):
  37. doc.DataTemplate.DataSources.Add("ds", data)
  38.  
  39. '' The single paragraph added to the document contains template tags
  40. '' - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source
  41. '' - {{ds.name}} -- fetches the image name
  42. '' - {{ds.imagePath}:image(266,400)} -- fetches the image file, resizes the image to specified size.
  43. Dim p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.name}}:" + vbCrLf + "{{ds.imagePath}:image(266,400)}{{/ds}}")
  44.  
  45. '' This call expands all data templates in the document,
  46. '' replacing template tags with data (iterating over all data items):
  47. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
  48.  
  49. '' Done
  50. Return doc
  51. End Function
  52. End Class
  53.