SupportedBarcodes.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.Drawing;
  8. using System.Collections.Generic;
  9. using GrapeCity.Documents.Drawing;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Barcode;
  13.  
  14. namespace DsPdfWeb.Demos.Barcodes
  15. {
  16. // Renders samples of all barcode symbologies supported by the DsBarcode library.
  17. public class SupportedBarcodes
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. // Simple layout parameters:
  22. const float margin = 72 / 2;
  23. const float pad = 4;
  24. const float gap = 10;
  25. //
  26. var doc = new GcPdfDocument();
  27. Page page;
  28. GcGraphics g;
  29. PointF ip;
  30. // This will add a new page and set the insertion point initial position:
  31. void newPage()
  32. {
  33. page = doc.NewPage();
  34. g = page.Graphics;
  35. ip = new PointF(margin, margin);
  36. }
  37. newPage();
  38. // Text formats for showing barcode names and values:
  39. var tfCaption = new TextFormat()
  40. {
  41. Font = StandardFonts.Times,
  42. FontSize = 12,
  43. };
  44. var tfBarcode = new TextFormat()
  45. {
  46. Font = StandardFonts.Helvetica,
  47. FontSize = 9,
  48. };
  49. // The barcode instance to be used to draw all symbologies,
  50. // here we set up its properties that won't change:
  51. var barcode = new GcBarcode()
  52. {
  53. TextFormat = tfBarcode,
  54. ScaleFactor = 1.5f,
  55. };
  56. barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below;
  57. barcode.Options.SizeOptions.NarrowWideRatio = 0;
  58. // Draw a barcode with the specified symbology (code type) and optional CC-A:
  59. void drawBarcode(CodeType symbology, string value, string cca)
  60. {
  61. // Set up the barcode symbology, value and other properties:
  62. var caption = $"{symbology}:\r\n{value}";
  63. if (string.IsNullOrEmpty(cca))
  64. barcode.Options.GS1Composite.Type = GS1CompositeType.None;
  65. else
  66. {
  67. // Composite Component A (CC-A) specified:
  68. caption += $"\r\nDependent CCA: {cca}";
  69. barcode.Options.GS1Composite.Type = GS1CompositeType.CCA;
  70. barcode.Options.GS1Composite.Value = cca;
  71. }
  72. // Not all barcodes support checksums:
  73. barcode.Options.CheckSumEnabled = symbology != CodeType.Code25intlv && symbology != CodeType.Code_2_of_5 && symbology != CodeType.Matrix_2_of_5;
  74. var csize = g.MeasureString(caption, tfCaption);
  75. barcode.CodeType = symbology;
  76. barcode.Text = value;
  77. var size = g.MeasureBarcode(barcode);
  78. size.Height = Math.Max(size.Height, csize.Height);
  79. var border = new RectangleF(ip, new SizeF(page.Size.Width - margin * 2, size.Height + pad * 2));
  80. // Add new pages as needed:
  81. if (ip.Y + border.Height > page.Size.Height - margin)
  82. {
  83. newPage();
  84. border = new RectangleF(ip, border.Size);
  85. }
  86. // Draw the barcode:
  87. g.DrawRectangle(border, Color.Gray);
  88. g.DrawString(caption, tfCaption, new PointF(border.Left + pad, border.Top + pad));
  89. g.DrawBarcode(barcode, new RectangleF(border.Right - size.Width - pad, border.Top + pad, size.Width, size.Height));
  90. ip.Y = border.Bottom + gap;
  91. }
  92. // Draw all supported symbologies:
  93. drawBarcode(CodeType.Ansi39, "*DSBARCODE*", null);
  94. drawBarcode(CodeType.Ansi39x, "*DsPdf*", null);
  95. drawBarcode(CodeType.Codabar, "A12041961D", null);
  96. drawBarcode(CodeType.Code25intlv, "1234567890", null); //#2 Interleaved 2 of 5 (ITF)
  97. drawBarcode(CodeType.Code39, "*DSBARCODE*", null);
  98. drawBarcode(CodeType.Code39x, "*DsPdf*", null);
  99. drawBarcode(CodeType.Code49, "DsBarcode+DsPdf", null);
  100. drawBarcode(CodeType.Code93x, "DsBarcode+DsPdf", null);
  101. drawBarcode(CodeType.Code_93, "DSBARCODE", null);
  102. drawBarcode(CodeType.Code_128_A, "DSPDF-2023", null);
  103. drawBarcode(CodeType.Code_128_B, "DsPdf-2023", null);
  104. drawBarcode(CodeType.Code_128_C, "1234567890", null);
  105. drawBarcode(CodeType.Code_128auto, "DsPdf-2023", null);
  106. drawBarcode(CodeType.Code_2_of_5, "1234567890", null);
  107. drawBarcode(CodeType.DataMatrix, "DsBarcode+DsPdf", null);
  108. drawBarcode(CodeType.QRCode, "DsBarcode+DsPdf", null);
  109. drawBarcode(CodeType.EAN_8, "1234567", null);
  110. drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null);
  111. drawBarcode(CodeType.EAN128FNC1, "DsBarcode\nDsPdf", null);
  112. drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null);
  113. drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", null);
  114. drawBarcode(CodeType.PostNet, "152063949", null);
  115. drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null);
  116. drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null);
  117. drawBarcode(CodeType.MSI, "1234567890", null);
  118. drawBarcode(CodeType.MicroPDF417, "DsPdf", null);
  119. drawBarcode(CodeType.Pdf417, "DsPdf", null);
  120. drawBarcode(CodeType.RSS14, "1234567890", null);
  121. drawBarcode(CodeType.RSS14Stacked, "1234567890", null);
  122. drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345");
  123. drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null);
  124. drawBarcode(CodeType.RSS14Truncated, "1234567890", null);
  125. drawBarcode(CodeType.RSSExpanded, "12345678901234", null);
  126. drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null);
  127. drawBarcode(CodeType.RSSLimited, "1234567890", null);
  128. drawBarcode(CodeType.RSSLimited, "1234567890", "12345");
  129. drawBarcode(CodeType.UCCEAN128, "DsBarcode+DsPdf", null);
  130. drawBarcode(CodeType.UPC_A, "123456789012", null);
  131. drawBarcode(CodeType.UPC_E0, "123456789012", null);
  132. drawBarcode(CodeType.UPC_E1, "123456789012", null);
  133. // Save the PDF:
  134. doc.Save(stream);
  135. return doc.Pages.Count;
  136. }
  137. }
  138. }
  139.