I have to print some barcodes on a label (which measures 4 x 3 inches), the barcodes I send them to a panel doing them with the library BarcodeLib, after reading a code this throws another that I have related to a database and display it in a panel with the following code
if (e.KeyChar == (char)Keys.Enter)
{
if (textBox1.Text != "")
{
Barcode codigo = new Barcode();
codigo.IncludeLabel = true;
DataTable buscar = existente.busqueda(textBox1.Text, textBox1.Text);
if (buscar.Rows.Count > 0)
{
DataRow row = buscar.Rows[0];
etiquetaNombre = Convert.ToString(row["nombre"]);
panel1.BackgroundImage = codigo.Encode(TYPE.CODE128, etiquetaNombre, Color.Black, Color.White, 300, 99);
guardado = true;
}
else
{
panel1.BackgroundImage = codigo.Encode(TYPE.CODE128, Convert.ToString("No Existe"), Color.White, Color.White, 300, 99);
MessageBox.Show("No se encontro un nombre asociado a este numero " + textBox1.Text);
panel1.BackColor = Color.White;
textBox1.Text = "";
}
}
}
I do the same with a second panel that would have a quantity
Then with a button to print the panels that is as follows
PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(pd_PrintPage2);
PrintDialog PrintSettings = new PrintDialog();
PrintSettings.Document = doc;
PageSettings pgsetting = new PageSettings();
if (PrintSettings.ShowDialog() == DialogResult.OK)
doc.Print();
Which sends for calling the following method
void pd_PrintPage2(object sender, PrintPageEventArgs e)
{
int w = e.MarginBounds.Width / 2;
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Font printFont = new Font("Code 128", 40);
Bitmap logo = new Bitmap(panel1.Width, panel1.Height, panel1.CreateGraphics());
panel1.DrawToBitmap(logo, new Rectangle(0, 0, panel1.Width, panel1.Height));
Bitmap logo2 = new Bitmap(panel2.Width, panel2.Height, panel2.CreateGraphics());
panel2.DrawToBitmap(logo2, new Rectangle(0, 0, panel2.Width, panel2.Height));
int height = 100 + y;
string tabDataText = textBox2.Text;
var tabDataForeColor = Color.Black;
var txtDataWidth = e.Graphics.MeasureString(tabDataText, printFont).Width;
using (var sf = new StringFormat())
{
height += logo.Height + 15;
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(tabDataText, new Font("bc39CR8", 40),
new SolidBrush(tabDataForeColor),
e.MarginBounds.Left + (e.MarginBounds.Width / 2),
e.MarginBounds.Top + (e.MarginBounds.Height / 2) + (logo.Height / 2) + 15,
sf);
}
e.Graphics.DrawImage(logo2,
e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (logo2.Width / 2),
e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (logo2.Height));
e.Graphics.DrawImage(logo,
e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2),
e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (logo.Height));
e.HasMorePages = false;
}
}
But with this code it only prints one of the bar codes, and the other one seems not to take it into account, so I did it with a text with a source to do it bar code but I want to know if there is a better way to do it and also be able to write on top of each barcode what is as name and quantity