ITF-14和 **Interleaved 2 of 5(I-2/5)*是纸板、板块和内部物流的线性象征;IT F-14 编码一个 GTIN-14(包括 Mod-10 检查数字)并且通常用 ** 带栏 打印在折叠板上。

完整的例子(复制准备)

你得到的:

  • 一个 .NET 控制台应用程序可以输出 ITF-14I-2/5 的条码。
  • 一个 GTIN-14 检查数字 ITF-14 的助理。
  • Even 长度执行 为 I-2/5.
  • 印刷的敏感缺陷(边缘、条厚、高度)。
  • PNG 以 filename 输出。

(一)创建项目并添加包

dotnet new console -n ItfAndI25Demo -f net8.0
cd ItfAndI25Demo
dotnet add package Aspose.BarCode

(二)替代 Program.cs 与下列

using System;
using Aspose.BarCode.Generation;

namespace ItfAndI25Demo
{
    class Program
    {
        // Usage:
        // ITF-14  -> dotnet run -- itf14  400638133393  260930
        //            (first arg "itf14", second is GTIN base ≤13 digits; we'll compute the 14th check digit)
        // I-2/5   -> dotnet run -- i25   123456789
        //
        // Output files:
        //   ITF14_<gtin14>.png
        //   I25_<dataEven>.png

        static int Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("  ITF-14: dotnet run -- itf14 <gtin_base_≤13_digits> [xPixels=3] [heightMM=22] [marginMM=4]");
                Console.WriteLine("  I-2/5 : dotnet run -- i25   <numeric_data>        [xPixels=3] [heightMM=22] [marginMM=4]");
                return 1;
            }

            var mode = args[0].Trim().ToLowerInvariant();
            int xPixels = args.Length > 2 && int.TryParse(args[2], out var x) ? Math.Max(1, x) : 3;
            float heightMM = args.Length > 3 && float.TryParse(args[3], out var h) ? Math.Max(10f, h) : 22f;
            float marginMM = args.Length > 4 && float.TryParse(args[4], out var m) ? Math.Max(1f, m) : 4f;

            try
            {
                switch (mode)
                {
                    case "itf14":
                        {
                            string gtinBase = args[1].Trim();
                            if (gtinBase.Length > 13 || !IsAllDigits(gtinBase))
                                throw new ArgumentException("For ITF-14, provide a numeric GTIN base (≤13 digits). The 14th check digit will be computed.");

                            // Build full GTIN-14: left-pad to 13 digits, then add Mod-10 check digit
                            string gtin13 = gtinBase.PadLeft(13, '0');
                            char check = CalcGtin14CheckDigit(gtin13);
                            string gtin14 = gtin13 + check;

                            // ITF-14 encodes the 14-digit GTIN
                            using var gen = new BarCodeGenerator(EncodeTypes.ITF14, gtin14);

                            // Print-friendly defaults
                            gen.Parameters.Barcode.XDimension.Pixels = xPixels;  // bar/module thickness
                            gen.Parameters.Barcode.BarHeight.Millimeters = heightMM;
                            gen.Parameters.Barcode.LeftMargin.Millimeters = marginMM;
                            gen.Parameters.Barcode.RightMargin.Millimeters = marginMM;
                            gen.Parameters.Barcode.TopMargin.Millimeters = Math.Max(2f, marginMM / 2f);
                            gen.Parameters.Barcode.BottomMargin.Millimeters = Math.Max(2f, marginMM / 2f);

                            // Optional: show human-readable text below (depends on layout preference)
                            gen.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;
                            gen.Parameters.Barcode.CodeTextParameters.FontSize.Point = 8f;

                            // Save PNG (lossless)
                            string file = $"ITF14_{gtin14}.png";
                            gen.Save(file, BarCodeImageFormat.Png);

                            Console.WriteLine($"✅ ITF-14 saved: {file}");
                            break;
                        }
                    case "i25":
                    case "interleaved2of5":
                    case "interleaved_2_of_5":
                        {
                            string data = args[1].Trim();
                            if (!IsAllDigits(data))
                                throw new ArgumentException("I-2/5 requires numeric data.");

                            // I-2/5 needs an even number of digits; if odd, left-pad with '0'
                            string evenData = data.Length % 2 == 0 ? data : "0" + data;

                            using var gen = new BarCodeGenerator(EncodeTypes.Interleaved2of5, evenData);

                            // Print-friendly defaults
                            gen.Parameters.Barcode.XDimension.Pixels = xPixels;
                            gen.Parameters.Barcode.BarHeight.Millimeters = heightMM;
                            gen.Parameters.Barcode.LeftMargin.Millimeters = marginMM;
                            gen.Parameters.Barcode.RightMargin.Millimeters = marginMM;
                            gen.Parameters.Barcode.TopMargin.Millimeters = Math.Max(2f, marginMM / 2f);
                            gen.Parameters.Barcode.BottomMargin.Millimeters = Math.Max(2f, marginMM / 2f);

                            gen.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;
                            gen.Parameters.Barcode.CodeTextParameters.FontSize.Point = 8f;

                            string file = $"I25_{evenData}.png";
                            gen.Save(file, BarCodeImageFormat.Png);

                            Console.WriteLine($"✅ Interleaved 2 of 5 saved: {file}");
                            break;
                        }
                    default:
                        throw new ArgumentException("First argument must be 'itf14' or 'i25'.");
                }

                return 0;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("❌ Error: " + ex.Message);
                return 2;
            }
        }

        // ----------------- Helpers -----------------

        // GTIN-14 check digit (Mod-10). Argument must be the first 13 digits as a string.
        static char CalcGtin14CheckDigit(string first13)
        {
            if (first13 is null || first13.Length != 13 || !IsAllDigits(first13))
                throw new ArgumentException("CalcGtin14CheckDigit expects 13 numeric digits.");

            int sum = 0;
            // Rightmost (position 13) is index 12; multiply alternating by 3 and 1, starting with 3 on the right.
            // From the rightmost toward left: 3,1,3,1,...
            for (int i = 0; i < 13; i++)
            {
                int digit = first13[12 - i] - '0';
                int weight = (i % 2 == 0) ? 3 : 1;
                sum += digit * weight;
            }
            int mod = sum % 10;
            int check = (10 - mod) % 10;
            return (char)('0' + check);
        }

        static bool IsAllDigits(string s)
        {
            if (string.IsNullOrEmpty(s)) return false;
            foreach (char c in s)
                if (c < '0' || c > '9') return false;
            return true;
        }
    }
}

