Bài này chúng tôi sẽ hướng dẫn bạn viết chương trình in mã vạch QR code sử dụng C# trong windows form.
Để in mã vạch QR code từ windows form các bạn cần tạo 1 tem mẫu barcode bartender sau đó viết code C# để in mã vạch từ tem barcode bartender đó.
Bước 1: Tạo tem mẫu Barcode Bartender.
Lưu ý: Bạn cần phải tải và cài đặt phần mềm Bartender trước, sau đó bạn tạo một tem mẫu Barcode Bartender.
1.1 Tải phần mềm Bartender từ link web sau: https://portal.seagullscientific.com/downloads/bartender
1.2 Tạo tem mẫu Barcode
-Tạo mã vạch QR code SN.btw và lưu vào đường dẫn sau: "D:\\Ihoclaptrinh.com\\Print-Barcode-use-Bartender-Code\\SN.btw"

-Thiết lập tên cho QR code (Share/Name)

Bước 2: Viết code C# in mã vạch QR Barcode từ tem mẫu barcode bartender trong Window Form.
-Tạo Form : PrintBarcodeSN

- Sau khi tạo Form "PrintBarcodeSN" xong bạn cần Add References thư viện Interop.BarTender.dll

Code C# Print Barcode từ tem bartender.
- Viết code sự kiện khi click chuột vào nút button "Print Barcode".
Ví dụ
private void btn_PrintBarcodeSN_Click(object sender, EventArgs e)
{
BarTender.Application btApp;
BarTender.Format btFomart;
btApp = new BarTender.Application();
if (File.Exists("D:\\Ihoclaptrinh.com\\Print-Barcode-use-Bartender-Code\\SN.btw"))
{
btFomart = btApp.Formats.Open("D:\\Ihoclaptrinh.com\\Print-Barcode-use-Bartender-Code\\SN.btw", false, string.Empty);
}
else
{
MessageBox.Show("Error: The Template is not exist !");
return;
}
try
{
if (txt_BarcodeSN.Text.Trim() != "")
{
btFomart.SetNamedSubStringValue("BarcodeSN", txt_BarcodeSN.Text.Trim());
btFomart.PrintOut(false, false);
}
else
{
MessageBox.Show("Error: Barcode SN is empty, please input Barcode SN.");
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
//Console.WriteLine(ex);
}
finally
{
btFomart.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
}
- Chạy chương trình và nhập thông tin và in vã vạch QR code.
