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