Renders samples of all barcode symbologies supported by the DsBarcode library

PDF TIFF SVG JPG C# VB
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. var doc = new GcPdfDocument();
  22. Page page = null;
  23. GcGraphics g = null;
  24. const float margin = 72 / 2;
  25. const float pad = 4;
  26. const float gap = 10;
  27. var ip = new PointF(margin, margin);
  28. Action newPage = () =>
  29. {
  30. page = doc.NewPage();
  31. g = page.Graphics;
  32. ip = new PointF(margin, margin);
  33. };
  34. newPage();
  35.  
  36. var tfCaption = new TextFormat()
  37. {
  38. Font = StandardFonts.Times,
  39. FontSize = 12,
  40. };
  41. var tfBarcode = new TextFormat()
  42. {
  43. Font = StandardFonts.Helvetica,
  44. FontSize = 9,
  45. };
  46. var barcode = new GcBarcode()
  47. {
  48. TextFormat = tfBarcode,
  49. ScaleFactor = 1.5f,
  50. };
  51. barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below;
  52. barcode.Options.SizeOptions.NarrowWideRatio = 0;
  53.  
  54. Action<CodeType, string, string> drawBarcode = (ct_, txt_, txt2_) =>
  55. {
  56. var caption = $"{ct_}:\r\n{txt_}";
  57. if (string.IsNullOrEmpty(txt2_))
  58. barcode.Options.GS1Composite.Type = GS1CompositeType.None;
  59. else
  60. {
  61. caption += $"\r\nDependent CCA: {txt2_}";
  62. barcode.Options.GS1Composite.Type = GS1CompositeType.CCA;
  63. barcode.Options.GS1Composite.Value = txt2_;
  64. }
  65. barcode.Options.CheckSumEnabled = ct_ != CodeType.Code25intlv && ct_ != CodeType.Code_2_of_5 && ct_ != CodeType.Matrix_2_of_5;
  66. var csize = g.MeasureString(caption, tfCaption);
  67. barcode.CodeType = ct_;
  68. barcode.Text = txt_;
  69. var size = g.MeasureBarcode(barcode);
  70. size.Height = Math.Max(size.Height, csize.Height);
  71. var border = new RectangleF(ip, new SizeF(page.Size.Width - margin * 2, size.Height + pad * 2));
  72. if (ip.Y + border.Height > page.Size.Height - margin)
  73. {
  74. newPage();
  75. border = new RectangleF(ip, border.Size);
  76. }
  77. g.DrawRectangle(border, Color.Gray);
  78. g.DrawString(caption, tfCaption, new PointF(border.Left + pad, border.Top + pad));
  79. g.DrawBarcode(barcode, new RectangleF(border.Right - size.Width - pad, border.Top + pad, size.Width, size.Height));
  80. ip.Y = border.Bottom + gap;
  81. };
  82. //
  83. drawBarcode(CodeType.Ansi39, "*DSBARCODE*", null);
  84. drawBarcode(CodeType.Ansi39x, "*DsPdf*", null);
  85. drawBarcode(CodeType.Codabar, "A12041961D", null);
  86. drawBarcode(CodeType.Code25intlv, "1234567890", null); //#2 Interleaved 2 of 5 (ITF)
  87. drawBarcode(CodeType.Code39, "*DSBARCODE*", null);
  88. drawBarcode(CodeType.Code39x, "*DsPdf*", null);
  89. drawBarcode(CodeType.Code49, "DsBarcode+DsPdf", null);
  90. drawBarcode(CodeType.Code93x, "DsBarcode+DsPdf", null);
  91. drawBarcode(CodeType.Code_93, "DSBARCODE", null);
  92. drawBarcode(CodeType.Code_128_A, "DSPDF-2023", null);
  93. drawBarcode(CodeType.Code_128_B, "DsPdf-2023", null);
  94. drawBarcode(CodeType.Code_128_C, "1234567890", null);
  95. drawBarcode(CodeType.Code_128auto, "DsPdf-2023", null);
  96. drawBarcode(CodeType.Code_2_of_5, "1234567890", null);
  97. drawBarcode(CodeType.DataMatrix, "DsBarcode+DsPdf", null);
  98. drawBarcode(CodeType.QRCode, "DsBarcode+DsPdf", null);
  99. drawBarcode(CodeType.EAN_8, "1234567", null);
  100. drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null);
  101. drawBarcode(CodeType.EAN128FNC1, "DsBarcode\nDsPdf", null);
  102. drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null);
  103. drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", null);
  104. drawBarcode(CodeType.PostNet, "152063949", null);
  105. drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null);
  106. drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null);
  107. drawBarcode(CodeType.MSI, "1234567890", null);
  108. drawBarcode(CodeType.MicroPDF417, "DsPdf", null);
  109. drawBarcode(CodeType.Pdf417, "DsPdf", null);
  110. drawBarcode(CodeType.RSS14, "1234567890", null);
  111. drawBarcode(CodeType.RSS14Stacked, "1234567890", null);
  112. drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345");
  113. drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null);
  114. drawBarcode(CodeType.RSS14Truncated, "1234567890", null);
  115. drawBarcode(CodeType.RSSExpanded, "12345678901234", null);
  116. drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null);
  117. drawBarcode(CodeType.RSSLimited, "1234567890", null);
  118. drawBarcode(CodeType.RSSLimited, "1234567890", "12345");
  119. drawBarcode(CodeType.UCCEAN128, "DsBarcode+DsPdf", null);
  120. drawBarcode(CodeType.UPC_A, "123456789012", null);
  121. drawBarcode(CodeType.UPC_E0, "123456789012", null);
  122. drawBarcode(CodeType.UPC_E1, "123456789012", null);
  123. // Save the PDF:
  124. doc.Save(stream);
  125. return doc.Pages.Count;
  126. }
  127. }
  128. }
  129.