BarcodePostNet.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace FastReport.Barcode
  7. {
  8. /// <summary>
  9. /// Generates the PostNet barcode.
  10. /// </summary>
  11. public class BarcodePostNet : LinearBarcodeBase
  12. {
  13. private static string[] tabelle_PostNet = {
  14. "5151919191", //0
  15. "9191915151", //1
  16. "9191519151", //2
  17. "9191515191", //3
  18. "9151919151", //4
  19. "9151915191", //5
  20. "9151519191", //6
  21. "5191919151", //7
  22. "5191915191", //8
  23. "5191519191" //9
  24. };
  25. internal override string GetPattern()
  26. {
  27. string result = "51";
  28. for (int i = 0; i < text.Length; i++)
  29. result += tabelle_PostNet[CharToInt(text[i])];
  30. result += "5";
  31. return result;
  32. }
  33. }
  34. /// <summary>
  35. /// Generates the Japan Post 4 State Code barcode.
  36. /// </summary>
  37. public class BarcodeJapanPost4StateCode : LinearBarcodeBase
  38. {
  39. private string CeckDigitSet = "0123456789-abcdefgh";
  40. private string EncodeTable = "1234567890-abcdefgh";
  41. private static string[] JapanTable =
  42. {
  43. "6161E", //1
  44. "61G1F", //2
  45. "G161F", //3
  46. "61F1G", //4
  47. "61E16", //5
  48. "G1F16", //6
  49. "F161G", //7
  50. "F1G16", //8
  51. "E1616", //9
  52. "61E1E", //0
  53. "E161E", //-
  54. "G1F1E", //a
  55. "G1E1F", //b
  56. "F1G1E", //c
  57. "E1G1F", //d
  58. "F1E1G", //e
  59. "E1F1G", //f
  60. "E1E16", //g
  61. "61616" //h
  62. };
  63. internal override string GetPattern()
  64. {
  65. string encoded = "";
  66. int sum = 0;
  67. int weight = 0;
  68. string result = "61G1"; // start bar
  69. if (text.Length < 7)
  70. {
  71. throw new FormatException(Res.Get("Messages,BarcodeFewError"));
  72. }
  73. foreach (var i in text)
  74. {
  75. if (((i >= '0') && (i <= '9')) || (i == '-'))
  76. {
  77. encoded += i;
  78. weight++;
  79. }
  80. else
  81. {
  82. if ((i >= 'A') && (i <= 'J'))
  83. {
  84. encoded += 'a';
  85. encoded += (char)(i - 'A' + '0');
  86. }
  87. if ((i >= 'K') && (i <= 'T'))
  88. {
  89. encoded += 'b';
  90. encoded += (char)(i - 'K' + '0');
  91. }
  92. if ((i >= 'U') && (i <= 'Z'))
  93. {
  94. encoded += 'c';
  95. encoded += (char)(i - 'U' + '0');
  96. }
  97. weight += 2;
  98. }
  99. }
  100. // remove the hyphens that will not be encoded in the barcode
  101. if (encoded.IndexOf('-') == 3)
  102. {
  103. encoded = encoded.Remove(3, 1);
  104. weight--;
  105. }
  106. if (encoded.IndexOf('-', 5) == 7)
  107. {
  108. encoded = encoded.Remove(7, 1);
  109. weight--;
  110. }
  111. if (weight > 20 || Regex.IsMatch(text.Substring(0, 7), "[^0-9\\-]") ||
  112. Regex.IsMatch(text.Substring(7, text.Length - 7), "[^A-Z0-9\\-]") ||
  113. (encoded.IndexOf('-') < 8 && encoded.IndexOf('-') != -1 ))
  114. {
  115. throw new FormatException(Res.Get("Messages,BarcodeLengthMismatch"));
  116. }
  117. // fill pad character CC4, if need
  118. for (int i = encoded.Length; i < 20; i++)
  119. {
  120. encoded += 'd';
  121. }
  122. for (int i = 0; i < 20; i++)
  123. {
  124. result += JapanTable[EncodeTable.IndexOf(encoded[i])];
  125. sum += CeckDigitSet.IndexOf(encoded[i]);
  126. result += '1';
  127. }
  128. //Calculate check digit
  129. char check_char = char.MinValue;
  130. int check = 19 - (sum % 19);
  131. if (check == 19) { check = 0; }
  132. if (check <= 9) { check_char = (char)(check + '0'); }
  133. if (check == 10) { check_char = '-'; }
  134. if (check >= 11) { check_char = (char)((check - 11) + 'a'); }
  135. result += JapanTable[EncodeTable.IndexOf(check_char)];
  136. // data + stop bar
  137. return result + "1G16";
  138. }
  139. }
  140. }