Print barcode from client side using ASP.NET - c#

I need to print a barcode from client Machine, While running the program in Visual Studio, the barcode prints perfectly but when the code is published & put into the IIS server, the barcode is not printing on the client machine but rather prints on the server side.
Even if I try to print from client machine it does not work. Once the barcode printer machine connects with the server it prints all the barcode given on client machine as well as on the server. The printer is dedicated to barcode printing only and is connected to the server and not the client.
"The barcode MUST print on the CLIENT MACHINE"
MY CODE:
protected void btnPrint_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
foreach (String printer in PrinterSettings.InstalledPrinters)
{
list.Add(printer.ToString());
}
string printerName = "";
for (int i = 0; i < list.Count; i++) // Loop through List with for
{
printerName = list[i].ToString().ToLower();
if (printerName.Equals(#"tsc ttp-244 plus"))
{
// Console.WriteLine("Printer = " + printer["Name"]);
if (list[i].ToString().ToLower().Equals("true"))
{
// printer is offline by user
//lblMsg.Text = "Your Plug-N-Play printer is not connected.";
}
else
{
// printer is not offline
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrinterSettings.PrinterName = "TSC TTP-244 Plus";
pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 197, 98);//Document No Size
// pd.DefaultPageSettings.Margins = new Margins(70, 12, 12, 12);
// pd.DefaultPageSettings.Margins.Left = 100;
pd.Print();
}
}
}
PlBarcodeFileNo.Controls.Add(_BarcodeImageView(lblBarcode.Text));
}
private System.Web.UI.WebControls.Image _BarcodeImageView(string barCodeimgNo)
{
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCodeimgNo.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCodeimgNo + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
}
return imgBarCode;
}

Related

Adding String Literal to Generated Barcode image

I'm creating a web app that generates barcodes that uses information that's databound. That information is a name from a database.
The barcode generates correctly, but I want to add text to it.
Here is my code for the barcode generator.
protected void btnGenerate_Click(object sender, EventArgs e)
{
foreach (ListItem item in BarCode.Items)
{
if (item.Selected)
{
string barCode = Barcode + txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 50, 90))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 18);
PointF point = new PointF(3f, 3f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
}
}
}
I want to add
"QTY:_____________"
underneath the barcode when it's generated. Although I believe the formatting of the barcode is limited to the code and I don't believe I can create a string literal
string lit = "QTY:_______________";
and add it to the barcode string:
string barCode = Barcode + txtCode.Text + Environement.NewLine + lit;
Is it possible to add that underneath programmatically?
If I understand you right You don't need to add text into code, you need to print some text under bar code. Just add into:
using (Graphics graphics
somthig like this:
// put coordinates here:
RectangleF rectf = new RectangleF(70, 90, 90, 50);
graphics.DrawString(lit , new Font("Tahoma",8), Brushes.Black, rectf);

Generated Barcode Image not getting scanned through scanner in asp.net web application

I have generated dynamic barcode image in asp.net web application but its not getting scanned/reading through barcode scanner.
Below is the code where i have generated barcode image.kindly find it out.
Need Your Suggestion and help,awaiting your response.
string barCode = LblSerialNo.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 11, 45))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 8);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}

Barcode not generated in C# web forms?

I am trying for create a barcode in my web form.for that i download font IDAutomationHC39M and install in my system,then i run my wesite in localhost but barcode cannot be generated.This is my code
protected void Button1_Click1(object sender, EventArgs e)
{
string barCode = TextBox1.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
And the result is appear like that code

Add absolute position text to pdf in memorystream

I need to add text to the lower right corner of a pdf. I was able to successfully do this with an annotation but I couldn't flatten it so I attempted to add the text via a paragraph instead. Now none of my text is showing up. Could anyone point out where I'm going wrong with my code below? (Or maybe just tell me how to flatten an annotation... the commented out portion works fine I just need it flattened)
private MemoryStream AddPageNumbers(MemoryStream stream)
{
MemoryStream ms = new MemoryStream();
PdfStamper Stamper = null;
PdfReader Reader = new PdfReader(stream);
try
{
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(Reader);
Copier.Close();
PdfReader docReader = new PdfReader(ms.ToArray());
ms = new MemoryStream();
Stamper = new PdfStamper(docReader, ms);
for (int i = 1; i <= Reader.NumberOfPages; i++)
{
//iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(315, 19, 560, 33);
//var pcb = new iTextSharp.text.pdf.PdfContentByte(Stamper.Writer);
//string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();
//FontFactory.RegisterDirectories();
//Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 8f, Font.NORMAL));
//Paragraph para = new Paragraph(PageNumber, fontNormalArial);
//var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(Stamper.Writer, newRectangle, para.Content, pcb);
//annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;
//annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
//Stamper.AddAnnotation(annot, i);
//Stamper.FreeTextFlattening = true;
//Stamper.FormFlattening = true;
PdfContentByte cb = new PdfContentByte(Stamper.Writer);
string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();
FontFactory.RegisterDirectories();
Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 10, Font.NORMAL));
Paragraph para = new Paragraph(PageNumber, fontNormalArial);
para.Alignment = Element.ALIGN_RIGHT;
ColumnText ct = new ColumnText(cb);
ct.AddText(para);
ct.SetSimpleColumn(315, 19, 560 , 38);
ct.SetIndent(316, false);
ct.Go();
}
}
catch (Exception ex)
{
string querystring = "?error=" + ex.Message.ToString();
Response.Redirect("~/ErrorPage.aspx" + querystring);
}
finally
{
if (Stamper != null)
{
Stamper.Close();
}
}
return ms;
}

Write text on a bitmap image in C#

Well, I try to write on an image in C#, my code is:
public string WriteOnImage(Bitmap Image, string NameImage, string TextFileName)
{
string Message = "OK";
try
{
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(Image))
{
graphImage.SmoothingMode = SmoothingMode.AntiAlias;
string line;
// Read the file and display it line by line.
StreamReader file = new StreamReader(Resources.C_PATH_DESTINO_IMG + TextFileName);
while ((line = file.ReadLine()) != null)
{
graphImage.DrawString(line, new Font("Courier New", 15, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));
HttpContext.Current.Response.ContentType = "image/jpeg";
bitMapImage.Save(Resources.C_PATH_DESTINO_IMG + NameImage, ImageFormat.Jpeg);
graphImage.Dispose();
bitMapImage.Dispose();
}
file.Close();
}
return Message;
}
catch (Exception ex)
{
EventLogWrite("Error: " + ex.Message);
return Message = ex.Message;
}
}
this method doesn't work because doesn't write on the image, please help me.
PD: I'm sorry for my english but I'm Latino jeje, thanks.
It looks like you are drawing on the wrong bitmap
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(Image))
should be
Bitmap bitMapImage = new Bitmap(Image);
using (Graphics graphImage = Graphics.FromImage(bitMapImage))

Categories

Resources