Exemple #1
0
        private Bitmap GenerateBarcodeHIBC()
        {
            ActiveUp.WebControls.SecondaryDataHibc secondData = new ActiveUp.WebControls.SecondaryDataHibc();
            if (SecondaryData.Date != "")
            {
                try
                {
                    secondData.Date = DateTime.Parse(SecondaryData.Date);
                }
                catch (Exception)
                {
                    throw new Exception("Invalid date format in the Date box.");
                }
            }

            if (SecondaryData.Quantity != "")
            {
                try
                {
                    secondData.Quantity = int.Parse(SecondaryData.Quantity);
                }
                catch (Exception)
                {
                    throw new Exception("The value in the quantity box must be a number between 0 and 99999.");
                }
            }

            if (SecondaryData.Serial != "")
            {
                secondData.Lot = SecondaryData.Serial;
            }

            secondData.DateFormat = this.SecondaryData.DateFormat;

            Regex licCheck = new Regex("[A-Z][A-Z0-9]{3}", RegexOptions.IgnoreCase);
            Regex pcnCheck = new Regex("[A-Z0-9]{1,13}", RegexOptions.IgnoreCase);
            Regex gs1Check = new Regex("[0-9]{12}");

            if (this.HIBCType == HIBCType.LIC)
            {
                //Check the LIC and PCN.
                if (this.LicGs1 == "" || !licCheck.IsMatch(this.LicGs1))
                {
                    throw new Exception("The LIC is not formatted correctly.");
                }

                if (this.Pcn != "" && !pcnCheck.IsMatch(this.Pcn))
                {
                    throw new Exception("The PCN is not formatted correctly.");
                }
            }
            else
            {
                //GS1
                if (this.LicGs1 == "" || !gs1Check.IsMatch(this.LicGs1))
                {
                    throw new Exception("The GS1 is not formatted correctly.");
                }

                if (this.Pcn != "" && !pcnCheck.IsMatch(this.Pcn))
                {
                    throw new Exception("The PCN is not formatted correctly.");
                }
            }

            byte uom;

            try
            {
                uom = (byte)this.UnitOfMeasure;
            }
            catch (Exception)
            {
                throw new Exception("The Unit of Measurement is not set.");
            }

            IBarcodeEncoder[] encoders;
            ActiveUp.WebControls.PrimaryEncodingMode   pMode;
            ActiveUp.WebControls.SecondaryEncodingMode sMode;


            try
            {
                pMode = this.PrimaryEncoding;
            }
            catch (Exception)
            {
                throw new Exception("An error occured in parsing the primary encoding mode.");
            }

            try
            {
                sMode = this.SecondaryData.SecondaryEncoding;
            }
            catch (Exception)
            {
                throw new Exception("An error occured in parsing the secondary encoding mode.");
            }

            try
            {
                if (this.HIBCType == HIBCType.LIC)
                {
                    encoders = ActiveUp.WebControls.HIBCEncoder.EncodeHIBC(this.LicGs1, this.Pcn, uom, secondData, pMode, sMode);
                }
                else
                {
                    encoders = ActiveUp.WebControls.HIBCEncoder.EncodeGS1(uom.ToString() + this.LicGs1, (this.Pcn == "") ? null : this.Pcn, secondData, pMode, sMode);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An exception occured:\n" + ex.Message);
            }

            encoders[0].Sizer.DPI = (float)this.Dpi.Value;
            if (encoders.Length == 2)
            {
                encoders[1].Sizer.DPI = (float)this.Dpi.Value;
            }

            encoders[0].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered;
            if (encoders.Length == 2)
            {
                encoders[1].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered;
            }

            Bitmap bmpPrimary = null, bmpSecondary = null;

            bmpPrimary = encoders[0].Generator.GenerateBarcode(encoders[0].Sizer.Size);
            if (encoders.Length == 2)
            {
                bmpSecondary = encoders[1].Generator.GenerateBarcode(encoders[1].Sizer.Size);
            }

            if (this.Level == Level.Primary)
            {
                return(bmpPrimary);
            }
            else
            {
                return(bmpSecondary);
            }
        }
        /// <summary>
        /// Encodes the specified GS1 primary data and optional secondary data into one or more barcodes.
        /// </summary>
        /// <param name="GS1">The GS1 primary data, consisting of an indicator digit, a twelve digit GS1 identifier, and an optional check digit.</param>
        /// <param name="PCN">An optional Product or Catalog Number, to be included in an EAN128 barcode under Application Identifier (240). Ignored if <paramref name="primaryMode"/> is <see cref="Barcodes.HIBC.PrimaryEncodingMode.I2of5"/>.</param>
        /// <param name="secondary">An optional instance of the <see cref="Barcodes.HIBC.SecondaryData"/> class, containing secondary data.</param>
        /// <param name="primaryMode">The symbology to use for the primary barcode.  Must be <see cref="Barcodes.HIBC.PrimaryEncodingMode.I2of5"/> or <see cref="Barcodes.HIBC.PrimaryEncodingMode.EAN128"/>.</param>
        /// <param name="secondaryMode">The symbology to use for the secondary barcode.  May be any <see cref="Barcodes.HIBC.SecondaryEncodingMode"/> value other than <see cref="Barcodes.HIBC.SecondaryEncodingMode.Code39"/>
        /// or <see cref="Barcodes.HIBC.SecondaryEncodingMode.Code39"/>, however,
        /// if <paramref name="secondary"/> is null, this value must be <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</param>
        /// <returns>An array of <see cref="Barcodes.IBarcodeEncoder"/> objects, containing one or two pre-set encoders.</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentException">There was an error in the GS1 or PCN strings.</exception>
        /// <exception cref="System.InvalidOperationException">The <paramref name="primaryMode"/> or <paramref name="secondaryMode"/>
        /// values are invalid for the type of data provided (HIBC vs. GS1).</exception>
        /// <exception cref="System.NullReferenceException">Argument <paramref name="secondary"/> is null and <paramref name="secondaryMode"/> is not <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</exception>
        /// <exception cref="System.OverflowException">If <paramref name="primaryMode"/> is <see cref="Barcodes.HIBC.PrimaryEncodingMode.EAN128"/>, the resulting barcode
        /// exceeds the maximum amount of data encodable in an EAN128 barcode.</exception>
        public static IBarcodeEncoder[] EncodeGS1(string GS1, string PCN, SecondaryDataHibc secondary, PrimaryEncodingMode primaryMode, SecondaryEncodingMode secondaryMode)
        {
            if (primaryMode == PrimaryEncodingMode.Code128 || primaryMode == PrimaryEncodingMode.Code39)
            {
                throw new InvalidOperationException("The primary mode can only be EAN128 or I2of5.");
            }
            if (secondaryMode == SecondaryEncodingMode.Code128 || secondaryMode == SecondaryEncodingMode.Code39)
            {
                throw new InvalidOperationException("The secondary mode can only be EAN128, Combined, or None.");
            }
            if (secondaryMode != SecondaryEncodingMode.None && secondary == null)
            {
                throw new NullReferenceException("The secondary encoding mode is not None and secondary is null.");
            }
            if (primaryMode != PrimaryEncodingMode.EAN128 && secondaryMode == SecondaryEncodingMode.Combined)
            {
                throw new InvalidOperationException("The secondary data can not be combined when the primary encoding is not EAN128.");
            }

            if (GS1.Length != 13 && GS1.Length != 14)
            {
                throw new ArgumentException("The GS1 must be 13 or 14 digits long.", "GS1");
            }
            byte checkSum = CalculateGS1Check(GS1);

            if (checkSum == 255)
            {
                throw new ArgumentException("The GS1 number contains non-digit characters.", "GS1");
            }
            if (GS1.Length == 14 && "0123456789"[checkSum] != GS1[13])
            {
                throw new ArgumentException("The check digit specified in the GS1 number is invalid.", "GS1");
            }
            else if (GS1.Length == 13)
            {
                GS1 += checkSum.ToString();
            }

            if (PCN != null)
            {
                PCN = PCN.ToUpper();
                if (!pcnCheck.IsMatch(PCN))
                {
                    throw new ArgumentException("The PCN number specified is invalid.", "PCN");
                }
            }

            string secondaryEncoding = "";

            if (secondary != null)
            {
                secondaryEncoding  = secondary.GS1Encoded;
                secondaryEncoding += checkSum.ToString(); //Link-char.
            }

            IBarcodeEncoder ean128, itf, secondEAN;

            string primaryEncoding;

            if (primaryMode == PrimaryEncodingMode.EAN128)
            {
                primaryEncoding = "(01)" + GS1;
                if (PCN != null)
                {
                    primaryEncoding += "(240)" + PCN;
                }
                if (secondaryMode == SecondaryEncodingMode.Combined && secondary != null)
                {
                    primaryEncoding += secondaryEncoding;
                }
                ean128 = new EAN128Encoder();
                try {
                    ean128.Text = primaryEncoding;
                } catch (ArgumentException ex) {
                    throw new OverflowException("The combined primary EAN128 barcode was larger than the maximum allowable EAN128 size.", ex);
                }
                if (secondaryMode != SecondaryEncodingMode.EAN128 || secondary == null)
                {
                    return new IBarcodeEncoder[] { ean128 }
                }
                ;

                secondEAN      = new EAN128Encoder();
                secondEAN.Text = secondaryEncoding;
                return(new IBarcodeEncoder[] { ean128, secondEAN });
            }
            else
            {
                itf = new I2of5Encoder();

                itf.Text = GS1;

                if (secondaryMode == SecondaryEncodingMode.None || secondary == null)
                {
                    return new IBarcodeEncoder[] { itf }
                }
                ;

                //We have a secondary.
                secondEAN      = new EAN128Encoder();
                secondEAN.Text = secondaryEncoding;
                return(new IBarcodeEncoder[] { itf, secondEAN });
            }
        }
    }
}
        /// <summary>
        /// Encodes an HIBC primary data string.
        /// </summary>
        /// <param name="primary">The primary data string, consisting of the LIC, the PCN, and the unit of measure digit.</param>
        /// <param name="secondary">An optional <see cref="Barcodes.HIBC.SecondaryData"/> object containing secondary data.</param>
        /// <param name="primaryMode">The symbology to use for the primary barcode.  Must be <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code39"/> or <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code128"/>.</param>
        /// <param name="secondaryMode">The symbology to use for the secondary barcode.  May be any <see cref="Barcodes.HIBC.SecondaryEncodingMode"/> value other than <see cref="Barcodes.HIBC.SecondaryEncodingMode.EAN128"/>, however,
        /// if <paramref name="secondary"/> is null, this value must be <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</param>
        /// <returns>An array of <see cref="Barcodes.IBarcodeEncoder"/> objects, containing one or two pre-set encoders.</returns>
        /// <remarks>This is the same function as <see cref="Barcodes.HIBC.HIBCEncoder.EncodeHIBC(string, SecondaryData, PrimaryEncodingMode, SecondaryEncodingMode)"/>, except
        /// that the primary data (LIC, PCN, and Unit of Measure) is combined into one parameter in the alternate method.</remarks>
        /// <exception cref="System.ArgumentException">There was an error in the primary data.</exception>
        /// <exception cref="System.InvalidOperationException">The <paramref name="primaryMode"/> or <paramref name="secondaryMode"/>
        /// values are invalid for the type of data provided (HIBC vs. GS1).</exception>
        /// <exception cref="System.NullReferenceException">Argument <paramref name="secondary"/> is null and <paramref name="secondaryMode"/> is not <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</exception>
        public static IBarcodeEncoder[] EncodeHIBC(string primary, SecondaryDataHibc secondary, PrimaryEncodingMode primaryMode, SecondaryEncodingMode secondaryMode)
        {
            if (primaryMode == PrimaryEncodingMode.EAN128 || primaryMode == PrimaryEncodingMode.I2of5)
            {
                throw new InvalidOperationException("The primary mode can only be Code39 or Code128.");
            }
            if (secondaryMode == SecondaryEncodingMode.EAN128)
            {
                throw new InvalidOperationException("The secondary mode can only be Code39, Code128, Combined, or None.");
            }
            if (secondaryMode != SecondaryEncodingMode.None && secondary == null)
            {
                throw new NullReferenceException("The secondary encoding mode is not None and secondary is null.");
            }

            Regex primaryCheck = new Regex("[A-Z][A-Z0-9]{3}[A-Z0-9]{1,13}[0-9]");

            primary = primary.ToUpper();
            if (!primaryCheck.IsMatch(primary))
            {
                throw new ArgumentException("The specified primary data string (LIC+PCN+Unit) is invalid. The proper format is [A-Z][A-Z0-9]{3}[A-Z0-9]{1,13}[0-9].", "primary");
            }

            primary = "+" + primary;
            byte checkSum = CalculateHIBCCheck(primary);

            string secondaryEncoding = "";

            if (secondary != null)
            {
                if (secondary.HIBCShortEncoded != null)
                {
                    secondaryEncoding = secondary.HIBCShortEncoded;
                }
                else
                {
                    secondaryEncoding = secondary.HIBCEncoded;
                }
            }

            if (secondaryMode == SecondaryEncodingMode.Combined)
            {
                primary  = primary + "/" + secondaryEncoding.Substring(1);
                checkSum = CalculateHIBCCheck(primary);
                primary += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
            }
            else
            {
                char checkChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
                primary           += checkChar;
                secondaryEncoding += checkChar;
                checkSum           = CalculateHIBCCheck(secondaryEncoding);
                secondaryEncoding += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
            }

            IBarcodeEncoder primaryEncoder, secondaryEncoder;

            if (primaryMode == PrimaryEncodingMode.Code39)
            {
                primaryEncoder      = new Code39Encoder();
                primaryEncoder.Text = primary;
                ((Code39Generator)primaryEncoder.Generator).Text = "*" + primary.Replace(' ', '_') + "*";
            }
            else    //Code 128
            {
                primaryEncoder      = new Code128Encoder();
                primaryEncoder.Text = primary;
                ((Code128Generator)primaryEncoder.Generator).Text = "*" + primary.Replace(' ', '_') + "*";
            }

            if (secondaryMode == SecondaryEncodingMode.Combined || secondaryMode == SecondaryEncodingMode.None)
            {
                return new IBarcodeEncoder[] { primaryEncoder }
            }
            ;

            if (secondaryMode == SecondaryEncodingMode.Code39)
            {
                secondaryEncoder = new Code39Encoder();

                secondaryEncoder.Text = secondaryEncoding;
                ((Code39Generator)secondaryEncoder.Generator).Text = "*" + secondaryEncoding.Replace(' ', '_') + "*";
            }
            else
            {
                secondaryEncoder      = new Code128Encoder();
                secondaryEncoder.Text = secondaryEncoding;
                ((Code128Generator)secondaryEncoder.Generator).Text = "*" + secondaryEncoding.Replace(' ', '_') + "*";
            }

            return(new IBarcodeEncoder[] { primaryEncoder, secondaryEncoder });
        }
 /// <summary>
 /// Encodes the specified GS1 primary data and optional secondary data into one or more barcodes.
 /// </summary>
 /// <param name="GS1">The GS1 primary data, consisting of an indicator digit, a twelve digit GS1 identifier, and an optional check digit.</param>
 /// <param name="secondary">An optional instance of the <see cref="Barcodes.HIBC.SecondaryData"/> class, containing secondary data.</param>
 /// <param name="primaryMode">The symbology to use for the primary barcode.  Must be <see cref="Barcodes.HIBC.PrimaryEncodingMode.I2of5"/> or <see cref="Barcodes.HIBC.PrimaryEncodingMode.EAN128"/>.</param>
 /// <param name="secondaryMode">The symbology to use for the secondary barcode.  May be any <see cref="Barcodes.HIBC.SecondaryEncodingMode"/> value other than <see cref="Barcodes.HIBC.SecondaryEncodingMode.Code39"/>
 /// or <see cref="Barcodes.HIBC.SecondaryEncodingMode.Code39"/>, however,
 /// if <paramref name="secondary"/> is null, this value must be <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</param>
 /// <returns>An array of <see cref="Barcodes.IBarcodeEncoder"/> objects, containing one or two pre-set encoders.</returns>
 /// <remarks></remarks>
 /// <exception cref="System.ArgumentException">There was an error in the primary data.</exception>
 /// <exception cref="System.InvalidOperationException">The <paramref name="primaryMode"/> or <paramref name="secondaryMode"/>
 /// values are invalid for the type of data provided (HIBC vs. GS1).</exception>
 /// <exception cref="System.NullReferenceException">Argument <paramref name="secondary"/> is null and <paramref name="secondaryMode"/> is not <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</exception>
 public static IBarcodeEncoder[] EncodeGS1(string GS1, SecondaryDataHibc secondary, PrimaryEncodingMode primaryMode, SecondaryEncodingMode secondaryMode)
 {
     return(EncodeGS1(GS1, null, secondary, primaryMode, secondaryMode));
 }
 /// <summary>
 /// Encodes an HIBC primary data string.
 /// </summary>
 /// <param name="LIC">The Labeler Identification Code, a four character code, consisting of a letter and three alphanumeric characters.</param>
 /// <param name="PCN">A Product or Catalog Number, consisting of 1-13 alphanumeric characters.</param>
 /// <param name="unitOfMeasure">A number from 0 to 9, inclusive, representing the unit of measure.</param>
 /// <param name="secondary">An optional <see cref="Barcodes.HIBC.SecondaryData"/> object containing secondary data.</param>
 /// <param name="primaryMode">The symbology to use for the primary barcode.  Must be <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code39"/> or <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code128"/>.</param>
 /// <param name="secondaryMode">The symbology to use for the secondary barcode.  May be any <see cref="Barcodes.HIBC.SecondaryEncodingMode"/> value other than <see cref="Barcodes.HIBC.SecondaryEncodingMode.EAN128"/>, however,
 /// if <paramref name="secondary"/> is null, this value must be <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</param>
 /// <returns>An array of <see cref="Barcodes.IBarcodeEncoder"/> objects, containing one or two pre-set encoders.</returns>
 /// <remarks>This is the same function as <see cref="Barcodes.HIBC.HIBCEncoder.EncodeHIBC(string, SecondaryData, PrimaryEncodingMode, SecondaryEncodingMode)"/>, except
 /// that the primary data (LIC, PCN, and Unit of Measure) is combined into one parameter in the alternate method.</remarks>
 /// <exception cref="System.ArgumentException">There was an error in one or more of the arguments
 /// (<paramref name="LIC"/>, <paramref name="PCN"/>, or <paramref name="unitOfMeasure"/>).  The <see cref="System.ArgumentException.ParamName"/>
 /// will be the name of the invalid parameter.</exception>
 /// <exception cref="System.InvalidOperationException">The <paramref name="primaryMode"/> or <paramref name="secondaryMode"/>
 /// values are invalid for the type of data provided (HIBC vs. GS1).</exception>
 /// <exception cref="System.NullReferenceException">Argument <paramref name="secondary"/> is null and <paramref name="secondaryMode"/> is not <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</exception>
 public static IBarcodeEncoder[] EncodeHIBC(string LIC, string PCN, byte unitOfMeasure, SecondaryDataHibc secondary, PrimaryEncodingMode primaryMode, SecondaryEncodingMode secondaryMode)
 {
     if (!licCheck.IsMatch(LIC))
     {
         throw new ArgumentException("LIC is invalid; it must be four alphanumeric characters, starting with a letter.", "LIC");
     }
     if (!pcnCheck.IsMatch(PCN))
     {
         throw new ArgumentException("PCN is invalid; it must be 1-13 alphanumeric characters.", "PCN");
     }
     if (unitOfMeasure > 9)
     {
         throw new ArgumentException("unitOfMeasure must be between 0 and 9 inclusive.", "unitOfMeasure");
     }
     return(EncodeHIBC(LIC + PCN + unitOfMeasure.ToString(), secondary, primaryMode, secondaryMode));
 }