3、举几个例子

# ITF-14: pass ≤13 digits, we compute the 14th check digit
dotnet run -- itf14 400638133393
# -> ITF14_0400638133393X.png (X = computed check digit)

# Interleaved 2 of 5: any numeric string; we pad a leading 0 if odd length
dotnet run -- i25 123456789
# -> I25_0123456789.png

Step-by-Step(代码正在做什么)

ITF14 必需品

  • 编码一个 GTIN-14(总数为14个数字)。
  • 最后一个数字是Mod-10 **检查数字。
  • 经常打印大,有 ** 安静区** 和有时 * 携带栏** (代码周围的框架) 在可调。

**在代码中:**我们接受高达13个数字,点击到13,计算到14,并将所有14个字符转移到 EncodeTypes.ITF14.

string gtin13 = gtinBase.PadLeft(13, '0');
char check = CalcGtin14CheckDigit(gtin13);
string gtin14 = gtin13 + check;
using var gen = new BarCodeGenerator(EncodeTypes.ITF14, gtin14);

包含 2 的 5 个必需品

  • 微型, ** 数字 - 仅** 象征。
  • 需要一个 ** 同等数量的数字** (夫妇被交换)。
  • 我们 左板0 如果输入是异常长度。
string evenData = data.Length % 2 == 0 ? data : "0" + data;
using var gen = new BarCodeGenerator(EncodeTypes.Interleaved2of5, evenData);

印刷友好缺陷

  • X尺寸(bar/module 厚度): 3 px 这是热打印机的实用起点。
    • 酒吧高度*: ~22 mm 它在1×3′′或2×1′′标签上工作得很好;适用于您的库存。
  • Quiet 區域: ~4 mm 左/右; 2–3 mm 顶部/底部
  • 人文可读的文本:显示下方(CodeLocation.Below如果您的标签需要文本。
gen.Parameters.Barcode.XDimension.Pixels = 3;
gen.Parameters.Barcode.BarHeight.Millimeters = 22f;
gen.Parameters.Barcode.LeftMargin.Millimeters = 4f;
gen.Parameters.Barcode.RightMargin.Millimeters = 4f;
gen.Parameters.Barcode.TopMargin.Millimeters = 2f;
gen.Parameters.Barcode.BottomMargin.Millimeters = 2f;
gen.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;
gen.Parameters.Barcode.CodeTextParameters.FontSize.Point = 8f;

定制理念

  • 大标签 / 硬媒体 → 增加 XDimension4–5 px.

  • Tighter 标签 → 降低字符串高度(18–20 mm但是,永远不要饥饿的安静区域。

  • Suppress HRT(人文可读的文本) 如果您的布局在其他地方打印:

gen.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;

** 笔记载栏(ITF-14):** 许多打印机/标准更喜欢 ITF-14 周围的框架或顶部/底部栏,以防止短扫描。

Troubleshooting

  • ** 没有扫描在 corrugated:** 增加 XDimension,确保高对比,添加/确认安静区域,考虑携带条。
  • I-2/5 被拒绝为奇怪的长度: 你忘了插;使用代码 evenData 逻辑
  • ITF-14 检查数字错误: 确保您只将第一个 13 个数字转移到计算机上;让代码计算第 14 号。

最佳实践

  • 锁定参数(X尺寸、高度、边缘)在配置中,以便输出可重复。
  • ** 检查您的目标扫描仪** 和标签媒体 - 按 ** 小增量**。
  • 版本您的模板 如果您添加标志/文本上或下栏。
  • ** 保持数字**: I-2/5 不支持非数字;早期清洁输入。

结论

使用几行代码,您可以在 .NET 中创建 robust, scanner-friendly ITF-14 和 Interleaved 2 of 5 barcodes 通过 Aspose.BarCode. 从上面的完整示例开始,然后将字符串厚度、高度和边缘调整到您的打印机和扫描机。

More in this category