/// <summary> /// Prints a bar code /// </summary> /// <param name="Value">The bar code</param> /// <param name="Format">The format to print the code in</param> /// <param name="BarWidth">The width of each single bar (so not of the full bar code)</param> /// <param name="BarHeight">The height of each single bar (also the height of the full bar code)</param> /// <param name="PrintValueAbove">When true, the value will also be printed in text, above the bar code</param> /// <param name="PrintValueBelow">When true, the value will also be printed in text, below the bar code</param> /// <remarks> /// The printer also validates the barcode. If it's not valid it won't print. /// If you send the checkbyte yourself (in most barcodes the last digit) and it does not compute, discard it. /// Also, if the barcode won't fit the paper, it won't print either. Try reducing the BarWidth in that case. /// </remarks> public void PrintBarcode(string Value, BarCodeSystem Format, byte BarWidth = 3, byte BarHeight = 50, bool PrintValueAbove = false, bool PrintValueBelow = true) { // Dimension checks if (BarHeight < 1 || BarHeight > 255) throw new ArgumentOutOfRangeException("Height can only be between 1 and 255"); if (BarWidth < 2 || BarWidth > 3) throw new ArgumentOutOfRangeException("Width can only be between 2 and 3"); // When true, the barcode is sent with a length parameter too bool DynamicLength = false; // Barcode system checks switch (Format) { case BarCodeSystem.UPC_A: //if (Value.Length != 12) throw new ArgumentOutOfRangeException("UPC_A only support a length of 12 characters"); if (Value.Length < 11 || Value.Length > 12) throw new ArgumentOutOfRangeException("UPC_A only support a length of 11 to 12 characters"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("UPC_A only support numbers"); break; case BarCodeSystem.UPC_E: if (Value.Length < 11 || Value.Length > 12) throw new ArgumentOutOfRangeException("UPC_E only support a length of 11 to 12 characters"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("UPC_E only support numbers"); if (Value.Substring(0, 1) != "0") throw new ArgumentException("UPC_E requires the first digit to be 0"); // Some other rules do apply, haven't checked all rules break; case BarCodeSystem.EAN13: if (Value.Length < 12 || Value.Length > 13) throw new ArgumentOutOfRangeException("EAN13 only support a length of 12 to 13 characters"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("EAN13 only support numbers"); break; case BarCodeSystem.EAN8: if (Value.Length != 8) throw new ArgumentOutOfRangeException("EAN8 only support a length of 8 characters"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("EAN8 only support numbers"); break; case BarCodeSystem.CODE39: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("CODE39 requires at least 2 characters and at most 255 characters"); if (!this._HasOnlyCharacters(Value, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -$%./+")) throw new ArgumentException("CODE39 only supports the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -$%./+"); break; case BarCodeSystem.I25: if (Value.Length == 0 || (Value.Length % 2) != 0) throw new ArgumentException("I25 only supports an even number of characters, above 0"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("I25 only support numbers"); break; case BarCodeSystem.CODEBAR: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("CODEBAR requires at least 2 characters and at most 255 characters"); if (!this._HasOnlyCharacters(Value, "$+-./0123456789:ABCD")) throw new ArgumentException("CODEBAR only supports the following characters: $+-./0123456789:ABCD"); // Some other rules do apply, haven't checked all rules break; case BarCodeSystem.CODE93: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("CODE93 requires at least 2 characters and at most 255 characters"); if (!this._HasOnlyCharacters(Value, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%")) throw new ArgumentException("CODE93 only supports the following characters: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"); break; case BarCodeSystem.CODE128: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("CODE128 requires at least 2 characters and at most 255 characters"); break; case BarCodeSystem.CODE11: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("CODE11 requires at least 2 characters and at most 255 characters"); if (!this._HasOnlyCharacters(Value, "0123456789-")) throw new ArgumentException("CODE11 only support numbers and a dash (-)"); break; case BarCodeSystem.MSI: if (Value.Length < 2 || Value.Length > 255) throw new ArgumentOutOfRangeException("MSI requires at least 2 characters and at most 255 characters"); if (!this._HasOnlyCharacters(Value, "0123456789")) throw new ArgumentException("MSI only support numbers"); break; } // Turns the PrintValueAbove & PrintValueBelow to a single byte byte PrintValue = (byte)((PrintValueAbove ? 0x01 : 0) + (PrintValueBelow ? 0x02 : 0)); // Select printing position of human readable characters this.Print(new byte[] { 0x1d, 0x48, PrintValue }); // Set bar code width this.Print(new byte[] { 0x1d, 0x77, BarWidth }); // Set bar code height this.Print(new byte[] { 0x1d, 0x68, BarHeight }); // Actually prints the barcode this.Print(new byte[] { 0x1d, 0x6b, (byte)Format }); if (DynamicLength) this.Print(new byte[] { (byte)Value.Length }); this.Print(Value); this.Print(new byte[] { 0x00 }); }
/// <summary> /// Prints a bar code /// </summary> /// <param name="Value">The bar code</param> /// <param name="Format">The format to print the code in</param> /// <param name="BarWidth">The width of each single bar (so not of the full bar code)</param> /// <param name="BarHeight">The height of each single bar (also the height of the full bar code)</param> /// <param name="PrintValueAbove">When true, the value will also be printed in text, above the bar code</param> /// <param name="PrintValueBelow">When true, the value will also be printed in text, below the bar code</param> /// <remarks> /// The printer also validates the barcode. If it's not valid it won't print. /// If you send the checkbyte yourself (in most barcodes the last digit) and it does not compute, discard it. /// Also, if the barcode won't fit the paper, it won't print either. Try reducing the BarWidth in that case. /// </remarks> public void PrintBarcode(string Value, BarCodeSystem Format, byte BarWidth = 3, byte BarHeight = 50, bool PrintValueAbove = false, bool PrintValueBelow = true) { // Dimension checks if (BarHeight < 1 || BarHeight > 255) { throw new ArgumentOutOfRangeException("Height can only be between 1 and 255"); } if (BarWidth < 2 || BarWidth > 3) { throw new ArgumentOutOfRangeException("Width can only be between 2 and 3"); } // When true, the barcode is sent with a length parameter too bool DynamicLength = false; // Barcode system checks switch (Format) { case BarCodeSystem.UPC_A: //if (Value.Length != 12) throw new ArgumentOutOfRangeException("UPC_A only support a length of 12 characters"); if (Value.Length < 11 || Value.Length > 12) { throw new ArgumentOutOfRangeException("UPC_A only support a length of 11 to 12 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("UPC_A only support numbers"); } break; case BarCodeSystem.UPC_E: if (Value.Length < 11 || Value.Length > 12) { throw new ArgumentOutOfRangeException("UPC_E only support a length of 11 to 12 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("UPC_E only support numbers"); } if (Value.Substring(0, 1) != "0") { throw new ArgumentException("UPC_E requires the first digit to be 0"); } // Some other rules do apply, haven't checked all rules break; case BarCodeSystem.EAN13: if (Value.Length < 12 || Value.Length > 13) { throw new ArgumentOutOfRangeException("EAN13 only support a length of 12 to 13 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("EAN13 only support numbers"); } break; case BarCodeSystem.EAN8: if (Value.Length != 8) { throw new ArgumentOutOfRangeException("EAN8 only support a length of 8 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("EAN8 only support numbers"); } break; case BarCodeSystem.CODE39: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("CODE39 requires at least 2 characters and at most 255 characters"); } if (!this._HasOnlyCharacters(Value, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -$%./+")) { throw new ArgumentException("CODE39 only supports the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -$%./+"); } break; case BarCodeSystem.I25: if (Value.Length == 0 || (Value.Length % 2) != 0) { throw new ArgumentException("I25 only supports an even number of characters, above 0"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("I25 only support numbers"); } break; case BarCodeSystem.CODEBAR: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("CODEBAR requires at least 2 characters and at most 255 characters"); } if (!this._HasOnlyCharacters(Value, "$+-./0123456789:ABCD")) { throw new ArgumentException("CODEBAR only supports the following characters: $+-./0123456789:ABCD"); } // Some other rules do apply, haven't checked all rules break; case BarCodeSystem.CODE93: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("CODE93 requires at least 2 characters and at most 255 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%")) { throw new ArgumentException("CODE93 only supports the following characters: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"); } break; case BarCodeSystem.CODE128: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("CODE128 requires at least 2 characters and at most 255 characters"); } break; case BarCodeSystem.CODE11: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("CODE11 requires at least 2 characters and at most 255 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789-")) { throw new ArgumentException("CODE11 only support numbers and a dash (-)"); } break; case BarCodeSystem.MSI: if (Value.Length < 2 || Value.Length > 255) { throw new ArgumentOutOfRangeException("MSI requires at least 2 characters and at most 255 characters"); } if (!this._HasOnlyCharacters(Value, "0123456789")) { throw new ArgumentException("MSI only support numbers"); } break; } // Turns the PrintValueAbove & PrintValueBelow to a single byte byte PrintValue = (byte)((PrintValueAbove ? 0x01 : 0) + (PrintValueBelow ? 0x02 : 0)); // Select printing position of human readable characters this.Print(new byte[] { 0x1d, 0x48, PrintValue }); // Set bar code width this.Print(new byte[] { 0x1d, 0x77, BarWidth }); // Set bar code height this.Print(new byte[] { 0x1d, 0x68, BarHeight }); // Actually prints the barcode this.Print(new byte[] { 0x1d, 0x6b, (byte)Format }); if (DynamicLength) { this.Print(new byte[] { (byte)Value.Length }); } this.Print(Value); this.Print(new byte[] { 0x00 }); }