ShippingLabels.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Pdf
  10. Imports GrapeCity.Documents.Text
  11. Imports GrapeCity.Documents.Barcode
  12. Imports GCTEXT = GrapeCity.Documents.Text
  13. Imports GCDRAW = GrapeCity.Documents.Drawing
  14.  
  15. '' This sample prints a set of shipping labels that include some barcodes.
  16. '' Note also the use of tab stops to vertically align data.
  17. Public Class ShippingLabels
  18. '' Client info. This sample prints one shipping label for each client.
  19. Private Class Client
  20. Public Property Name As String
  21. Public Property Addr As String
  22. Public Property City As String
  23. Public Property Country As String
  24. Sub New(ByVal name_ As String, ByVal addr_ As String, ByVal city_ As String, ByVal country_ As String)
  25. Name = name_
  26. Addr = addr_
  27. City = city_
  28. Country = country_
  29. End Sub
  30. End Class
  31.  
  32. '' The clients base.
  33. Shared ReadOnly s_clients As New List(Of Client) From
  34. {
  35. New Client("Simons bistro", "Vinb�ltet 34", "K�benhavn", "Denmark"),
  36. New Client("Richter Supermarkt", "Starenweg 5", "Gen�ve", "Switzerland"),
  37. New Client("Bon app'", "12, rue des Bouchers", "Marseille", "France"),
  38. New Client("Rattlesnake Canyon Grocery", "2817 Milton Dr.", "Albuquerque", "USA"),
  39. New Client("Lehmanns Marktstand", "Magazinweg 7", "Frankfurt a.M.", "Germany"),
  40. New Client("LILA-Supermercado", "Carrera 52 con Ave. Bol�dim #65-98 Llano Largo", "Barquisimeto", "Venezuela"),
  41. New Client("Ernst Handel", "Kirchgasse 6", "Graz", "Austria"),
  42. New Client("Pericles Comidas cl�sicas", "Calle Dr. Jorge Cash 321", "M�xico D.F.", "Mexico"),
  43. New Client("Drachenblut Delikatessen", "Walserweg 21", "Aachen", "Germany"),
  44. New Client("Queen Cozinha", "Alameda dos Can�rios, 891", "S�o Paulo", "Brazil"),
  45. New Client("Tortuga Restaurante", "Avda. Azteca 123", "M�xico D.F.", "Mexico"),
  46. New Client("Save-a-lot Markets", "187 Suffolk Ln.", "Boise", "USA"),
  47. New Client("Franchi S.p.A.", "Via Monte Bianco 34", "Torino", "Italy")
  48. }
  49.  
  50. '' Consts and vars used to render the labels:
  51. Const hmargin = 24.0F, vmargin = 36.0F
  52. Dim _labelWidth, _labelHeight As Single
  53. Dim _pBold, _pNorm As GCDRAW.Pen
  54. Dim _fontReg, _fontBold As GCTEXT.Font
  55. Dim _tfSmall, _tfSmallB, _tfLarge As TextFormat
  56. Dim _tsHeader, _tsFrom, _tsCodes As List(Of TabStop)
  57. Dim _logo As GCDRAW.Image
  58. Dim _ia As ImageAlign
  59. Dim bcTop, bcBottom As GcBarcode
  60.  
  61. '' The main sample driver.
  62. Function CreatePDF(ByVal stream As Stream) As Integer
  63. Dim doc = New GcPdfDocument()
  64. Init(doc)
  65. '' Loop over clients, print up to 4 labels per page:
  66. Dim i = 0
  67. For pg = 0 To ((s_clients.Count + 3) / 4) - 1
  68. Dim page = doc.NewPage()
  69. PrintLabel(s_clients(i), page, New RectangleF(hmargin, vmargin, _labelWidth, _labelHeight))
  70. i += 1
  71. If (i < s_clients.Count) Then
  72. PrintLabel(s_clients(i), page, New RectangleF(hmargin + _labelWidth, vmargin, _labelWidth, _labelHeight))
  73. i += 1
  74. End If
  75. If (i < s_clients.Count) Then
  76. PrintLabel(s_clients(i), page, New RectangleF(hmargin, vmargin + _labelHeight, _labelWidth, _labelHeight))
  77. i += 1
  78. End If
  79. If (i < s_clients.Count) Then
  80. PrintLabel(s_clients(i), page, New RectangleF(hmargin + _labelWidth, vmargin + _labelHeight, _labelWidth, _labelHeight))
  81. i += 1
  82. End If
  83. Next
  84. '' Done:
  85. doc.Save(stream)
  86. Term()
  87. Return doc.Pages.Count
  88. End Function
  89.  
  90. '' Init variables used to render labels:
  91. Private Sub Init(ByVal doc As GcPdfDocument)
  92. _labelWidth = (doc.PageSize.Width - hmargin * 2) / 2
  93. _labelHeight = _labelWidth
  94. _pBold = New GCDRAW.Pen(Color.Black, 2)
  95. _pNorm = New GCDRAW.Pen(Color.Black, 0.5F)
  96. _logo = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "AcmeLogo-vertical-250px.png"))
  97. _fontReg = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"))
  98. _fontBold = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuib.ttf"))
  99. _tfSmall = New TextFormat() With {.Font = _fontReg, .FontSize = 8}
  100. _tfSmallB = New TextFormat() With {.Font = _fontBold, .FontSize = 8}
  101. _tfLarge = New TextFormat() With {.Font = _fontBold, .FontSize = 14}
  102.  
  103. _ia = New ImageAlign(ImageAlignHorz.Right, ImageAlignVert.Center, True, True, True, False, False)
  104. _tsHeader = New List(Of TabStop) From {New TabStop(24, TabStopAlignment.Leading), New TabStop(108, TabStopAlignment.Leading)}
  105. _tsFrom = New List(Of TabStop) From {New TabStop(12, TabStopAlignment.Leading), New TabStop(72, TabStopAlignment.Leading)}
  106. _tsCodes = New List(Of TabStop) From {New TabStop(_labelWidth / 8, TabStopAlignment.Center)}
  107.  
  108. bcTop = New GcBarcode() With {
  109. .TextFormat = _tfSmall,
  110. .CodeType = CodeType.Code_128_B,
  111. .HorizontalAlignment = ImageAlignHorz.Center,
  112. .VerticalAlignment = ImageAlignVert.Center
  113. }
  114. bcTop.Options.CaptionPosition = BarCodeCaptionPosition.Below
  115. bcBottom = New GcBarcode() With {
  116. .TextFormat = _tfSmall,
  117. .CodeType = CodeType.Code_128auto,
  118. .HorizontalAlignment = ImageAlignHorz.Center,
  119. .VerticalAlignment = ImageAlignVert.Center
  120. }
  121. bcBottom.Options.CaptionPosition = BarCodeCaptionPosition.Below
  122. End Sub
  123.  
  124. Private Sub Term()
  125. _logo.Dispose()
  126. End Sub
  127.  
  128. Private Sub PrintLabel(ByVal client As Client, ByVal page As Page, ByVal bounds As RectangleF)
  129. '' Used to randomize some sample data:
  130. Dim rnd = Util.NewRandom()
  131.  
  132. '' Sample shipper/sender data:
  133. Dim shipper = If(rnd.Next(2) = 0, "United Package", "Speedy Express")
  134.  
  135. Dim sender = New With {
  136. .name = "ACME Inc.", .addr = "1 Main Street", .city = "Metropolis", .country = "USA", .zip = "34567"
  137. }
  138.  
  139. Dim shuttle = rnd.Next(10000, 15000).ToString()
  140.  
  141. Dim area = rnd.Next(1, 12).ToString()
  142. Dim tour = rnd.Next(0, 20).ToString()
  143.  
  144. Dim g = page.Graphics
  145.  
  146. Dim tl = g.CreateTextLayout()
  147. tl.DefaultFormat.Font = _tfSmall.Font
  148. tl.DefaultFormat.FontSize = _tfSmall.FontSize
  149. tl.LineSpacingScaleFactor = 0.75F
  150. tl.ParagraphAlignment = ParagraphAlignment.Center
  151.  
  152. '' Header
  153. Dim hHeader = bounds.Height / 2 / 5
  154. Dim rHeader = New RectangleF(bounds.X, bounds.Y, bounds.Width, hHeader)
  155. tl.TabStops = _tsHeader
  156. tl.Append($"{vbTab}{shipper}")
  157. tl.Append($"{vbTab}CMR", _tfSmallB)
  158. tl.Append($"{vbLf}{vbTab}{vbTab}{Util.TimeNow():yyyy-MM-dd}")
  159. tl.MaxHeight = rHeader.Height
  160. tl.MaxWidth = rHeader.Width
  161. tl.PerformLayout(True)
  162. g.DrawTextLayout(tl, rHeader.Location)
  163.  
  164. Dim rLogo = rHeader
  165. rLogo.Inflate(-5, -5)
  166. g.DrawImage(_logo, rLogo, Nothing, _ia)
  167.  
  168. '' From
  169. Dim hFrom = hHeader
  170. Dim rFrom = New RectangleF(bounds.X, rHeader.Bottom, bounds.Width, hFrom)
  171. tl.Clear()
  172. tl.TabStops = _tsFrom
  173. tl.Append($"{vbTab}From:{vbTab}{sender.name}{vbLf}{vbTab}{vbTab}{sender.addr}{vbLf}{vbTab}{vbTab}{sender.city}, {sender.country} {sender.zip}")
  174. tl.MaxHeight = rFrom.Height
  175. tl.MaxWidth = rFrom.Width
  176. tl.PerformLayout(True)
  177. g.DrawTextLayout(tl, rFrom.Location)
  178.  
  179. '' To
  180. Dim hTo = bounds.Height / 2 / 3
  181. Dim rTo = New RectangleF(bounds.X, rFrom.Bottom, bounds.Width, hTo)
  182. tl.Clear()
  183. tl.TabStops = _tsFrom
  184. tl.Append($"{vbTab}To:{vbTab}{client.Name}{vbLf}{vbTab}{vbTab}{client.Addr}{vbLf}{vbTab}{vbTab}{client.City}{vbLf}{vbTab}{vbTab}{client.Country}")
  185. tl.MaxHeight = rTo.Height
  186. tl.MaxWidth = rTo.Width
  187. tl.PerformLayout(True)
  188. g.DrawTextLayout(tl, rTo.Location)
  189.  
  190. '' Codes
  191. Dim hCodes = bounds.Height / 2 / (15.0F / 4)
  192. Dim rCodes = New RectangleF(bounds.X, rTo.Bottom, bounds.Width, hCodes)
  193. tl.TabStops = _tsCodes
  194. tl.Clear()
  195. tl.AppendLine($"{vbTab}Shuttle")
  196. tl.Append($"{vbTab}{shuttle}", _tfLarge)
  197. tl.MaxHeight = rCodes.Height
  198. tl.MaxWidth = rCodes.Width / 4
  199. tl.PerformLayout(True)
  200. g.DrawTextLayout(tl, rCodes.Location)
  201.  
  202. tl.Clear()
  203. tl.AppendLine($"{vbTab}Area")
  204. tl.Append($"{vbTab}{area}", _tfLarge)
  205. tl.PerformLayout(True)
  206. g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4, rCodes.Y))
  207.  
  208. tl.Clear()
  209. tl.AppendLine($"{vbTab}Exception")
  210. tl.Append($"{vbTab} ", _tfLarge)
  211. tl.PerformLayout(True)
  212. g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4 * 2, rCodes.Y))
  213.  
  214. tl.Clear()
  215. tl.AppendLine($"{vbTab}Tour")
  216. tl.Append($"{vbTab}{tour}", _tfLarge)
  217. tl.PerformLayout(True)
  218. g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4 * 3, rCodes.Y))
  219.  
  220. '' Barcodes
  221. Dim hBarcodes = bounds.Height / 2
  222. Dim rBcTop = New RectangleF(bounds.X, rCodes.Bottom, bounds.Width, hBarcodes / 2)
  223. Dim rBcBottom = New RectangleF(bounds.X, rBcTop.Bottom, bounds.Width, hBarcodes / 2)
  224.  
  225. bcTop.Text = client.Country
  226. g.DrawBarcode(bcTop, rBcTop)
  227.  
  228. '' Make up a longish 'code':
  229. Dim code = $"{client.Name(0)}{client.Addr(0)}{client.City(0)}{client.Country(0)}".ToUpper()
  230. bcBottom.Text = $"{code}{client.GetHashCode().ToString("X12")}"
  231. g.DrawBarcode(bcBottom, rBcBottom)
  232.  
  233. '' Lines:
  234. g.DrawLine(rHeader.Left, rHeader.Bottom, rHeader.Right, rHeader.Bottom, _pNorm)
  235. g.DrawLine(rFrom.Left, rFrom.Bottom, rFrom.Right, rFrom.Bottom, _pNorm)
  236. g.DrawLine(rTo.Left, rTo.Bottom, rTo.Right, rTo.Bottom, _pNorm)
  237. g.DrawLine(rCodes.Left, rCodes.Bottom, rCodes.Right, rCodes.Bottom, _pNorm)
  238.  
  239. g.DrawLine(rCodes.Left + rCodes.Width / 4, rCodes.Top, rCodes.Left + rCodes.Width / 4, rCodes.Bottom, _pNorm)
  240. g.DrawLine(rCodes.Left + rCodes.Width / 4 * 2, rCodes.Top, rCodes.Left + rCodes.Width / 4 * 2, rCodes.Bottom, _pNorm)
  241. g.DrawLine(rCodes.Left + rCodes.Width / 4 * 3, rCodes.Top, rCodes.Left + rCodes.Width / 4 * 3, rCodes.Bottom, _pNorm)
  242.  
  243. g.DrawRectangle(bounds, _pBold)
  244. End Sub
  245. End Class
  246.