ITF-14 và Interleaved 2 of 5 (I-2/5) là các biểu tượng linear cho cartons, pallets, và logistics nội bộ. itf-14 mã hóa một GTIN-14** (14 chữ số, bao gồm một Mod-10 kiểm tra số) và thường được in với barer trên bảng xếp hạng. i-2/5 là một nhân vật nhỏ gọn, số-chỉ, even-length thường sử dụng cho các đường và trường hợp bên trong.
Mẫu hoàn chỉnh (Copy-Paste Ready)
Những gì bạn nhận được:
- Một ứng dụng .NET console có thể phát hành mã thanh ITF-14 và I-2/5.
- A GTIN-14 check digit trợ giúp cho ITF-14.
- Hành động thực thi chiều dài bên ngoài cho I-2/5.
- Độ nhạy cảm cho in (margin, độ dày bar, chiều cao).
- PNG sản xuất theo tên filename.
1) Tạo dự án và thêm gói
dotnet new console -n ItfAndI25Demo -f net8.0
cd ItfAndI25Demo
dotnet add package Aspose.BarCode
2) Thay thế Program.cs
Với những
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) Thực hiện một vài ví dụ
# 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 (Những gì mã đang làm)
Các yếu tố ITF-14
- Mã hóa một GTIN-14 (14 chữ số tổng).
- Điểm số cuối cùng* là một Mod-10 check digit.
- Thường được in lớn với các vùng yên tĩnh** và đôi khi barer (một khung xung quanh mã) trên corugated.
** Trong mã:** chúng tôi chấp nhận lên đến 13 chữ số, dán xuống 13, tính số 14, và chuyển tất cả 14 chữ cái cho EncodeTypes.ITF14
.
string gtin13 = gtinBase.PadLeft(13, '0');
char check = CalcGtin14CheckDigit(gtin13);
string gtin14 = gtin13 + check;
using var gen = new BarCodeGenerator(EncodeTypes.ITF14, gtin14);
Interleaved 2 of 5 nguyên liệu cần thiết
- Nhãn hiệu: Compact, numeric-only symbology
- Nó đòi hỏi một một số chữ số tương đương (cặp đôi được chia sẻ).
- Chúng tôi left-pad với
0
Nếu đầu vào là odd-length
string evenData = data.Length % 2 == 0 ? data : "0" + data;
using var gen = new BarCodeGenerator(EncodeTypes.Interleaved2of5, evenData);
Dấu hiệu Print-Friendly defaults
- X-Dimension** (mộ dày bar/module):
3 px
Đây là điểm khởi đầu thực tế cho máy in nhiệt.
- X-Dimension** (mộ dày bar/module):
- Độ cao Bar *:
~22 mm
hoạt động tốt trên các nhãn 1×3′′ hoặc 2×1′′; điều chỉnh cho kho của bạn.
- Độ cao Bar *:
- Khu vực quan trọng *:
~4 mm
trái / phải;2–3 mm
Top / Bottom
- Khu vực quan trọng *:
- Đọc văn bản*: hiển thị dưới đây (
CodeLocation.Below
Nếu nhãn của bạn cần văn bản.
- Đọc văn bản*: hiển thị dưới đây (
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;
Ý tưởng Customization
Bigger labels / hard media → tăng
XDimension
để4–5 px
.- Độ nhãn tắt hơn** → giảm độ cao bar (
18–20 mm
Nhưng không bao giờ đói ở những vùng yên tĩnh.
- Độ nhãn tắt hơn** → giảm độ cao bar (
Suppress HRT (Human-readable text) nếu bố trí của bạn in văn bản ở nơi khác:
gen.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;
Hãy lưu ý các thanh vận tải (ITF-14): Nhiều máy in / tiêu chuẩn thích một frame hoặc top / bottom bar xung quanh ITF-14 để ngăn chặn quét ngắn.Nếu Aspose.BarCode xây dựng của bạn tiết lộ các thông số cụ thể của thanh điều khiển ITP, kích hoạt chúng; nếu không, hãy thêm frame vào bố trí nhãn của mình.
Troubleshooting
- Không được quét trên corrugated: Tăng
XDimension
, đảm bảo độ tương phản cao, thêm / xác nhận vùng yên tĩnh, xem xét thanh vận tải. - I-2/5 bị từ chối như một chiều dài kỳ lạ: Bạn quên đeo; sử dụng mã
evenData
logic . - ITF-14 kiểm tra số sai: Hãy chắc chắn rằng bạn chỉ chuyển 13 số đầu tiên** đến máy tính; hãy để mã tính số 14.
Thực hành tốt nhất
- Lock parameters (X-dimension, height, margins) in config so output is reproducible.
- ** Kiểm tra trên máy quét mục tiêu của bạn** và các phương tiện truyền thông nhãn-tweak bằng cải thiện nhỏ.
- Các phiên bản mẫu của bạn nếu bạn thêm logo / văn bản ở trên hoặc dưới các thanh.
- Giữ nó số: I-2/5 không hỗ trợ non-digits; sanitize input sớm.
Kết luận
Với một vài dòng mã, bạn có thể sản xuất robust, scanner-friendly ITF-14 và Interleaved 2 of 5 barcodes trong .NET bằng cách sử dụng Aspose.BarCode. Bắt đầu với ví dụ đầy đủ ở trên, sau đó tuân thanh dày, chiều cao, và giới hạn cho máy in và máy quét của bạn.